using push and pop with jit.mgraphics

madbutter's icon
Max Patch
Copy patch and select New From Clipboard in Max.

I love mgraphics, I hate mgraphics, I love mgraphics, I....
I must say, mgraphics, like its fellow bastard stepchildren Dict and SQL, is a super powerful addition to the max arsenal that is so poorly documented that trying to use it is a serious exercise in masochism. The mgraphics patch a day is a big help but it can be maddeningly difficult to use what I learn there with the altered syntax of jit.mgraphics.
Here is my current problem, I am hoping someone can help!
I want to draw something complicated that is the same every time through my loop, like the gradient in this example patch, then draw something else different each time over it, like some random black rectangles. So I understand from some JSUI examples, I issue a push_group command, write my gradient with all of its stops, then pop_group to save this as an image or surface that I can then recall quickly and efficiently at the beginning of every cycle by calling image_surface_draw. But I get errors using pop_group, which isn't in the command list of the jit.mgraphics help, so I tried pop_group_to_source. I can't "re-call" this image in the second step though.
Here is the patcher, the first example with no push and pop and the second with it. Can anyone tell me how to get the second to work?
Thanks, Bob

Ben Bracken's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Unfortunately, push/pop is not currently exposed in jit.mgraphics, though it is in JS. What you would need to do instead is to use a named matrix as a "buffer" for a surface, then use image_surface_create/image_surface_draw to create the surface, then perform your changes. Maybe something like this:

Valery_Kondakoff's icon

Do I need to use [jit.matrixset] if I need to redraw the overlay? Something like this, or there are better ways?

Max Patch
Copy patch and select New From Clipboard in Max.

Thank you!