Forums > MaxMSP

Javascript 2 Sketch contexts

May 14, 2009 | 8:56 am

Hi all,
I got a little problem I can’t fix – which should be really easy I guess:
I want to have 2 sketch contexts in a Jsui-Script.
If I create a new Sketch-Object "mysketch", i can’t use it, see below. While the same code works if I use the normal "sketch" it works.
Do you have a hint what else I have to do with the "mysketch" instance to get it to work ?
thanks a lot
frank

//////BEGIN JSUI CODE///////
var mysketch = new Sketch(sketch.size[1],sketch.size[0]);

with (sketch) {
glclear();
moveto(0.5,0.5);
glcolor(0,1,1,1);
circle(0.25);

}

with (mysketch) {
glclear();
moveto(-0.5,-0.5);
glcolor(0,1,1,1);
circle(0.25);

}
//////END JSUI CODE///////


May 14, 2009 | 4:06 pm
frank desben wrote on Thu, 14 May 2009 01:56
Hi all,
I got a little problem I can’t fix – which should be really easy I guess:
I want to have 2 sketch contexts in a Jsui-Script.
If I create a new Sketch-Object "mysketch", i can’t use it, see below.

It doesn’t work for me either. I tried a few other ways, to no avail. Maybe it’s a bug?

mz


May 14, 2009 | 4:45 pm

It works, just not the way we imagined. From JKC:

Quote:
It renders to an offscreen bitmap, not to the screen. Useful for sprites, layers etc. From the documentation:

Every instance of jsui has an instance of Sketch bound to the variable "sketch". This is often the only instance of Sketch you will need to use. However, if you want to do things like render sprites, have multiple layers of images, or use drawing commands to create alpha channels for images, then you can create additional instances to render in. By default, when any function in your jsui object has been called the context is already set for the instance of Sketch bound to the variable "sketch".

http://www.cycling74.com/docs/max5/vignettes/js/jssketchobject.html

To copy the data (optionally with blending):

Sketch.copypixels();

To use it as an image:

new Image(mysketch).

http://www.cycling74.com/docs/max5/vignettes/js/jsimageobject.html


May 14, 2009 | 5:11 pm

thanks a lot, that solved the riddle !
best, frank


June 4, 2009 | 3:13 pm

hi,
thanks again for the explanation.

One thing that puzzles me is, or that was the reason i tried to do this:

I want to have a background that i redraw every now and then. and i have a foreground which i redraw all the time.
i would like to have the foreground in a separate context, so i only have to redraw this one (to save CPU).
BUT if i copy the pixels to the first (background) context then i have to clear the background context again for the next draw-event.
is this understandable?

is there a way to do this?
keep background unchanged and have an object in front of it redrawn several times ?

thanks a lot for the help
frank


Viewing 5 posts - 1 through 5 (of 5 total)