How can I clear an mgraphics canvas?

Noah's icon

Hey folks.

So today I managed to fumble my way through using jsui to render a picture of a buffer's waveform. The only problem with it is that if I tell it to to render a different buffer, it just adds to what it's previously drawn, rather than starting fresh. The obvious solution to this would be to somehow clear the current canvas, but after combing over the mgraphics quickstart guide, the forum, and the jsui object I've been using as a sort of reference (warpy2.js), I'm still stumped on how exactly I go about that. It seems the redraw method is made for that sort of thing, but personally I'm not having any luck with it. Anyone have any ideas?

js-wave.zip
zip
Nikolas K's icon

Hi Noah,

If you check the "warpy2", every time the buffer gets replaced, it creates a new MGraphics instance, so it doesn't actually redraws on the preview "ocan".
Just add the following line in your replace function, before the drawing part and it will work.ocan = new MGraphics(w, h);
If you are wondering about what happens to the previews "Mgraphics" instance, I think the JavaScript's garbage collector picks it up at some point and deletes it, so you don't have to worry about that!

-N

Noah's icon

Man, I was worried it'd be some sort of silly misunderstanding like that. Like I said though I'm still kind of fumbling my way through this. Thanks very much for the help!

Nikolas K's icon

Don't worry, it took me a while to figure it out the first time i tried something like that! Whole day searched the forums for exactly that - clearing an mgraphics canvas!!

Cheers!

Ted Wiggin's icon

I'm having a similar issue but I'm using jit.mgraphics rather than jsui--
I noticed the clear routine in the help file resets it to a black canvas with a full alpha, but I'm unable to find a way to reset it to blank canvas with no alpha

Anyone point me in the right direction? I'm struggling to replicate this using max messages

Rob Ramirez's icon

hey Ted! I had to dig in the forums to find this, but we added a message to jit.mgraphics a while back that should do exactly what you want: clear_surface

sending this message will zero out all color planes, including alpha. hope this helps.

Ted Wiggin's icon

Bingo! Thank you a ton Rob!