mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Rename ObservedRectangle to LinkedRectangle, ObservedPoint to LinkedPoint, and add more comments about what it is they are doing.
This commit is contained in:
parent
dbb947b7aa
commit
b5fdecf3d1
6 changed files with 38 additions and 39 deletions
|
@ -159,7 +159,7 @@ var Point = this.Point = Base.extend({
|
|||
if (this.isZero()) {
|
||||
if (this._angle != null) {
|
||||
var a = this._angle;
|
||||
// Use #set() instead of direct assignment, so ObservedPoint
|
||||
// Use #set() instead of direct assignment, so LinkedPoint
|
||||
// can optimise
|
||||
this.set(
|
||||
Math.cos(a) * length,
|
||||
|
@ -177,7 +177,7 @@ var Point = this.Point = Base.extend({
|
|||
// x and y are 0
|
||||
this.getAngle();
|
||||
}
|
||||
// Use #set() instead of direct assignment, so ObservedPoint
|
||||
// Use #set() instead of direct assignment, so LinkedPoint
|
||||
// can optimise
|
||||
this.set(
|
||||
this.x * scale,
|
||||
|
@ -237,7 +237,7 @@ var Point = this.Point = Base.extend({
|
|||
angle = this._angle = angle * Math.PI / 180;
|
||||
if (!this.isZero()) {
|
||||
var length = this.getLength();
|
||||
// Use #set() instead of direct assignment, so ObservedPoint
|
||||
// Use #set() instead of direct assignment, so LinkedPoint
|
||||
// can optimise
|
||||
this.set(
|
||||
Math.cos(angle) * length,
|
||||
|
@ -573,13 +573,19 @@ var Point = this.Point = Base.extend({
|
|||
}
|
||||
});
|
||||
|
||||
var ObservedPoint = Point.extend({
|
||||
/**
|
||||
* An internal version of Point that notifies its owner of each change through
|
||||
* setting itself again on the setter that corresponds to the getter that
|
||||
* produced this LinkedPoint. See uses of LinkedPoint.create()
|
||||
* Note: This prototype is not exported.
|
||||
*/
|
||||
var LinkedPoint = Point.extend({
|
||||
beans: true,
|
||||
|
||||
set: function(x, y) {
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._observer[this._set](this);
|
||||
this._owner[this._set](this);
|
||||
return this;
|
||||
},
|
||||
|
||||
|
@ -589,7 +595,7 @@ var ObservedPoint = Point.extend({
|
|||
|
||||
setX: function(x) {
|
||||
this._x = x;
|
||||
this._observer[this._set](this);
|
||||
this._owner[this._set](this);
|
||||
},
|
||||
|
||||
getY: function() {
|
||||
|
@ -598,24 +604,15 @@ var ObservedPoint = Point.extend({
|
|||
|
||||
setY: function(y) {
|
||||
this._y = y;
|
||||
this._observer[this._set](this);
|
||||
this._owner[this._set](this);
|
||||
},
|
||||
|
||||
statics: {
|
||||
/**
|
||||
* Provide a faster creator for Points out of two coordinates that
|
||||
* does not rely on Point#initialize at all. This speeds up all math
|
||||
* operations a lot.
|
||||
*/
|
||||
create: function(observer, set, x, y) {
|
||||
// Don't use the shorter form as we want absolute maximum
|
||||
// performance here:
|
||||
// return new Point(Point.dont).set(x, y);
|
||||
// TODO: Benchmark and decide
|
||||
var point = new ObservedPoint(ObservedPoint.dont);
|
||||
create: function(owner, set, x, y) {
|
||||
var point = new LinkedPoint(LinkedPoint.dont);
|
||||
point._x = x;
|
||||
point._y = y;
|
||||
point._observer = observer;
|
||||
point._owner = owner;
|
||||
point._set = set;
|
||||
return point;
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
|||
},
|
||||
|
||||
getPoint: function() {
|
||||
return ObservedPoint.create(this, 'setPoint', this.x, this.y);
|
||||
return LinkedPoint.create(this, 'setPoint', this.x, this.y);
|
||||
},
|
||||
|
||||
setPoint: function(point) {
|
||||
|
@ -149,7 +149,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
|||
},
|
||||
|
||||
getCenter: function() {
|
||||
return ObservedPoint.create(this, 'setCenter',
|
||||
return LinkedPoint.create(this, 'setCenter',
|
||||
this.getCenterX(), this.getCenterY());
|
||||
},
|
||||
|
||||
|
@ -258,7 +258,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
|||
get = 'get' + part,
|
||||
set = 'set' + part;
|
||||
this[get] = function() {
|
||||
return ObservedPoint.create(this, set,
|
||||
return LinkedPoint.create(this, set,
|
||||
this[getX](), this[getY]());
|
||||
};
|
||||
this[set] = function(point) {
|
||||
|
@ -269,7 +269,13 @@ var Rectangle = this.Rectangle = Base.extend({
|
|||
}, { beans: true });
|
||||
});
|
||||
|
||||
var ObservedRectangle = Rectangle.extend({
|
||||
/**
|
||||
* An internal version of Rectangle that notifies its owner of each change
|
||||
* through setting itself again on the setter that corresponds to the getter
|
||||
* that produced this LinkedRectangle. See uses of LinkedRectangle.create()
|
||||
* Note: This prototype is not exported.
|
||||
*/
|
||||
var LinkedRectangle = Rectangle.extend({
|
||||
beans: true,
|
||||
|
||||
set: function(x, y, width, height) {
|
||||
|
@ -277,8 +283,8 @@ var ObservedRectangle = Rectangle.extend({
|
|||
this._y = y;
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
if (this._observer)
|
||||
this._observer[this._set](this);
|
||||
if (this._owner)
|
||||
this._owner[this._set](this);
|
||||
return this;
|
||||
},
|
||||
|
||||
|
@ -288,14 +294,10 @@ var ObservedRectangle = Rectangle.extend({
|
|||
* does not rely on Point#initialize at all. This speeds up all math
|
||||
* operations a lot.
|
||||
*/
|
||||
create: function(observer, set, x, y, width, height) {
|
||||
// Don't use the shorter form as we want absolute maximum
|
||||
// performance here:
|
||||
// return new Point(Point.dont).set(x, y);
|
||||
// TODO: Benchmark and decide
|
||||
var rect = new ObservedRectangle(ObservedRectangle.dont).set(x, y,
|
||||
create: function(owner, set, x, y, width, height) {
|
||||
var rect = new LinkedRectangle(LinkedRectangle.dont).set(x, y,
|
||||
width, height);
|
||||
rect._observer = observer;
|
||||
rect._owner = owner;
|
||||
rect._set = set;
|
||||
return rect;
|
||||
}
|
||||
|
@ -315,7 +317,7 @@ var ObservedRectangle = Rectangle.extend({
|
|||
// Check if this setter is called from another one which sets
|
||||
// _dontNotify, as it will notify itself
|
||||
if (!this._dontNotify)
|
||||
this._observer[this._set](this);
|
||||
this._owner[this._set](this);
|
||||
}
|
||||
}, Base.each(['Point', 'Size', 'Center',
|
||||
'Left', 'Top', 'Right', 'Bottom', 'CenterX', 'CenterY',
|
||||
|
@ -325,12 +327,12 @@ var ObservedRectangle = Rectangle.extend({
|
|||
var name = 'set' + key;
|
||||
this[name] = function(value) {
|
||||
// Make sure the above setters of x, y, width, height do not
|
||||
// each notify the observer, as we're going to take care of this
|
||||
// each notify the owner, as we're going to take care of this
|
||||
// afterwards here, only once per change.
|
||||
this._dontNotify = true;
|
||||
proto[name].apply(this, arguments);
|
||||
delete this._dontNotify;
|
||||
this._observer[this._set](this);
|
||||
this._owner[this._set](this);
|
||||
return this;
|
||||
}
|
||||
}, { beans: true })
|
||||
|
|
|
@ -43,7 +43,7 @@ var Group = this.Group = Item.extend({
|
|||
y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1);
|
||||
}
|
||||
}
|
||||
return ObservedRectangle.create(this, 'setBounds',
|
||||
return LinkedRectangle.create(this, 'setBounds',
|
||||
x1, y1, x2 - x1, y2 - y1);
|
||||
},
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend({
|
|||
|
||||
getBounds: function() {
|
||||
var bounds = this.symbol._definition.getStrokeBounds(this.matrix, true);
|
||||
return ObservedRectangle.create(this, 'setBounds',
|
||||
return LinkedRectangle.create(this, 'setBounds',
|
||||
bounds.x, bounds.y, bounds.width, bounds.height);
|
||||
},
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
|
|||
y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1);
|
||||
}
|
||||
}
|
||||
return ObservedRectangle.create(this, 'setBounds',
|
||||
return LinkedRectangle.create(this, 'setBounds',
|
||||
x1, y1, x2 - x1, y2 - y1);
|
||||
},
|
||||
|
||||
|
|
|
@ -701,7 +701,7 @@ var Path = this.Path = PathItem.extend({
|
|||
// Pass the matrix hidden from Bootstrap, so it still inject
|
||||
// getBounds as bean too.
|
||||
var bounds = getBounds(this, arguments[0]);
|
||||
return ObservedRectangle.create(this, 'setBounds',
|
||||
return LinkedRectangle.create(this, 'setBounds',
|
||||
bounds.x, bounds.y, bounds.width, bounds.height);
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in a new issue