Skip to content

Instantly share code, notes, and snippets.

@SIRHAMY
Created January 9, 2016 20:58
Show Gist options
  • Save SIRHAMY/1c0b7581e82e6eaf6e8a to your computer and use it in GitHub Desktop.
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.
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);
}
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);
}
@mxa
Copy link

mxa commented Sep 19, 2017

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