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.
|
2011-05-31 09:18:37 -04:00
|
|
|
* @ignore
|
2011-05-14 06:38:45 -04:00
|
|
|
*/
|
|
|
|
var PaperScope = this.PaperScope = Base.extend({
|
2011-06-14 18:04:59 -04:00
|
|
|
/**
|
|
|
|
* The version of Paper.js, as a float number.
|
|
|
|
* @type Number
|
|
|
|
*/
|
|
|
|
version: 0.1,
|
2011-05-16 05:59:45 -04:00
|
|
|
|
2011-05-15 06:32:42 -04:00
|
|
|
initialize: function(id) {
|
2011-05-31 09:18:37 -04:00
|
|
|
/** @lends _global_# */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The global active project.
|
|
|
|
* @type Project
|
|
|
|
*/
|
2011-05-16 08:33:15 -04:00
|
|
|
this.project = null;
|
2011-05-31 09:18:37 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The global projects array.
|
|
|
|
* @type Project[]
|
|
|
|
*/
|
2011-05-16 08:33:15 -04:00
|
|
|
this.projects = [];
|
2011-05-31 09:18:37 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The active view of the active project.
|
|
|
|
* @type View
|
|
|
|
*/
|
2011-05-17 08:25:46 -04:00
|
|
|
this.view = null;
|
|
|
|
this.views = [];
|
2011-05-31 09:18:37 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The reference to the tool object.
|
|
|
|
* @type Tool
|
|
|
|
*/
|
2011-05-17 08:25:46 -04:00
|
|
|
this.tool = null;
|
2011-05-14 06:38:45 -04:00
|
|
|
this.tools = [];
|
2011-05-15 06:32:42 -04:00
|
|
|
this.id = id;
|
2011-05-16 05:59:45 -04:00
|
|
|
PaperScope._scopes[id] = this;
|
2011-05-31 09:18:37 -04:00
|
|
|
|
|
|
|
// DOCS: should the different event handlers be in here?
|
|
|
|
/**
|
|
|
|
* {@grouptitle View Event Handlers}
|
|
|
|
* A reference to the {@link View#onFrame} handler function.
|
|
|
|
*
|
|
|
|
* @name onFrame
|
|
|
|
* @property
|
|
|
|
* @type Function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the {@link View#onResize} handler function.
|
|
|
|
*
|
|
|
|
* @name onResize
|
|
|
|
* @property
|
|
|
|
* @type Function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@grouptitle Mouse Event Handlers}
|
|
|
|
* A reference to the {@link Tool#onMouseDown} handler function.
|
|
|
|
* @name onMouseDown
|
|
|
|
* @property
|
|
|
|
* @type Function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the {@link Tool#onMouseDrag} handler function.
|
|
|
|
*
|
|
|
|
* @name onMouseDrag
|
|
|
|
* @property
|
|
|
|
* @type Function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the {@link Tool#onMouseMove} handler function.
|
|
|
|
*
|
|
|
|
* @name onMouseMove
|
|
|
|
* @property
|
|
|
|
* @type Function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the {@link Tool#onMouseUp} handler function.
|
|
|
|
*
|
|
|
|
* @name onMouseUp
|
|
|
|
* @property
|
|
|
|
* @type Function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@grouptitle Keyboard Event Handlers}
|
|
|
|
* A reference to the {@link Tool#onKeyDown} handler function.
|
|
|
|
*
|
|
|
|
* @name onKeyDown
|
|
|
|
* @property
|
|
|
|
* @type Function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the {@link Tool#onKeyUp} handler function.
|
|
|
|
*
|
|
|
|
* @name onKeyUp
|
|
|
|
* @property
|
|
|
|
* @type Function
|
|
|
|
*/
|
2011-05-16 05:59: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:
|
2011-06-14 17:59:45 -04:00
|
|
|
*
|
2011-05-14 06:38:45 -04:00
|
|
|
* paper.install(window);
|
2011-05-31 09:18:37 -04:00
|
|
|
* @ignore
|
2011-05-14 06:38:45 -04:00
|
|
|
*/
|
|
|
|
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-17 08:25:46 -04:00
|
|
|
// Remove all projects, views and tools.
|
2011-05-16 08:33:15 -04:00
|
|
|
for (var i = this.projects.length - 1; i >= 0; i--)
|
|
|
|
this.projects[i].remove();
|
2011-05-17 08:25:46 -04:00
|
|
|
// This also removes the installed event handlers.
|
|
|
|
for (var i = this.views.length - 1; i >= 0; i--)
|
|
|
|
this.views[i].remove();
|
2011-05-15 19:29:29 -04:00
|
|
|
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();
|
2011-05-16 05:59:45 -04:00
|
|
|
delete PaperScope._scopes[this.id];
|
2011-05-15 19:56:47 -04:00
|
|
|
},
|
|
|
|
|
2011-05-14 07:15:31 -04:00
|
|
|
statics: {
|
2011-05-16 05:59:45 -04:00
|
|
|
_scopes: {},
|
2011-05-14 07:15:31 -04:00
|
|
|
|
2011-05-15 19:29:29 -04:00
|
|
|
get: function(id) {
|
2011-05-16 05:59:45 -04:00
|
|
|
// If a script tag is passed, get the id from it.
|
|
|
|
if (typeof id === 'object')
|
|
|
|
id = id.getAttribute('id');
|
|
|
|
return this._scopes[id] || null;
|
2011-05-14 07:15:31 -04:00
|
|
|
}
|
2011-05-14 06:38:45 -04:00
|
|
|
}
|
|
|
|
});
|