We are about to switch to a new forum software. Until then we have removed the registration on this forum.
EDIT: I do not believe this should be moved to / belongs in the Raspberry Pi category. The Pi is my motivation for doing this but I am running this at the moment on a normal windows install of Processing. The Pi is irrelevant for the time being.
I am using processing to make some gauge meters. My usual method is to draw a lot of primitives, but now that I'm trying to move to use the Raspberry Pi, I've noticed that my previous techniques are too intensive for it and the FPS drop makes it unusable.
So I'm trying a new technique. To simply draw an overlay to stamp over a primitive that represents the meter, to reduce the math required. This presents a problem, however, as I can't appear to figure out how to 'draw' transparency into a PGraphics thing. I want transparency where black is, essentially. How do I achieve this? I want the ring of black in the square gauge to be transparent and to show whatever I have behind it, in this case, the white pie graph. I will then draw text on top of this, and it should be a pretty clean way to draw gauges.
Thank you for the help!
PGraphics gaugeoverlay;
void setup() {
size(500, 500);
gaugeoverlay = createGraphics(200, 200); gaugeoverlay.beginDraw();
gaugeoverlay.background(0);
gaugeoverlay.endDraw();
drawgauge();
}
void draw() {
fill(255);
noStroke();
arc(100, 100, 170, 170, 0, PI+QUARTER_PI, PIE);
image(gaugeoverlay, 0, 0);
}
void drawgauge() {
gaugeoverlay.beginDraw();
gaugeoverlay.fill(100);
gaugeoverlay.noStroke();
gaugeoverlay.rect(10, 10, 180, 180, 10);
gaugeoverlay.fill(0);
gaugeoverlay.ellipse(100, 100, 170, 170);
gaugeoverlay.fill(100);
gaugeoverlay.ellipse(100, 100, 150, 150);
gaugeoverlay.endDraw();
}
Answers
Both fill() & stroke() got the alpha parameter:
https://Processing.org/reference/fill_.html
I have attempted to add those values but all it does is make the ring transparent in relation to the grey rectangles in the PGraphics. What I want to do is make it so the grey is drawn, and all the black in the image is actually transparent so when I draw it on top of something else, I can see whatever is revealed by the black ring, which should have no pixels at all. I'm not sure how to do this.
The only black at your PGraphics is this 1:
gaugeoverlay.fill(0);
Have you really tried the alpha parameter, as taught by the reference?
gaugeoverlay.fill(0, 0);
Yes, I have tried that, the result appears to be that the black ring disappears. I assume because it is completely transparent and not drawn? Or am I mistaken and something else is happening?
Is there some kind of mode for drawing shapes/lines that 'erases' pixels instead of setting them? There appears to not be a way to set transparent pixels.
Here is what I have tried. The ring simply no longer renders.
You're right. Indeed Processing doesn't have a way to erase things, but background() & clear().
Once something is drawn at some place, drawing primitives like ellipse(), rect(), etc, can't remove it.
That happens b/c Processing blends the colors of the existing pixel w/ the pixel about to be drawn.
You'll have to create your own ellipse() function which got a chosen unblending color via pixels[].
Darn. Okay, thank you. I'll have to try something else, then.
Here's a very antique example: O:-)
https://forum.Processing.org/two/discussion/598/mousewheel-syntax-question-solved
However, given it's very old, in order to work w/ latest Processing versions, you're gonna have to add some
canvas.beginDraw();
statements there. :-<Is it possible to adapt the program on this thread to operate on a PGraphics instead of a PImage?
https://forum.processing.org/one/topic/turn-white-pixels-transparent.html
Class PGraphics is a subclass of class PImage.
Everything (members) which PImage have, PGraphics have equally and more! :-bd
PGraphics transparency is possible and even normal. The thread title "impossible" is misleading.
You are conceptually on the wrong track about masking with trying to "erase" pixels. This is because you are trying to draw the controls first, then the control plate. A much easier approach: draw on transparent surfaces.
Here is an example sketch with one control, and the mask cutout shown next to it. Notice the mask is drawn into a transparent PGraphics image: possible!
Add as many cutouts to
gmask
and as many controls as you want togcontrols
, (two screen-size PGraphics layers), then combine once and display once withgcontrols.mask(gmask);
image(gcontrols,0,0);
.(accidental duplicate post)
Thank you so much! This is exactly what I wanted to do. I'm going to adapt my old gauge routine to do this instead.
Indeed mask() is a more advanced way to force transparent "holes" through PGraphics layer.
But I think Processing should have some kinda hint() for turning off the auto-color blending for primitive drawings like ellipse(), rect(), etc.
That is, the
color
set in fill() & stroke() would be drawn as is, rather than blending pixel-by-pixel over the existing pixels already in the canvas.That'd be much faster too! \m/
Of course, if you weren't developing a whole interface and just wanted simple gauges, you don't need masking. Instead you can use the
OPEN
mode of arc() and draw a heavy strokeWeight with no fill.@GoToLoop -- so, if I'm understanding you right, something like
PImage.blend()
orPGraphics.blend()
would be set on by default, but settingPGraphics.noBlend()
could have all write operations replace target with source? Then "erasing" would work e.g....although it wouldn't work on the main sketch surface -- because no transparency.
I'm mostly looking to convert an existing program to involve less intensive operations so the Pi doesn't choke, as it appears to have massively less processing performance then the PC does. It takes a billion drawing operations to do it though, which I assume is why.
This is what it looks like, I'm hoping to update it a bit and make it a bit slicker looking, too. I'm already having a good time with this masking system.
It flashes red and stuff when values are critical, obviously. I like it.
Very cool! I see the stroke-nofill mask approach to your gauges also helps helping with those rounded-rect buttons.
@jeremydouglass, something like that. That'd make alpha "holes" more easily.
And most importantly, pixel color blending is surely very expensive.
Having the option to turn that off would gr8 increase performance for primitive drawings. $-)
@GoToLoop --
I thought about writing this up as a feature request, then I just realized, isn't
blendMode(REPLACE)
already a hint() for turning off the auto-color blending for primitive drawings? I wasn't aware it existed.blendMode() reference
Wow, me neither! 3:-O I didn't pay attention of that new stuff b/c I thought it was related to PImage rendering, like blend(), instead of primitive drawings. b-(
@itman -- could you share that image from above ( dronedisplayanimation.gif ) again?
@jeremydouglass
Hello! I apologize for not noticing this sooner. I did not get any sort of notification. Here is the original gif, it appears dropbox nuked it when public sharing died?
Thanks so much, @ITman496. I'll cross-post it somewhere in the new forum -- unless you would like to list it there in the Gallery as a project to inspire others working on similar things.
https://discourse.processing.org/c/gallery
...I had actually found a copy a couple weeks ago -- somehow -- and posted it here.
https://discourse.processing.org/t/turn-gauge-arc-into-gradient/4103/11
@jeremydouglass
Glad you found a copy! I will move to the new forum. I'm no longer working on this particular project anymore (finished!) so you can cross-post however you like =)
I actually am back here because I want to adapt this trick to build a very similar gauge display for my ultralight aircraft I just got! And it is running on a Pi, so I do need it to be optimized. So again, thank you for the help.
See you on the other forum -- and looking forward to hearing more about the ultralight