Created
May 25, 2017 08:21
-
-
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.
This file contains 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
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