diff --git a/src/core/PaperScope.js b/src/core/PaperScope.js index cdc5c216..b5f91a57 100644 --- a/src/core/PaperScope.js +++ b/src/core/PaperScope.js @@ -189,9 +189,6 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{ // Make sure this is the active scope, so the created project and view // are automatically associated with it. paper = this; - // Link the element to this scope, so we can reuse the scope when - // compiling multiple scripts for the same element. - element.setAttribute('data-paper-scope', this._id); // Create an empty project for the scope. this.project = new Project(element); // This is needed in PaperScript.load(). @@ -237,15 +234,11 @@ var PaperScope = Base.extend(/** @lends PaperScope# */{ _id: 0, /** - * Retrieves a PaperScope object with the given id or associated - * with the passed canvas element. + * Retrieves a PaperScope object with the given scope id. * * @param id */ get: function(id) { - // If an element is passed, get the id from it. - if (id && id.getAttribute) - id = id.getAttribute('data-paper-scope'); return this._scopes[id] || null; }, diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index 467d1da2..8fe740db 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -449,14 +449,21 @@ Base.exports.PaperScript = (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 canvas = PaperScope.getAttribute(script, 'canvas'), - src = script.src; - canvas = document.getElementById(canvas) || canvas; + var canvasId = PaperScope.getAttribute(script, 'canvas'), + canvas = document.getElementById(canvasId), + src = script.src, + scopeKey = 'data-paper-scope'; + if (!canvas) + throw new Error('Unable to find canvas with id "' + + canvasId + '"'); // See if there already is a scope for this canvas and reuse // it, to support multiple scripts per canvas. Otherwise // create a new one. - var scope = PaperScope.get(canvas) + var scope = PaperScope.get(canvas.getAttribute(scopeKey)) || new PaperScope().setup(canvas); + // Link the element to this scope, so we can reuse the scope + // when compiling multiple scripts for the same element. + canvas.setAttribute(scopeKey, scope._id); if (src) { // If we're loading from a source, request that first and // then run later.