paper.js/src/path/SegmentPoint.js

95 lines
2.1 KiB
JavaScript
Raw Normal View History

2011-05-08 04:59:37 -04:00
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
2011-05-08 04:59:37 -04:00
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
2011-05-08 04:59:37 -04:00
* http://lehni.org/ & http://jonathanpuckey.com/
*
2011-07-01 06:17:45 -04:00
* Distributed under the MIT license. See LICENSE file for details.
*
2011-05-08 04:59:37 -04:00
* All rights reserved.
*/
2011-04-21 08:21:56 -04:00
/**
* @name SegmentPoint
* @class An internal version of Point that notifies its segment of each change
2011-04-21 08:21:56 -04:00
* Note: This prototype is not exported.
*
* @private
2011-04-21 08:21:56 -04:00
*/
var SegmentPoint = Point.extend({
initialize: function SegmentPoint(point, owner, key) {
var x, y, selected;
if (!point) {
x = y = 0;
} else if ((x = point[0]) !== undefined) { // Array-like
y = point[1];
} else {
// If not Point-like already, read Point from arguments
if ((x = point.x) === undefined) {
point = Point.read(arguments);
x = point.x;
}
y = point.y;
selected = point.selected;
}
this._x = x;
this._y = y;
this._owner = owner;
// We have to set the owner's property that points to this point already
// now, so #setSelected(true) can work.
owner[key] = this;
if (selected)
this.setSelected(true);
},
2011-04-21 08:21:56 -04:00
set: function(x, y) {
this._x = x;
this._y = y;
this._owner._changed(this);
2011-04-21 08:21:56 -04:00
return this;
},
2013-10-17 06:04:11 -04:00
_serialize: function(options) {
var f = options.formatter,
x = f.number(this._x),
y = f.number(this._y);
return this.isSelected()
? { x: x, y: y, selected: true }
: [x, y];
2013-10-17 06:04:11 -04:00
},
2011-04-21 08:21:56 -04:00
getX: function() {
return this._x;
},
setX: function(x) {
this._x = x;
this._owner._changed(this);
2011-04-21 08:21:56 -04:00
},
getY: function() {
return this._y;
},
setY: function(y) {
this._y = y;
this._owner._changed(this);
2011-04-21 08:21:56 -04:00
},
isZero: function() {
// Provide our own version of Point#isZero() that does not use the x / y
// accessors but the internal properties directly, for performance
// reasons, since it is used a lot internally.
return Numerical.isZero(this._x) && Numerical.isZero(this._y);
},
2011-04-21 12:06:06 -04:00
setSelected: function(selected) {
this._owner._setSelected(this, selected);
2011-04-21 12:06:06 -04:00
},
isSelected: function() {
return this._owner._isSelected(this);
2011-04-21 08:21:56 -04:00
}
});