mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-22 23:39:59 -05:00
Change straps.js to directly use initialize() as constructor function rather than redirecting calls to it.
Should yield some interesting performance improvements.
This commit is contained in:
parent
094e35f2e5
commit
369b329b23
14 changed files with 24 additions and 21 deletions
|
@ -252,16 +252,19 @@ var Base = this.Base = new function() { // Straps scope
|
||||||
},
|
},
|
||||||
|
|
||||||
extend: function(src/* , ... */) {
|
extend: function(src/* , ... */) {
|
||||||
var ctor = function() {
|
var ctor,
|
||||||
// Call the constructor function, if defined
|
base = this;
|
||||||
if (this.initialize)
|
// Look for an initialize function in all injection scopes and use
|
||||||
return this.initialize.apply(this, arguments);
|
// it as the actual constructor.
|
||||||
|
for (var i = 0, l = arguments.length; i < l; i++)
|
||||||
|
if (ctor = arguments[i].initialize)
|
||||||
|
break;
|
||||||
|
// If no initialize function is provided, create one that simply
|
||||||
|
// calls the base constructor.
|
||||||
|
ctor = ctor || function() {
|
||||||
|
base.apply(this, arguments);
|
||||||
};
|
};
|
||||||
ctor.prototype = create(this.prototype);
|
ctor.prototype = create(this.prototype);
|
||||||
// Add a toString function that delegates to initialize if possible
|
|
||||||
ctor.toString = function() {
|
|
||||||
return (this.prototype.initialize || function() {}).toString();
|
|
||||||
};
|
|
||||||
// The new prototype extends the constructor on which extend is
|
// The new prototype extends the constructor on which extend is
|
||||||
// called. Fix constructor.
|
// called. Fix constructor.
|
||||||
define(ctor.prototype, 'constructor',
|
define(ctor.prototype, 'constructor',
|
||||||
|
|
|
@ -89,7 +89,7 @@ var Group = this.Group = Item.extend(/** @lends Group# */{
|
||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
initialize: function(arg) {
|
initialize: function(arg) {
|
||||||
this.base();
|
Item.call(this);
|
||||||
// Allow Group to have children and named children
|
// Allow Group to have children and named children
|
||||||
this._children = [];
|
this._children = [];
|
||||||
this._namedChildren = {};
|
this._namedChildren = {};
|
||||||
|
|
|
@ -61,7 +61,7 @@ var Layer = this.Layer = Group.extend(/** @lends Layer# */{
|
||||||
this._project = paper.project;
|
this._project = paper.project;
|
||||||
// Push it onto project.layers and set index:
|
// Push it onto project.layers and set index:
|
||||||
this._index = this._project.layers.push(this) - 1;
|
this._index = this._project.layers.push(this) - 1;
|
||||||
this.base.apply(this, arguments);
|
Group.apply(this, arguments);
|
||||||
this.activate();
|
this.activate();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend(/** @lends PlacedSymbol# */{
|
||||||
// Support two forms of item initialization: Passing one object literal
|
// Support two forms of item initialization: Passing one object literal
|
||||||
// describing all the different properties to be set, or a symbol (arg0)
|
// describing all the different properties to be set, or a symbol (arg0)
|
||||||
// and a point where it should be placed (arg1).
|
// and a point where it should be placed (arg1).
|
||||||
this.base(arg1 !== undefined && Point.read(arguments, 1));
|
Item.call(this, arg1 !== undefined && Point.read(arguments, 1));
|
||||||
// If we can handle setting properties through object literal, we're all
|
// If we can handle setting properties through object literal, we're all
|
||||||
// set. Otherwise we need to set symbol.
|
// set. Otherwise we need to set symbol.
|
||||||
if (arg0 && !this._set(arg0))
|
if (arg0 && !this._set(arg0))
|
||||||
|
|
|
@ -76,7 +76,7 @@ var Raster = this.Raster = Item.extend(/** @lends Raster# */{
|
||||||
// Support two forms of item initialization: Passing one object literal
|
// Support two forms of item initialization: Passing one object literal
|
||||||
// describing all the different properties to be set, or an image
|
// describing all the different properties to be set, or an image
|
||||||
// (object) and a point where it should be placed (point).
|
// (object) and a point where it should be placed (point).
|
||||||
this.base(position !== undefined && Point.read(arguments, 1));
|
Item.call(this, position !== undefined && Point.read(arguments, 1));
|
||||||
// If we can handle setting properties through object literal, we're all
|
// If we can handle setting properties through object literal, we're all
|
||||||
// set. Otherwise we need to check the type of object:
|
// set. Otherwise we need to check the type of object:
|
||||||
if (object && !this._set(object)) {
|
if (object && !this._set(object)) {
|
||||||
|
|
|
@ -22,7 +22,7 @@ var Shape = this.Shape = Item.extend(/** @lends Shape# */{
|
||||||
_applyMatrix: false,
|
_applyMatrix: false,
|
||||||
|
|
||||||
initialize: function(type, point, size) {
|
initialize: function(type, point, size) {
|
||||||
this.base(point);
|
Item.call(this, point);
|
||||||
this._type = type;
|
this._type = type;
|
||||||
this._size = size;
|
this._size = size;
|
||||||
},
|
},
|
||||||
|
|
|
@ -50,7 +50,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath#
|
||||||
* compoundPath.children[1].position.x += 5;
|
* compoundPath.children[1].position.x += 5;
|
||||||
*/
|
*/
|
||||||
initialize: function(arg) {
|
initialize: function(arg) {
|
||||||
this.base();
|
PathItem.call(this);
|
||||||
// CompoundPath has children and supports named children.
|
// CompoundPath has children and supports named children.
|
||||||
this._children = [];
|
this._children = [];
|
||||||
this._namedChildren = {};
|
this._namedChildren = {};
|
||||||
|
|
|
@ -70,7 +70,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
|
||||||
initialize: function(arg) {
|
initialize: function(arg) {
|
||||||
this._closed = false;
|
this._closed = false;
|
||||||
this._segments = [];
|
this._segments = [];
|
||||||
this.base();
|
Item.call(this);
|
||||||
// arg can either be an object literal describing properties to be set
|
// arg can either be an object literal describing properties to be set
|
||||||
// on the path, a list of segments to be set, or the first of multiple
|
// on the path, a list of segments to be set, or the first of multiple
|
||||||
// arguments describing separate segments.
|
// arguments describing separate segments.
|
||||||
|
|
|
@ -47,7 +47,7 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
|
||||||
initialize: function(view) {
|
initialize: function(view) {
|
||||||
// Activate straight away by passing true to base(), so paper.project is
|
// Activate straight away by passing true to base(), so paper.project is
|
||||||
// set, as required by Layer and DoumentView constructors.
|
// set, as required by Layer and DoumentView constructors.
|
||||||
this.base(true);
|
PaperScopeItem.call(this, true);
|
||||||
this.layers = [];
|
this.layers = [];
|
||||||
this.symbols = [];
|
this.symbols = [];
|
||||||
this._currentStyle = new Style();
|
this._currentStyle = new Style();
|
||||||
|
|
|
@ -38,7 +38,7 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
|
||||||
// might be a properties object literal for #setPropeties() at the end.
|
// might be a properties object literal for #setPropeties() at the end.
|
||||||
var hasProperties = arg && Base.isPlainObject(arg)
|
var hasProperties = arg && Base.isPlainObject(arg)
|
||||||
&& arg.x === undefined && arg.y === undefined;
|
&& arg.x === undefined && arg.y === undefined;
|
||||||
this.base(hasProperties ? null : Point.read(arguments));
|
Item.call(this, hasProperties ? null : Point.read(arguments));
|
||||||
this._content = '';
|
this._content = '';
|
||||||
this._lines = [];
|
this._lines = [];
|
||||||
if (hasProperties)
|
if (hasProperties)
|
||||||
|
|
|
@ -51,7 +51,7 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{
|
||||||
|
|
||||||
// DOCS: rewrite Tool constructor explanation
|
// DOCS: rewrite Tool constructor explanation
|
||||||
initialize: function(props) {
|
initialize: function(props) {
|
||||||
this.base();
|
PaperScopeItem.call(this);
|
||||||
this._firstMove = true;
|
this._firstMove = true;
|
||||||
this._count = 0;
|
this._count = 0;
|
||||||
this._downCount = 0;
|
this._downCount = 0;
|
||||||
|
|
|
@ -34,7 +34,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
this._context = canvas.getContext('2d');
|
this._context = canvas.getContext('2d');
|
||||||
// Have Item count installed mouse events.
|
// Have Item count installed mouse events.
|
||||||
this._eventCounters = {};
|
this._eventCounters = {};
|
||||||
this.base(canvas);
|
View.call(this, canvas);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
*/
|
*/
|
||||||
var KeyEvent = this.KeyEvent = Event.extend(/** @lends KeyEvent# */{
|
var KeyEvent = this.KeyEvent = Event.extend(/** @lends KeyEvent# */{
|
||||||
initialize: function(down, key, character, event) {
|
initialize: function(down, key, character, event) {
|
||||||
this.base(event);
|
Event.call(this, event);
|
||||||
this.type = down ? 'keydown' : 'keyup';
|
this.type = down ? 'keydown' : 'keyup';
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.character = character;
|
this.character = character;
|
||||||
|
|
|
@ -24,7 +24,7 @@
|
||||||
*/
|
*/
|
||||||
var MouseEvent = this.MouseEvent = Event.extend(/** @lends MouseEvent# */{
|
var MouseEvent = this.MouseEvent = Event.extend(/** @lends MouseEvent# */{
|
||||||
initialize: function(type, event, point, target, delta) {
|
initialize: function(type, event, point, target, delta) {
|
||||||
this.base(event);
|
Event.call(this, event);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.point = point;
|
this.point = point;
|
||||||
this.target = target;
|
this.target = target;
|
||||||
|
|
Loading…
Reference in a new issue