Improve PaperScope definition so it also works when using load.js, and seperate paper.js code into core/Base.js and core/PaperScope.js.

This commit is contained in:
Jürg Lehni 2011-05-14 13:38:45 +03:00
parent 9ef31616d5
commit a1efd85ecb
5 changed files with 166 additions and 122 deletions

107
src/core/Base.js Normal file
View file

@ -0,0 +1,107 @@
/*
* 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.
*/
// Extend Base with utility functions used across the library.
Base.inject({
statics: true,
read: function(list, start, length) {
var start = start || 0,
length = length || list.length - start;
var obj = list[start];
if (obj instanceof this
// If the class defines _readNull, return null when nothing
// was provided
|| this.prototype._readNull && obj == null && length <= 1)
return obj;
obj = new this(this.dont);
return obj.initialize.apply(obj, start > 0 || length < list.length
? Array.prototype.slice.call(list, start, start + length)
: list) || obj;
},
readAll: function(list, start) {
var res = [], entry;
for (var i = start || 0, l = list.length; i < l; i++) {
res.push(Array.isArray(entry = list[i])
? this.read(entry, 0)
: this.read(list, i, 1));
}
return res;
},
/**
* Utility function for adding and removing items from a list of which
* each entry keeps a reference to its index in the list in the private
* _index property. Used for paper.documents and Item#children.
*/
splice: function(list, items, index, remove) {
var amount = items && items.length,
append = index === undefined;
index = append ? list.length : index;
// Update _index on the items to be added first.
for (var i = 0; i < amount; i++) {
items[i]._index = index + i;
}
if (append) {
// Append them all at the end by using push
list.push.apply(list, items);
// Nothing removed, and nothing to adjust above
return [];
} else {
// Insert somewhere else and/or remove
var args = [index, remove];
if (items)
args.push.apply(args, items);
var removed = list.splice.apply(list, args);
// Adjust the indices of the items above.
for (var i = index + amount, l = list.length; i < l; i++) {
list[i]._index = i;
}
return removed;
}
},
capitalize: function(str) {
return str.replace(/\b[a-z]/g, function(match) {
return match.toUpperCase();
});
},
camelize: function(str) {
return str.replace(/-(\w)/g, function(all, chr) {
return chr.toUpperCase();
});
},
/**
* Utility function for rendering numbers to strings at a precision of up
* to 5 fractional digits.
*/
formatNumber: function(num) {
return (Math.round(num * 100000) / 100000).toString();
},
/**
* Utility function for rendering objects to strings, in object literal
* notation.
*/
formatObject: function(obj) {
return '{ ' + Base.each(obj, function(value, key) {
this.push(key + ': ' + value);
}, []).join(', ') + ' }';
}
});

42
src/core/PaperScope.js Normal file
View file

@ -0,0 +1,42 @@
/*
* 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({
initialize: function() {
this.document = null;
this.documents = [];
this.tools = [];
},
/**
* 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);
}
});

View file

@ -18,6 +18,8 @@ var Document = this.Document = Base.extend({
beans: true,
initialize: function(canvas) {
// Store reference to currently active global paper scope:
this._scope = paper;
if (canvas && canvas instanceof HTMLCanvasElement) {
this.canvas = canvas;
if (canvas.attributes.resize) {
@ -59,8 +61,8 @@ var Document = this.Document = Base.extend({
this.bounds = Rectangle.create(0, 0, this._size.width,
this._size.height);
this.context = this.canvas.getContext('2d');
// Push it onto paper.documents and set index:
this._index = paper.documents.push(this) - 1;
// Push it onto this._scope.documents and set index:
this._index = this._scope.documents.push(this) - 1;
this.activate();
this.layers = [];
this.activeLayer = new Layer();
@ -108,9 +110,9 @@ var Document = this.Document = Base.extend({
activate: function() {
// TODO: Remove indexOf()
var index = paper.documents.indexOf(this);
var index = this._scope.documents.indexOf(this);
if (index != -1) {
paper.document = this;
this._scope.document = this;
return true;
}
return false;

View file

@ -24,6 +24,9 @@ var sources = [
'lib/parse-js.js',
'lib/stats.js',
'src/core/Base.js',
'src/core/PaperScope.js',
'src/paper.js',
'src/basic/Point.js',

View file

@ -39,124 +39,12 @@ var paper = new function() {
// Inline Bootstrap core (the Base class) inside the paper scope first:
//#include "../lib/bootstrap.js"
/**
* 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 = Base.extend({
initialize: function() {
this.document = null;
this.documents = [];
this.tools = [];
},
//#include "core/Base.js"
//#include "core/PaperScope.js"
/**
* 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);
}
});
// Extend Base with utility functions used across the library.
Base.inject({
statics: true,
read: function(list, start, length) {
var start = start || 0,
length = length || list.length - start;
var obj = list[start];
if (obj instanceof this
// If the class defines _readNull, return null when nothing
// was provided
|| this.prototype._readNull && obj == null && length <= 1)
return obj;
obj = new this(this.dont);
return obj.initialize.apply(obj, start > 0 || length < list.length
? Array.prototype.slice.call(list, start, start + length)
: list) || obj;
},
readAll: function(list, start) {
var res = [], entry;
for (var i = start || 0, l = list.length; i < l; i++) {
res.push(Array.isArray(entry = list[i])
? this.read(entry, 0)
: this.read(list, i, 1));
}
return res;
},
/**
* Utility function for adding and removing items from a list of which
* each entry keeps a reference to its index in the list in the private
* _index property. Used for paper.documents and Item#children.
*/
splice: function(list, items, index, remove) {
var amount = items && items.length,
append = index === undefined;
index = append ? list.length : index;
// Update _index on the items to be added first.
for (var i = 0; i < amount; i++) {
items[i]._index = index + i;
}
if (append) {
// Append them all at the end by using push
list.push.apply(list, items);
// Nothing removed, and nothing to adjust above
return [];
} else {
// Insert somewhere else and/or remove
var args = [index, remove];
if (items)
args.push.apply(args, items);
var removed = list.splice.apply(list, args);
// Adjust the indices of the items above.
for (var i = index + amount, l = list.length; i < l; i++) {
list[i]._index = i;
}
return removed;
}
},
capitalize: function(str) {
return str.replace(/\b[a-z]/g, function(match) {
return match.toUpperCase();
});
},
camelize: function(str) {
return str.replace(/-(\w)/g, function(all, chr) {
return chr.toUpperCase();
});
},
/**
* Utility function for rendering numbers to strings at a precision of up
* to 5 fractional digits.
*/
formatNumber: function(num) {
return (Math.round(num * 100000) / 100000).toString();
},
/**
* Utility function for rendering objects to strings, in object literal
* notation.
*/
formatObject: function(obj) {
return '{ ' + Base.each(obj, function(value, key) {
this.push(key + ': ' + value);
}, []).join(', ') + ' }';
}
});
// Include Paper classes, which are later injected into PaperScope by setting
// them on the 'this' object, e.g.:
// var Point = this.Point = Base.extend(...);
//#include "basic/Point.js"
//#include "basic/Size.js"
@ -214,6 +102,8 @@ Base.inject({
//#include "util/PaperScript.js"
//#include "util/BlendMode.js"
// Now create the first PaperScope and return it.
// Finally inject the classes set on 'this' into the PaperScope class.
PaperScope.inject(this);
// Create the first PaperScope and return it.
return new PaperScope();
};