简体   繁体   English

有没有办法在保持像素大小的同时缩放 p5js 草图?

[英]Is there a way to scaling a p5js sketch, while maintaining its pixel size?

I'm working on a project in p5.js where I need to be able to define obscure canvas sizes that are much bigger then my browser window.我正在 p5.js 中处理一个项目,我需要能够定义比浏览器窗口大得多的模糊画布大小。 I essentially need to maintain a p5js pixel size (ex. 3840px, 1920px) but have the entire canvas in view.我基本上需要保持 p5js 像素大小(例如 3840 像素、1920 像素),但要查看整个画布。 Think of how you can zoom in and out of a canvas in photoshop, thats the functionality I'm trying to achieve.想想如何在 photoshop 中放大和缩小画布,这就是我想要实现的功能。

For now, zooming in and out with the default browser functionality is Okay but not ideal and causes some other errors with arrow key presses etc. I've looked all over processing, p5 and html canvas forums and can't seem to find my exact scenario.目前,使用默认浏览器功能放大和缩小是好的,但并不理想,并会导致其他一些错误,如箭头键按下等。设想。

There are a few ways you might approach this.有几种方法可以解决这个问题。 In no particular order:没有特定的顺序:

  • Keep track of a zoom multiplier.跟踪缩放倍数。 Use this multiplier in all of your drawing code, for both coordinates and sizes.在所有绘图代码中使用此乘数,用于坐标和大小。 Change this multiplier when the user scrolls the mouse wheel or takes some other action.当用户滚动鼠标滚轮或执行一些其他操作时更改此乘数。
  • Use the scale() function to change the scale of the whole canvas.使用scale()函数更改整个画布的比例。
  • Use the createGraphics() function to create a drawing buffer.使用createGraphics()函数创建绘图缓冲区。 This buffer can be as large as you want it.这个缓冲区可以大到你想要的大小。 Draw your scene to the large buffer, and then draw that buffer to your on-screen canvas.将场景绘制到大缓冲区,然后将该缓冲区绘制到屏幕画布。

Try to get something simple working, like a hard-coded circle and square.尝试让一些简单的工作,比如硬编码的圆形和正方形。 Then if you get stuck, you can post a MCVE with what you've tried.然后,如果您遇到困难,您可以发布包含您尝试过的内容的MCVE Good luck.祝你好运。

I had the opposite problem where I needed to scale a canvas of 260 x 130 up to 1080 by 720 while keeping the pixel density.我遇到了相反的问题,我需要将260 x 130的画布缩放到1080 by 720 260 x 130 1080 by 720同时保持像素密度。 I fixed this by having the following at the end of my setup() function:我通过在setup()函数末尾添加以下内容来解决此问题:

 $("#defaultCanvas0").css({ 'height': "720px" });
 $("#defaultCanvas0").css({ 'width': "1080px" });

This meant the canvas was scaled up while still maintaining the low resolution.这意味着画布被放大,同时仍保持低分辨率。 I found the usual scale() function did not work for me.我发现通常的scale()函数对我不起作用。

I know this wasn't what you were looking for at the time but maybe it'll help anyone else looking at this question in the future...我知道这不是你当时要找的东西,但也许它会帮助其他人在未来看这个问题......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM