Created
January 9, 2016 20:58
-
-
Save SIRHAMY/1c0b7581e82e6eaf6e8a to your computer and use it in GitHub Desktop.
Example problem and solution for P5.js image quality issues arising from multiple calls to resize.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exImg; | |
function preload() { | |
exImg = loadImage("COMPUTER/FAKE.png"); | |
} | |
function setup() { | |
exImg.resize((windowHeight/exImg.height) * exImg.width, windowHeight); | |
} | |
function draw() { | |
image(exImg, windowWidth/2 - exImg.width/2, windowHeight/2); | |
} | |
function windowResized() { | |
exImg.resize((windowHeight/exImg.height) * exImg.width, windowHeight); | |
resizeCanvas(windowWidth, windowHeight); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exImg; | |
function preload() { | |
exImg = loadImage("COMPUTER/FAKE.png"); | |
} | |
function setup() { | |
} | |
function draw() { | |
image(exImg, (windowWidth/2) - (((windowHeight/exImg.height) * exImg.width)/2), 0, (windowHeight/exImg.height) * exImg.width, windowHeight); | |
} | |
function windowResized() { | |
resizeCanvas(windowWidth, windowHeight); | |
} |
I think there is a better way. In the Forum it was said that this is bad for performance, because it will make the image resize for display every frame. Here is an optimized version:
var moon, //image
moonCopy, //resized image of the image (visible)
imageRatio; //ratio of the image h/w
function preload() {
moon=loadImage("assets/moonwalk.jpg", img => moonCopy = img.get());
}
function setup() {
createCanvas(windowWidth, windowHeight);
imageRatio = moon.height/moon.width;
print("imageRatio: "+imageRatio);
}
function draw() {
background(255);
image(moonCopy,0,0);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
print("resizing to: "+windowWidth+" "+windowHeight);
if (windowWidth < moon.width){
moonCopy = moon.get();
moonCopy.resize(windowWidth,0);
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome! thanks.