p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Trouble with createGraphics and updatePixels() and/or mask() when pixel density is not 1

Open dnassler opened this issue 12 years ago • 4 comments

I have a problem with the graphics buffer from createGraphics() that only exists when I use a browser on a retina display.

I draw into a graphics buffer then use that as a mask for an image. The masked image is perfect on a non-retina display but double on a retina display (retina displays have double the pixel density).

I've also noticed that when I read the pixels from my graphics buffer directly by doing a call to updatePixels on my buffer then the pixel array seems to not be correct (the image drawn into the graphics buffer is double sized).

var img;

function preload() {
  img = loadImage("randomSquare100.png"); // img is size 100x100 pixels
}

function setup() {
  createCanvas(100, 100);
  var pg = createGraphics(width,height);
  pg.clear();
  pg.fill(0,0,255,255);
  pg.rect(0,0,width,10);
  img.mask(pg);
  image( img, 0, 0 );
  noLoop();
}

function draw() {
}

On a non retina display the masked out area is 10 pixels high as expected. But on a retina display (iPad, iPhone, iBook retina) the masked out area is 20 pixels high!

dnassler avatar Sep 10 '14 20:09 dnassler

interesting... when we were making the hello.p5js.org we were messing around with this and I thought that we arrived at the correct scaling settings to make the p5.buffer work, but I guess not. thanks for logging this one, I'll take another look as soon as I can.

lmccart avatar Sep 11 '14 17:09 lmccart

hmm having trouble reproducing this. can you tell me which version of the library you're using?

lmccart avatar Sep 19 '14 15:09 lmccart

I was using version 0.3.5.

I've retested with version 0.3.6 and then 0.3.7 and the problem as shown in my example is not reproducible anymore.

But I just tried tweaking my test code above and then can reproduce the problem again. Note that this problem only occurs on my retina display macbook pro, iphone, ipad (all retina displays). On my regular desktop there is no problem.

  var tempImg = createImage(width,height);
  tempImg.set(0,0,img);
  tempImg.mask(pg);
  image( tempImg, 0, 0 );

  // img.mask(pg);
  // image( img, 0, 0 );

dnassler avatar Sep 20 '14 13:09 dnassler

ok thanks, will check this one out

lmccart avatar Sep 20 '14 14:09 lmccart