mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-08 05:42:07 -05:00
Simplify Point#setLength().
This commit is contained in:
parent
b0282b9bd4
commit
4b832e0ea9
1 changed files with 8 additions and 23 deletions
|
@ -149,33 +149,18 @@ var Point = this.Point = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setLength: function(length) {
|
setLength: function(length) {
|
||||||
|
// Whenever setting x/y, use #set() instead of direct assignment,
|
||||||
|
// so LinkedPoint does not report changes twice.
|
||||||
if (this.isZero()) {
|
if (this.isZero()) {
|
||||||
if (this._angle != null) {
|
var angle = this._angle || 0;
|
||||||
var a = this._angle;
|
this.set(Math.cos(angle) * length, Math.sin(angle) * length);
|
||||||
// Use #set() instead of direct assignment, so LinkedPoint
|
|
||||||
// can optimise
|
|
||||||
this.set(
|
|
||||||
Math.cos(a) * length,
|
|
||||||
Math.sin(a) * length
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// Assume angle = 0
|
|
||||||
this.x = length;
|
|
||||||
// y is already 0
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
var scale = length / this.getLength();
|
var scale = length / this.getLength();
|
||||||
if (scale == 0) {
|
// Force calculation of angle now, so it will be preserved even when
|
||||||
// Calculate angle now, so it will be preserved even when
|
|
||||||
// x and y are 0
|
// x and y are 0
|
||||||
|
if (scale == 0)
|
||||||
this.getAngle();
|
this.getAngle();
|
||||||
}
|
this.set(this.x * scale, this.y * scale);
|
||||||
// Use #set() instead of direct assignment, so LinkedPoint
|
|
||||||
// can optimise
|
|
||||||
this.set(
|
|
||||||
this.x * scale,
|
|
||||||
this.y * scale
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue