Allow Gradient constructor to receive an array of colors or [color, midPoint]. Also allow the user to specify the type of the gradient in the constructor.

This commit is contained in:
Jonathan Puckey 2011-04-07 17:01:49 +02:00
parent 14010eb8b2
commit 06df8b1288

View file

@ -17,11 +17,15 @@
var Gradient = this.Gradient = Base.extend({
beans: true,
initialize: function() {
this.setStops([
new GradientStop('white', 0),
new GradientStop('black', 1)]);
this.type = 'linear';
// Todo: should type here be called 'radial' and have it
// receive a boolean value?
initialize: function(stops, type) {
if(!stops) {
stops = [new GradientStop('white', 0),
new GradientStop('black', 1)];
}
this.setStops(stops);
this.type = type ? type : 'linear';
},
getStops: function() {
@ -32,6 +36,22 @@ var Gradient = this.Gradient = Base.extend({
if (stops.length < 2)
throw new Error(
'Gradient stop list needs to contain at least two stops.');
if(!(stops[0] instanceof GradientStop)) {
for(var i = 0, l = stops.length; i < l; i++) {
var midPoint;
var stop = stops[i];
// If it is an array, the second argument is the midPoint:
if(stop.length) {
midPoint = stop[1];
stop = stop[0];
} else {
// Otherwise stops is an array of colors, and we need to
// calculate the midPoint:
midPoint = i / (l - 1);
}
stops[i] = new GradientStop(stop, midPoint);
}
}
this._stops = stops;
}
});