2011-05-14 06:38:45 -04:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-14 06:38:45 -04:00
|
|
|
* 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/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-14 06:38:45 -04:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-05-14 06:38:45 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-06-22 18:56:05 -04:00
|
|
|
* @name PaperScope
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-24 19:25:23 -04:00
|
|
|
* @class The {@code PaperScope} class represents the scope associated with a
|
|
|
|
* Paper context. When working with PaperScript, these scopes are automatically
|
|
|
|
* created and their fields and methods become part of the global scope. When
|
|
|
|
* working with normal JavaScript files, {@code PaperScope} objects need to be
|
|
|
|
* manually created and handled.
|
|
|
|
* The global {@link _global_#paper} object is simply a reference to the
|
|
|
|
* currently active {@code PaperScope}.
|
2011-05-14 06:38:45 -04:00
|
|
|
*/
|
2011-07-24 19:25:23 -04:00
|
|
|
var PaperScope = this.PaperScope = Base.extend(/** @lends PaperScope# */{
|
2011-05-15 06:32:42 -04:00
|
|
|
initialize: function(id) {
|
2011-05-16 08:33:15 -04:00
|
|
|
this.project = null;
|
|
|
|
this.projects = [];
|
2011-05-17 08:25:46 -04:00
|
|
|
this.view = null;
|
|
|
|
this.views = [];
|
|
|
|
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-07-24 19:25:23 -04:00
|
|
|
},
|
2011-05-31 09:18:37 -04:00
|
|
|
|
2011-07-24 19:25:23 -04:00
|
|
|
/**
|
|
|
|
* The version of Paper.js, as a float number.
|
|
|
|
*
|
|
|
|
* @type Number
|
|
|
|
*/
|
2011-07-26 05:09:31 -04:00
|
|
|
version: /*#=*/ options.version,
|
2011-05-31 09:18:37 -04:00
|
|
|
|
2011-07-24 19:25:23 -04:00
|
|
|
/**
|
|
|
|
* The currently active project.
|
|
|
|
* @name PaperScope#project
|
|
|
|
* @type Project
|
|
|
|
*/
|
2011-05-31 09:18:37 -04:00
|
|
|
|
2011-07-24 19:25:23 -04:00
|
|
|
/**
|
|
|
|
* The list of all open projects within the current Paper.js context.
|
|
|
|
* @name PaperScope#projects
|
|
|
|
* @type Project[]
|
|
|
|
*/
|
2011-05-31 09:18:37 -04:00
|
|
|
|
2011-07-24 19:25:23 -04:00
|
|
|
/**
|
|
|
|
* The active view of the active project.
|
|
|
|
* @name PaperScope#view
|
|
|
|
* @type View
|
|
|
|
*/
|
2011-05-31 09:18:37 -04:00
|
|
|
|
2011-07-24 19:25:23 -04:00
|
|
|
/**
|
|
|
|
* The list of view of the active project.
|
|
|
|
* @name PaperScope#views
|
|
|
|
* @type View[]
|
|
|
|
*/
|
2011-05-31 09:18:37 -04:00
|
|
|
|
2011-07-24 19:25:23 -04:00
|
|
|
/**
|
|
|
|
* The reference to the active tool.
|
|
|
|
* @name PaperScope#tool
|
|
|
|
* @type Tool
|
|
|
|
*/
|
2011-05-31 09:18:37 -04:00
|
|
|
|
2011-07-24 19:25:23 -04:00
|
|
|
/**
|
|
|
|
* The list of available tools.
|
|
|
|
* @name PaperScope#tools
|
|
|
|
* @type Tool[]
|
|
|
|
*/
|
2011-05-16 05:59:45 -04:00
|
|
|
|
2011-05-15 20:22:06 -04:00
|
|
|
evaluate: function(code) {
|
2011-06-21 16:49:36 -04:00
|
|
|
var res = PaperScript.evaluate(code, this);
|
|
|
|
View.updateFocus();
|
|
|
|
return res;
|
2011-05-15 20:22:06 -04:00
|
|
|
},
|
|
|
|
|
2011-07-19 18:52:32 -04:00
|
|
|
/**
|
|
|
|
* Sets up the scope for a standard project, by creating an empty
|
|
|
|
* {@link Project} object for us, along with a {@link View} for the passed
|
|
|
|
* canvas, both linked to this scope.
|
|
|
|
*/
|
|
|
|
setup: function(canvas) {
|
|
|
|
// We need to set the global paper reference to this scope,
|
|
|
|
// since that will be used in the Project constructor to set
|
|
|
|
// internal references.
|
|
|
|
paper = this;
|
|
|
|
new Project();
|
|
|
|
if (canvas) {
|
|
|
|
// Activate the newly created view straight away
|
|
|
|
new View(canvas).activate();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-05-14 06:38:45 -04:00
|
|
|
/**
|
2011-07-19 18:51:50 -04:00
|
|
|
* Injects the paper scope into any other given scope. Can be used for
|
2011-05-14 06:38:45 -04:00
|
|
|
* examle to inject the currently active PaperScope into the window's global
|
2011-07-19 18:51:50 -04:00
|
|
|
* scope, to emulate PaperScript-style globally accessible Paper classes and
|
|
|
|
* objects:
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-19 18:51:50 -04:00
|
|
|
* @example
|
2011-05-14 06:38:45 -04:00
|
|
|
* 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-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-06-19 18:03:18 -04:00
|
|
|
_needsRedraw: function() {
|
|
|
|
// Make sure we're not looping through the view list each time...
|
|
|
|
if (!this._redrawNotified) {
|
|
|
|
for (var i = this.views.length - 1; i >= 0; i--)
|
|
|
|
this.views[i]._redrawNeeded = true;
|
|
|
|
this._redrawNotified = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-07-24 19:25:23 -04:00
|
|
|
statics: /** @lends PaperScope */{
|
2011-05-16 05:59:45 -04:00
|
|
|
_scopes: {},
|
2011-05-14 07:15:31 -04:00
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* Retrieves a PaperScope object with the given id or associated with
|
|
|
|
* the passed canvas element.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @param id
|
|
|
|
*/
|
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-06-21 16:49:36 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2011-07-24 19:25:23 -04:00
|
|
|
* Iterates over all active scopes and calls the passed iterator
|
|
|
|
* function for each of them.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-24 19:25:23 -04:00
|
|
|
* @param iter the iterator function.
|
2011-06-21 16:49:36 -04:00
|
|
|
*/
|
|
|
|
each: function(iter) {
|
|
|
|
Base.each(this._scopes, iter);
|
2011-05-14 07:15:31 -04:00
|
|
|
}
|
2011-05-14 06:38:45 -04:00
|
|
|
}
|
|
|
|
});
|