We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I asked this question the other day but clicked on "answered" too early.
I have an image that shoud adapt to the window size, scaling down when the window is smaller then the image. It should stop scaling up when the image is 100%. The problem is that the resize command discards the resolution of the image, so that when you scale down, then up again, the image is completely blurry. How to fix this?
function setup() {
moon = loadImage("assets/moonwalk.jpg");
createCanvas(windowWidth, windowHeight);
}
function draw() {
background(255);
image(moon,0,0);
moon.resize(windowWidth,0)
}
Answers
image(moon,0,0, windowWidth, windowHeight);
is not the right answer, because that will distort the image ratio.I think that's the way to do it, you just have to ensure that the new width and height maintain the original aspect ratio.
I think I solved it. It could be prettier and there is some resizing issue, but in principle, it works:
https://alpha.editor.p5js.org/mxa/sketches/Hy9CEZj5-
@xna -- if resizing is rare then you can increase performance by:
setup()
load the original image and copy it to a separate display imagedraw()
when theif
triggers, copy and resize the original to the displaydraw()
, callimage()
with only two coordinates -- now you aren't scaling the image every single frame.This should give you better performance while still avoiding the scaling compression problems you mentioned here: https://forum.processing.org/two/discussion/24139/resizing-an-image-without-losing-resolution
related: https://gist.github.com/SIRHAMY/1c0b7581e82e6eaf6e8a
@jeremydouglass -- cool. I've implemented that. I think that's as optimized as it gets:
@xna -- looking good! :-bd
Tiny caveat: When resizing the browser window very fast, it is possible to end up with a smaller image in a big window, I guess there is a limit from the Browser in how often the windowResized is called
This might fix that (untested):