2011-05-14 06:38:45 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Define internal PaperScope class that handles all the fields available on the
|
|
|
|
* global paper object, which simply is a pointer to the currently active scope.
|
|
|
|
*/
|
|
|
|
var PaperScope = this.PaperScope = Base.extend({
|
2011-05-15 06:32:42 -04:00
|
|
|
initialize: function(id) {
|
2011-05-14 06:38:45 -04:00
|
|
|
this.document = null;
|
|
|
|
this.documents = [];
|
|
|
|
this.tools = [];
|
2011-05-15 06:32:42 -04:00
|
|
|
this.id = id;
|
2011-05-15 19:29:29 -04:00
|
|
|
PaperScope.scopes[id] = this;
|
2011-05-14 06:38:45 -04:00
|
|
|
},
|
|
|
|
|
2011-05-15 20:22:06 -04:00
|
|
|
evaluate: function(code) {
|
|
|
|
return PaperScript.evaluate(code, this);
|
|
|
|
},
|
|
|
|
|
2011-05-14 06:38:45 -04:00
|
|
|
/**
|
|
|
|
* Installs the paper scope into any other given scope. Can be used for
|
|
|
|
* examle to inject the currently active PaperScope into the window's global
|
|
|
|
* scope, to emulate PaperScript-style globally accessible Paper classes:
|
|
|
|
*
|
|
|
|
* paper.install(window);
|
|
|
|
*/
|
|
|
|
install: function(scope) {
|
|
|
|
// Use scope as side-car (= 'this' inside iterator), and have it
|
|
|
|
// returned at the end.
|
|
|
|
return Base.each(this, function(value, key) {
|
|
|
|
this[key] = value;
|
|
|
|
}, scope);
|
2011-05-14 07:15:31 -04:00
|
|
|
},
|
|
|
|
|
2011-05-15 19:56:47 -04:00
|
|
|
clear: function() {
|
2011-05-15 19:29:29 -04:00
|
|
|
// Remove all documents and tools.
|
|
|
|
for (var i = this.documents.length - 1; i >= 0; i--)
|
|
|
|
this.documents[i].remove();
|
|
|
|
for (var i = this.tools.length - 1; i >= 0; i--)
|
|
|
|
this.tools[i].remove();
|
|
|
|
},
|
|
|
|
|
2011-05-15 19:56:47 -04:00
|
|
|
remove: function() {
|
|
|
|
this.clear();
|
|
|
|
delete PaperScope.scopes[this.id];
|
|
|
|
},
|
|
|
|
|
2011-05-14 07:15:31 -04:00
|
|
|
statics: {
|
2011-05-15 19:29:29 -04:00
|
|
|
scopes: {},
|
2011-05-14 07:15:31 -04:00
|
|
|
|
2011-05-15 19:29:29 -04:00
|
|
|
get: function(id) {
|
|
|
|
return this.scopes[id] || null;
|
2011-05-14 07:15:31 -04:00
|
|
|
}
|
2011-05-14 06:38:45 -04:00
|
|
|
}
|
|
|
|
});
|