Reuse scopes associated with canvases to support multiple scripts per canvas.

As described in .
This commit is contained in:
Jürg Lehni 2013-02-15 00:15:44 -08:00
parent 8b4104fe19
commit 24a5016470
2 changed files with 8 additions and 2 deletions

View file

@ -167,6 +167,8 @@ var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
// are automatically associated with it.
paper = this;
this.project = new Project(canvas);
// This is needed in PaperScript.load().
return this;
},
clear: function() {

View file

@ -278,8 +278,12 @@ var PaperScript = this.PaperScript = new function() {
// retrieved through PaperScope.get().
// If a canvas id is provided, pass it on to the PaperScope
// so a project is created for it now.
var scope = new PaperScope(script);
scope.setup(PaperScript.getAttribute(script, 'canvas'));
var canvas = PaperScript.getAttribute(script, 'canvas'),
// See if there already is a scope for this canvas and reuse
// it, to support multiple scripts per canvas. Otherwise
// create a new one.
scope = PaperScope.get(canvas)
|| new PaperScope(script).setup(canvas);
if (script.src) {
// If we're loading from a source, request that first and then
// run later.