We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to cut a rectangular hole into a red circular shape, making an image that I can later stamp -- image(myImage, x, y) -- at different canvas locations.
Something like this:
I assume I need to create 2 graphics objects (createGraphics), fill one with a red ellipse, fill the other with a smaller (black, white?) rectangular shape. What I am missing is how to do the cutting (masking) of one from the other. I've tried several blend modes but they don't seem to give me the results I want.
Thanks in advance. Alex
Answers
I'm not sure
but when you mean a hole in the center as in "I can see through the hole the image behind it":
there's no such thing as a hole afaik.
you might be better off to draw the red as 4 separate parts (
arc
) and just don't draw the center in the first place. That'd give you a hole.that wouldn't give you no transparency but just a white rectangle
;-)
There's a 'mask' listed in the p5 reference but i can't get the page to open at the moment :/
If it's been implemented it should work as per the processing example. Otherwise you could create a vector shape with a hole in and create an image from that.
thanks, I didn't find
mask
in the reference...mask was the ticket. Thanks for this.
However, it looks like p5.Graphics objects don't have a mask method while p5.Image objects do. So I ended up doing this:
and done, almost. On my Retina display, the resulting stamp was too small. Calling devicePixelScaling(false) solved it.
Thanks again.
@alexR, I've answered something similar in this forum thread:
http://forum.processing.org/two/discussion/10291/issue-with-color-alpha
The issue is that we need to rect() w/ an opaque fill() in order to override the bigger ellipse()'s.
But you want that rect() to be 100% transparent though.
My solution is to hunt down every pixel which matches w/ rect()'s fill().
Then assign its alpha channel to
0
if so. :DFor now, see the "Java Mode" version 1st. Gonna paste p5.js's 1 next post: :P
Here it is the p5.js version. However it's somewhat very buggy! :-&
noStroke(), ellipseMode() & rectMode() aren't working for createGraphics() at all! ~X(
We can paste and run it from here if we wish: http://p5js.org/reference/#/p5/clear
As a way to circumvent the buggy p5.Graphics class, we can change canvas' styles instead. /:)
So instead of the expected code below which would address the p5.Graphics individually:
We go w/ directly changing p5's global canvas itself: :O)
Thanks a lot for your in-depth answer, GoToLoop. It's too bad that p5.graphics is buggy and that it doesn't implement masking. This would have made p5 a solid contender for my current project, other features I have yet to look into notwithstanding.
My solution doesn't demand any mask() b/c the inner square was made fully transparent already.
But I dunno where did you get the idea that p5.js doesn't support mask()! :-\"
Indeed, as @blindfish has discovered, its true mask()'s web reference is buggy as well: :O)
http://p5js.org/reference/#/p5/mask
However, I've found out some working example here: :-$
http://p5js.org/examples/examples/Image_Alpha_Mask.php
Oh, now I've got what you meant: That only p5.Graphics lacks mask(). b-(
Much probably b/c p5.Graphics doesn't necessarily "inherits" from p5.Image like in "Java Mode". ~:>
But fix is very simple... Just do what I did at the end of maskedCircle():
pg.get();
>-)Method get() w/o or w/ 4 arguments returns a
new
p5.Image after all:http://p5js.org/reference/#/p5/get
Forum editor problem. See next post.
p5.Graphics.get... I had missed that and it sure helps. Thanks a lot for that.
I added a punchOut (opposite of mask) method to p5.Image:
Then tested it: success.
Thanks again.