mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
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:
parent
14010eb8b2
commit
06df8b1288
1 changed files with 25 additions and 5 deletions
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue