I'm trying to use paper.js in a webapp, but I've been unable to get it to work with multiple canvases. It's like the scopes are getting mixed up between the canvases, so when I intend to draw on canvas 1, it appears on canvas 2.
In each view, I'm initialize the paper like this:
this.mypaper = new paper.PaperScope();
this.mypaper.setup($("myCanvasId")[0]);
When I create new paper objects, I use what should be the local scope:
var circle = new this.mypaper.Path.Circle(10, 10, 5);
However, when I create a circle in view1, it draws it in view2 instead.
I've done a lot of reading, but I still haven't found a clear explanation of how to setup multiple paperscopes or how to isolate views from each other.
Does anyone know how to use multiple canvases with paper.js correctly?
EDIT: I've created a jsFiddle to illustrate the problem: http://jsfiddle.net/94RTX/1/
In order to more explicitly manage which paperscope you are adding items to, you might consider setting the option
insertItems
tofalse
.That way, when you create new paper items, they are not automatically added to the paper. So, no matter which scope your paper item was created in, you still decide to add it to one paper or another. For example, you can theoretically do:
I haven't worked with Paper.js extensively, but it seems that each call to
Path
isn't using thePaperScope
from which it's being accessed, but the globalpaper
object. So if you overwritepaper
to your desired PaperScope before each instantiation it should work.See my updated fiddle.
Use arrays to separated your papers.
EDIT: I've update your js fiddle: http://jsfiddle.net/94RTX/3/
Apparently each setup erase the previous one, so the solution is to do it in this order:
I think I found the solution: I expanded the fiddle from @freejosh to also work with callbacks (e.g. resize): The trick is to re-fetch the correct scope inside the callback function:
http://jsfiddle.net/rassoh/mx9n3vsf/7/
Please note that I also included a simple interaction with the circles, to also test for correct behaviour there.
I actually solve this a bit differently:
When I want to draw in scope1:
Similarly when I want to draw in scope 2