Skip to content

Instantly share code, notes, and snippets.

@johnniehard
Created May 25, 2017 08:21
Show Gist options
  • Save johnniehard/82a2d06cc1ae4230809f722c66c318e5 to your computer and use it in GitHub Desktop.
Save johnniehard/82a2d06cc1ae4230809f722c66c318e5 to your computer and use it in GitHub Desktop.
Converts a p5js graphics object to an Image object. This way you can use an off screen buffer to create image masks.
function graphicsToImage(graphics){
var img = createImage(graphics.width, graphics.height);
img.loadPixels();
graphics.loadPixels();
for(var x = 0; x < img.width; x++){
for(var y = 0; y < img.height; y++){
idx = 4 * (y * img.width + x);
img.pixels[idx] = graphics.pixels[idx];
img.pixels[idx+1] = graphics.pixels[idx+1];
img.pixels[idx+2] = graphics.pixels[idx+2];
img.pixels[idx+3] = graphics.pixels[idx+3];
}
}
img.updatePixels();
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment