offscreen Mgraphics > jit.gl.texture - can't clear screen

Robert White's icon

Hello, I want to use mgraphics offscreen (not jsui) to pipe into a jit.gl.pbr texture. I need to maintain the transparency. I have this working using in a jsui using a modified version of Darwin Gross's patch-a-day (1129-ToJitter). But when I try translating the same thing using to use an offscreen context of mgraphics I'm not able to clear the screen so everything just accumulates.

In the jsui version Darwin is sending a pattern(pop_group) > matrix out. I don't understand why this is working in the jsui version and not the offscreen version. Anybody know?

mgraphics-jsui.zip
zip
jit.gl.texture includes transparency + redraws correctly
mgraphics-offscreen.zip
zip
How do I clear theImage every frame using mgraphics offscreen?


TFL's icon

I didn't even know it was possible to use MGraphics in [js] instead of [jsui]. I wonder if it is supported at all?

From my understanding, you're supposed to call mgraphics.init() at the beginning of any code making use of the mgraphics API. This won't work in [js] because it hasn't been made to initialize a mgraphics context inside of a non-UI object. Hence the wrong behavior with the mgraphics commands you use.

This said, I was able to get the the mgraphics image to clear by calling var mg = new MGraphics(400, 400) every frame (in the draw method) instead of globally, which I guess gives you the result you want.

mgraphics-offscreen.zip
zip

Oh, and I wasn't aware of this image.tonamedmatrix() method (which is awesome!). It is not in the current documentation, not sure if it is now unsupported or just a miss.

Alternatively, another approach to get a mgraphics drawing into a matrix directly, without drawing it on screen, is to use [jit.mgraphics] although I admit it is much less practical to maintain long code like in your example.


Robert White's icon

Thanks for fixing the code. I completely overlooked jit.mgraphics which would work for most cases. However what I'm trying to do is breakup SVG file and animate the opacity for each layer. I thought that offscreen would help with frame rates given a lot of SVG layers. For the most part it works great! But for instance here I'm animating the opacity on 112 SVG layers and If I were to display them all at the same time my CPU would grind to a halt. But it's ok if you turn a dozen or so at the same time which is what I'm looking to do.

I wanted to share my implementation using js offscreen. Thanks again!

offscreenMgraphicsSvg2Jitter.zip
zip