We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a function that grabs an image out of an array an creates a mask for that image (an ellipse on a specific location):
function getMask(image, size, w, h) {
var mask = createGraphics(1280, 720);
var img = imgList[image];
mask.fill(100);
mask.ellipse(w, h, size, size);
img.mask(mask);
return img;
}
Running this gives me a
Uncaught TypeError: Cannot read property 'width' of undefined
p5.Renderer2D._copyHelper @ p5.js:14342
p5.copy @ p5.js:20920
p5.Image.copy @ p5.js:20575
p5.Image.mask @ p5.js:20632
getMask @ sketch.js:55 // <---- this line is 'img.mask(mask)'
draw @ sketch.js:63
p5.redraw @ p5.js:16075
(anonymous function) @ p5.js:11801
_runIfPreloadsAreDone @ p5.js:11718
_decrementPreload @ p5.js:11725
img.onload @ p5.js:19883
Do I need to convert mask to an PImage? How can I get this to work?
Answers
https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
var img = imgList[image];
number
? It should be, b/c arrays are supposed to be accessed by some numerical index value!Thanks! That did the trick, I would have never found out that there is a renderer property inside the Graphics.
Also thanks for the pointers on code formatting and conventions.
Glad it worked for ya! But just to make sure, gonna try to explain the situation better: :-@
img.mask(pgMask);
should simply work.img.mask(pgMask._renderer);
[-(Actually I had to search for it about a year ago: :-\"
Proof p5.Graphics stores either a p5.Renderer2D or a p5.Renderer3D in its _renderer property:
https://GitHub.com/processing/p5.js/blob/master/src/core/p5.Graphics.js#L39
Proof that createGraphics() instantiates & returns a p5.Graphics wrapper object: :-&
https://GitHub.com/processing/p5.js/blob/master/src/core/rendering.js#L203
return new p5.Graphics(w, h, renderer, this);
Proof that createCanvas() instantiates 1 of the 2 pure p5.Renderer object types:
https://GitHub.com/processing/p5.js/blob/master/src/core/rendering.js#L92
Then returns it at the end:
return this._renderer;
:>https://GitHub.com/processing/p5.js/blob/master/src/core/rendering.js#L108