Simplify GradientColor code before merge with Color.

This commit is contained in:
Jürg Lehni 2013-04-08 23:29:15 -07:00
parent a03363a6d1
commit 380fce3946
2 changed files with 23 additions and 24 deletions

View file

@ -327,6 +327,7 @@ var Color = this.Color = Base.extend(new function() {
*/ */
_changed: function() { _changed: function() {
this._css = null; this._css = null;
this._canvasGradient = null;
if (this._owner) if (this._owner)
this._owner._changed(/*#=*/ Change.STYLE); this._owner._changed(/*#=*/ Change.STYLE);
}, },

View file

@ -160,10 +160,7 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
}, },
setOrigin: function(origin) { setOrigin: function(origin) {
origin = Point.read(arguments, 0, 0, true); // clone this._origin = Point.read(arguments, 0, 0, true); // clone
this._origin = origin;
if (this._destination)
this._radius = this._destination.getDistance(this._origin);
this._changed(); this._changed();
}, },
@ -202,9 +199,7 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
}, },
setDestination: function(destination) { setDestination: function(destination) {
destination = Point.read(arguments, 0, 0, true); // clone this._destination = Point.read(arguments, 0, 0, true); // clone
this._destination = destination;
this._radius = this._destination.getDistance(this._origin);
this._changed(); this._changed();
}, },
@ -241,33 +236,37 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
}, },
setHilite: function(hilite) { setHilite: function(hilite) {
hilite = Point.read(arguments, 0, 0, true); // clone this._hilite = Point.read(arguments, 0, 0, true); // clone
var vector = hilite.subtract(this._origin);
if (vector.getLength() > this._radius) {
this._hilite = this._origin.add(
vector.normalize(this._radius - 0.1));
} else {
this._hilite = hilite;
}
this._changed(); this._changed();
}, },
toCanvasStyle: function(ctx) { toCanvasStyle: function(ctx) {
var gradient, if (this._canvasGradient)
stops = this._gradient._stops; return this._canvasGradient;
var stops = this._gradient._stops,
origin = this._origin,
destination = this._destination,
gradient;
if (this._gradient._radial) { if (this._gradient._radial) {
var origin = this._hilite || this._origin; var radius = destination.getDistance(origin),
gradient = ctx.createRadialGradient(origin.x, origin.y, hilite = this._hilite;
0, this._origin.x, this._origin.y, this._radius); if (hilite) {
var vector = hilite.subtract(origin);
if (vector.getLength() > radius)
hilite = origin.add(vector.normalize(radius - 0.1));
}
var start = hilite || origin;
gradient = ctx.createRadialGradient(start.x, start.y,
0, origin.x, origin.y, radius);
} else { } else {
gradient = ctx.createLinearGradient(this._origin.x, this._origin.y, gradient = ctx.createLinearGradient(origin.x, origin.y,
this._destination.x, this._destination.y); destination.x, destination.y);
} }
for (var i = 0, l = stops.length; i < l; i++) { for (var i = 0, l = stops.length; i < l; i++) {
var stop = stops[i]; var stop = stops[i];
gradient.addColorStop(stop._rampPoint, stop._color.toCss()); gradient.addColorStop(stop._rampPoint, stop._color.toCss());
} }
return gradient; return this._canvasGradient = gradient;
}, },
/** /**
@ -294,7 +293,6 @@ var GradientColor = this.GradientColor = Color.extend(/** @lends GradientColor#
matrix._transformPoint(this._destination, this._destination, true); matrix._transformPoint(this._destination, this._destination, true);
if (this._hilite) if (this._hilite)
matrix._transformPoint(this._hilite, this._hilite, true); matrix._transformPoint(this._hilite, this._hilite, true);
this._radius = this._destination.getDistance(this._origin);
} }
}); });