mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
Define Base.isPlainValue() and use it to implement more flexible #equals() for Color and Rectangle.
This commit is contained in:
parent
a163d890e6
commit
4a8469b740
4 changed files with 17 additions and 6 deletions
|
@ -201,11 +201,12 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
* @return {Boolean} {@true if the rectangles are equal}
|
||||
*/
|
||||
equals: function(rect) {
|
||||
return rect === this || rect && (this.x === rect.x && this.y === rect.y
|
||||
&& this.width === rect.width && this.height=== rect.height
|
||||
|| Array.isArray(rect) && this.x === rect[0]
|
||||
&& this.y === rect[1] && this.width === rect[2]
|
||||
&& this.height === rect[3]) || false;
|
||||
if (Base.isPlainValue(rect))
|
||||
rect = Rectangle.read(arguments);
|
||||
return rect === this
|
||||
|| rect && this.x === rect.x && this.y === rect.y
|
||||
&& this.width === rect.width && this.height=== rect.height
|
||||
|| false;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -259,6 +259,14 @@ Base.inject(/** @lends Base# */{
|
|||
return !!this.getNamed(list, name);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns true if obj is either a plain object or an array, as used by
|
||||
* many argument reading methods.
|
||||
*/
|
||||
isPlainValue: function(obj) {
|
||||
return this.isPlainObject(obj) || Array.isArray(obj);
|
||||
},
|
||||
|
||||
/**
|
||||
* Serializes the passed object into a format that can be passed to
|
||||
* JSON.stringify() for JSON serialization.
|
||||
|
|
|
@ -697,6 +697,8 @@ var Color = Base.extend(new function() {
|
|||
* @return {Boolean} {@true if the colors are the same}
|
||||
*/
|
||||
equals: function(color) {
|
||||
if (Base.isPlainValue(color))
|
||||
color = Color.read(arguments);
|
||||
return color && this._type === color._type
|
||||
&& this._alpha === color._alpha
|
||||
&& Base.equals(this._components, color._components);
|
||||
|
|
|
@ -18,5 +18,5 @@ test('PointText', function() {
|
|||
content: 'Hello World!'
|
||||
});
|
||||
equals(text.point, { x: 100, y: 100 });
|
||||
equals(text.fillColor, 'black', 'text.fillColor should be black by default');
|
||||
equals(text.fillColor, { red: 0, green: 0, blue: 0 }, 'text.fillColor should be black by default');
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue