mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Add ObservedPoint class that records changes and passes them on to the owner, and use it for all Point objects returned from Rectangle.
This commit is contained in:
parent
4e65d2c1d3
commit
b1c0a48552
2 changed files with 76 additions and 13 deletions
|
@ -159,8 +159,12 @@ var Point = this.Point = Base.extend({
|
||||||
if (this.isZero()) {
|
if (this.isZero()) {
|
||||||
if (this._angle != null) {
|
if (this._angle != null) {
|
||||||
var a = this._angle;
|
var a = this._angle;
|
||||||
this.x = Math.cos(a) * length;
|
// Use #set() instead of direct assignment, so ObservedPoint
|
||||||
this.y = Math.sin(a) * length;
|
// can optimise
|
||||||
|
this.set(
|
||||||
|
Math.cos(a) * length,
|
||||||
|
Math.sin(a) * length
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// Assume angle = 0
|
// Assume angle = 0
|
||||||
this.x = length;
|
this.x = length;
|
||||||
|
@ -173,8 +177,12 @@ var Point = this.Point = Base.extend({
|
||||||
// x and y are 0
|
// x and y are 0
|
||||||
this.getAngle();
|
this.getAngle();
|
||||||
}
|
}
|
||||||
this.x *= scale;
|
// Use #set() instead of direct assignment, so ObservedPoint
|
||||||
this.y *= scale;
|
// can optimise
|
||||||
|
this.set(
|
||||||
|
this.x * scale,
|
||||||
|
this.y * scale
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
@ -229,8 +237,12 @@ var Point = this.Point = Base.extend({
|
||||||
angle = this._angle = angle * Math.PI / 180;
|
angle = this._angle = angle * Math.PI / 180;
|
||||||
if (!this.isZero()) {
|
if (!this.isZero()) {
|
||||||
var length = this.getLength();
|
var length = this.getLength();
|
||||||
this.x = Math.cos(angle) * length;
|
// Use #set() instead of direct assignment, so ObservedPoint
|
||||||
this.y = Math.sin(angle) * length;
|
// can optimise
|
||||||
|
this.set(
|
||||||
|
Math.cos(angle) * length,
|
||||||
|
Math.sin(angle) * length
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
@ -560,3 +572,52 @@ var Point = this.Point = Base.extend({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var ObservedPoint = Point.extend({
|
||||||
|
beans: true,
|
||||||
|
|
||||||
|
set: function(x, y) {
|
||||||
|
this._x = x;
|
||||||
|
this._y = y;
|
||||||
|
this._observer[this._set](this);
|
||||||
|
return this;
|
||||||
|
},
|
||||||
|
|
||||||
|
getX: function() {
|
||||||
|
return this._x;
|
||||||
|
},
|
||||||
|
|
||||||
|
setX: function(x) {
|
||||||
|
this._x = x;
|
||||||
|
this._observer[this._set](this);
|
||||||
|
},
|
||||||
|
|
||||||
|
getY: function() {
|
||||||
|
return this._y;
|
||||||
|
},
|
||||||
|
|
||||||
|
setY: function(y) {
|
||||||
|
this._y = y;
|
||||||
|
this._observer[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);
|
||||||
|
point._x = x;
|
||||||
|
point._y = y;
|
||||||
|
point._observer = observer;
|
||||||
|
point._set = set;
|
||||||
|
return point;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
@ -71,7 +71,7 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
getPoint: function() {
|
getPoint: function() {
|
||||||
return Point.create(this.x, this.y);
|
return ObservedPoint.create(this, 'setPoint', this.x, this.y);
|
||||||
},
|
},
|
||||||
|
|
||||||
setPoint: function(point) {
|
setPoint: function(point) {
|
||||||
|
@ -150,7 +150,8 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
getCenter: function() {
|
getCenter: function() {
|
||||||
return Point.create(this.getCenterX(), this.getCenterY());
|
return ObservedPoint.create(this, 'setCenter',
|
||||||
|
this.getCenterX(), this.getCenterY());
|
||||||
},
|
},
|
||||||
|
|
||||||
setCenter: function(point) {
|
setCenter: function(point) {
|
||||||
|
@ -242,10 +243,10 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
['Right', 'Center'], ['Bottom', 'Center']
|
['Right', 'Center'], ['Bottom', 'Center']
|
||||||
],
|
],
|
||||||
function(parts, index) {
|
function(parts, index) {
|
||||||
var key = parts.join('');
|
var part = parts.join('');
|
||||||
// find out if the first of the pair is an x or y property,
|
// find out if the first of the pair is an x or y property,
|
||||||
// by checking the first character for [R]ight or [L]eft;
|
// by checking the first character for [R]ight or [L]eft;
|
||||||
var xFirst = /^[RL]/.test(key);
|
var xFirst = /^[RL]/.test(part);
|
||||||
// Rename Center to CenterX or CenterY:
|
// Rename Center to CenterX or CenterY:
|
||||||
if (index >= 4)
|
if (index >= 4)
|
||||||
parts[1] += xFirst ? 'Y' : 'X';
|
parts[1] += xFirst ? 'Y' : 'X';
|
||||||
|
@ -255,10 +256,11 @@ var Rectangle = this.Rectangle = Base.extend({
|
||||||
getY = 'get' + y,
|
getY = 'get' + y,
|
||||||
setX = 'set' + x,
|
setX = 'set' + x,
|
||||||
setY = 'set' + y;
|
setY = 'set' + y;
|
||||||
this['get' + key] = function() {
|
this['get' + part] = function() {
|
||||||
return Point.create(this[getX](), this[getY]());
|
return ObservedPoint.create(this, 'set' + part,
|
||||||
|
this[getX](), this[getY]());
|
||||||
};
|
};
|
||||||
this['set' + key] = function(point) {
|
this['set' + part] = function(point) {
|
||||||
point = Point.read(arguments);
|
point = Point.read(arguments);
|
||||||
return this[setX](point.x)[setY](point.y); // Note: call chaining!
|
return this[setX](point.x)[setY](point.y); // Note: call chaining!
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue