Javascript 2 Sketch contexts

frank desben's icon

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///////

mzed's icon

frank desben wrote on Thu, 14 May 2009 01:56Hi 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

mzed's icon

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".

To copy the data (optionally with blending):

Sketch.copypixels();

To use it as an image:

new Image(mysketch).

frank desben's icon

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

frank desben's icon

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

klaus filip's icon

hi frank and mzed,
catching up this thread..
can you please post a simple working example, drawing into two different Sketch-Objects.
(me working on a jsui mc.waveform~)
thanks
klaus