mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
Implement _changed() mechanism in Color, by having Colors know which items they are defining styles for, through an internal _owners list.
This commit is contained in:
parent
af0e5a07b7
commit
3a232b305e
3 changed files with 32 additions and 2 deletions
|
@ -270,7 +270,7 @@ var Color = this.Color = Base.extend(new function() {
|
|||
? ((value % 360) + 360) % 360
|
||||
// All other values are 0..1
|
||||
: Math.min(Math.max(value, 0), 1);
|
||||
this._cssString = null;
|
||||
this._changed();
|
||||
return this;
|
||||
};
|
||||
}, src);
|
||||
|
@ -306,6 +306,27 @@ var Color = this.Color = Base.extend(new function() {
|
|||
}, {
|
||||
/** @lends Color# */
|
||||
|
||||
_changed: function() {
|
||||
this._cssString = null;
|
||||
for (var i = 0, l = this._owners && this._owners.length; i < l; i++)
|
||||
this._owners[i]._changed(Change.STYLE);
|
||||
},
|
||||
|
||||
_addOwner: function(item) {
|
||||
if (!this._owners)
|
||||
this._owners = [];
|
||||
this._owners.push(item);
|
||||
},
|
||||
|
||||
_removeOwner: function(item) {
|
||||
var index = this._owners ? this._owners.indexOf(item) : -1;
|
||||
if (index != -1) {
|
||||
this._owners.splice(index, 1);
|
||||
if (this._owners.length == 0)
|
||||
delete this._owners;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the type of the color as a string.
|
||||
*
|
||||
|
@ -357,7 +378,7 @@ var Color = this.Color = Base.extend(new function() {
|
|||
|
||||
setAlpha: function(alpha) {
|
||||
this._alpha = alpha == null ? null : Math.min(Math.max(alpha, 0), 1);
|
||||
this._cssString = null;
|
||||
this._changed();
|
||||
return this;
|
||||
},
|
||||
|
||||
|
|
|
@ -136,6 +136,7 @@ var GradientColor = this.GradientColor = Color.extend({
|
|||
this._origin = origin;
|
||||
if (this._destination)
|
||||
this._radius = this._destination.getDistance(this._origin);
|
||||
this._changed();
|
||||
return this;
|
||||
},
|
||||
|
||||
|
@ -175,6 +176,7 @@ var GradientColor = this.GradientColor = Color.extend({
|
|||
destination = Point.read(arguments).clone();
|
||||
this._destination = destination;
|
||||
this._radius = this._destination.getDistance(this._origin);
|
||||
this._changed();
|
||||
return this;
|
||||
},
|
||||
|
||||
|
@ -218,6 +220,7 @@ var GradientColor = this.GradientColor = Color.extend({
|
|||
} else {
|
||||
this._hilite = hilite;
|
||||
}
|
||||
this._changed();
|
||||
return this;
|
||||
},
|
||||
|
||||
|
|
|
@ -104,6 +104,12 @@ var PathStyle = this.PathStyle = Base.extend(new function() {
|
|||
var old = this['_' + key];
|
||||
if (old != value && !(old && old.equals && old.equals(value))) {
|
||||
this['_' + key] = value;
|
||||
if (isColor) {
|
||||
if (old)
|
||||
old._removeOwner(this._item);
|
||||
if (value)
|
||||
value._addOwner(this._item);
|
||||
}
|
||||
if (this._item) {
|
||||
this._item._changed(Change.STYLE
|
||||
| (strokeFlags[key] ? Change.STROKE : 0));
|
||||
|
|
Loading…
Reference in a new issue