mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Improve Base.read() to support cloning of objects that are already provided in the required type.
This commit is contained in:
parent
0a93465e94
commit
4d1920ee8b
2 changed files with 18 additions and 11 deletions
|
@ -133,8 +133,8 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
|
|||
},
|
||||
|
||||
setOrigin: function(origin) {
|
||||
// PORT: Add clone to Scriptographer
|
||||
origin = Point.read(arguments).clone();
|
||||
// PORT: Add origin cloning to Scriptographer
|
||||
origin = Point.read(arguments, 0, 0, true); // clone
|
||||
this._origin = origin;
|
||||
if (this._destination)
|
||||
this._radius = this._destination.getDistance(this._origin);
|
||||
|
@ -174,8 +174,8 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
|
|||
},
|
||||
|
||||
setDestination: function(destination) {
|
||||
// PORT: Add clone to Scriptographer
|
||||
destination = Point.read(arguments).clone();
|
||||
// PORT: Add destination cloning to Scriptographer
|
||||
destination = Point.read(arguments, 0, 0, true); // clone
|
||||
this._destination = destination;
|
||||
this._radius = this._destination.getDistance(this._origin);
|
||||
this._changed();
|
||||
|
@ -213,8 +213,8 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
|
|||
},
|
||||
|
||||
setHilite: function(hilite) {
|
||||
// PORT: Add clone to Scriptographer
|
||||
hilite = Point.read(arguments).clone();
|
||||
// PORT: Add hilite cloning to Scriptographer
|
||||
hilite = Point.read(arguments, 0, 0, true); // clone
|
||||
var vector = hilite.subtract(this._origin);
|
||||
if (vector.getLength() > this._radius) {
|
||||
this._hilite = this._origin.add(
|
||||
|
|
|
@ -57,8 +57,12 @@ this.Base = Base.inject(/** @lends Base# */{
|
|||
* the specified length. This is used in argument conversion, e.g. by
|
||||
* all basic types (Point, Size, Rectangle) and also higher classes such
|
||||
* as Color and Segment.
|
||||
* @param {Number} start the index at which to start reading in the list
|
||||
* @param {Number} length the amount of elements that can be read
|
||||
* @param {Boolean} clone controls wether passed objects should be
|
||||
* cloned if they are already provided in the required type
|
||||
*/
|
||||
read: function(list, start, length) {
|
||||
read: function(list, start, length, clone) {
|
||||
var start = start || 0,
|
||||
length = length || list.length - start;
|
||||
var obj = list[start];
|
||||
|
@ -66,7 +70,7 @@ this.Base = Base.inject(/** @lends Base# */{
|
|||
// If the class defines _readNull, return null when nothing
|
||||
// was provided
|
||||
|| this.prototype._readNull && obj == null && length <= 1)
|
||||
return obj;
|
||||
return obj && clone ? obj.clone() : obj;
|
||||
obj = new this(this.dont);
|
||||
return obj.initialize.apply(obj, start > 0 || length < list.length
|
||||
? Array.prototype.slice.call(list, start, start + length)
|
||||
|
@ -76,13 +80,16 @@ this.Base = Base.inject(/** @lends Base# */{
|
|||
/**
|
||||
* Reads all readable arguments from the list, handling nested arrays
|
||||
* seperately.
|
||||
* @param {Number} start the index at which to start reading in the list
|
||||
* @param {Boolean} clone controls wether passed objects should be
|
||||
* cloned if they are already provided in the required type
|
||||
*/
|
||||
readAll: function(list, start) {
|
||||
readAll: function(list, start, clone) {
|
||||
var res = [], entry;
|
||||
for (var i = start || 0, l = list.length; i < l; i++) {
|
||||
res.push(Array.isArray(entry = list[i])
|
||||
? this.read(entry, 0)
|
||||
: this.read(list, i, 1));
|
||||
? this.read(entry, 0, 0, clone) // 0 for length = max
|
||||
: this.read(list, i, 1, clone));
|
||||
}
|
||||
return res;
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue