mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -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({
|
var Gradient = this.Gradient = Base.extend({
|
||||||
beans: true,
|
beans: true,
|
||||||
|
|
||||||
initialize: function() {
|
// Todo: should type here be called 'radial' and have it
|
||||||
this.setStops([
|
// receive a boolean value?
|
||||||
new GradientStop('white', 0),
|
initialize: function(stops, type) {
|
||||||
new GradientStop('black', 1)]);
|
if(!stops) {
|
||||||
this.type = 'linear';
|
stops = [new GradientStop('white', 0),
|
||||||
|
new GradientStop('black', 1)];
|
||||||
|
}
|
||||||
|
this.setStops(stops);
|
||||||
|
this.type = type ? type : 'linear';
|
||||||
},
|
},
|
||||||
|
|
||||||
getStops: function() {
|
getStops: function() {
|
||||||
|
@ -32,6 +36,22 @@ var Gradient = this.Gradient = Base.extend({
|
||||||
if (stops.length < 2)
|
if (stops.length < 2)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Gradient stop list needs to contain at least two stops.');
|
'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;
|
this._stops = stops;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue