2011-03-06 19:50:44 -05:00
|
|
|
/*
|
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-03-04 08:34:31 -05:00
|
|
|
var Gradient = this.Gradient = Base.extend({
|
2011-03-04 20:26:12 -05:00
|
|
|
beans: true,
|
|
|
|
|
2011-04-26 07:39:48 -04:00
|
|
|
// TODO: should type here be called 'radial' and have it
|
2011-04-07 11:01:49 -04:00
|
|
|
// receive a boolean value?
|
|
|
|
initialize: function(stops, type) {
|
2011-05-03 03:55:01 -04:00
|
|
|
if (!stops) {
|
2011-04-07 11:01:49 -04:00
|
|
|
stops = [new GradientStop('white', 0),
|
|
|
|
new GradientStop('black', 1)];
|
|
|
|
}
|
|
|
|
this.setStops(stops);
|
|
|
|
this.type = type ? type : 'linear';
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
getStops: function() {
|
|
|
|
return this._stops;
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
setStops: function(stops) {
|
2011-02-20 21:32:39 -05:00
|
|
|
if (stops.length < 2)
|
2011-03-03 17:45:17 -05:00
|
|
|
throw new Error(
|
|
|
|
'Gradient stop list needs to contain at least two stops.');
|
2011-05-03 03:55:01 -04:00
|
|
|
if (!(stops[0] instanceof GradientStop)) {
|
2011-05-03 03:54:13 -04:00
|
|
|
for (var i = 0, l = stops.length; i < l; i++) {
|
2011-04-07 11:12:00 -04:00
|
|
|
var rampPoint;
|
2011-04-07 11:01:49 -04:00
|
|
|
var stop = stops[i];
|
2011-04-07 11:12:00 -04:00
|
|
|
// If it is an array, the second argument is the rampPoint:
|
2011-05-16 09:46:25 -04:00
|
|
|
if (Array.isArray(stop)) {
|
2011-04-07 11:12:00 -04:00
|
|
|
rampPoint = stop[1];
|
2011-04-07 11:01:49 -04:00
|
|
|
stop = stop[0];
|
|
|
|
} else {
|
|
|
|
// Otherwise stops is an array of colors, and we need to
|
|
|
|
// calculate the midPoint:
|
2011-04-07 11:12:00 -04:00
|
|
|
rampPoint = i / (l - 1);
|
2011-04-07 11:01:49 -04:00
|
|
|
}
|
2011-04-07 11:12:00 -04:00
|
|
|
stops[i] = new GradientStop(stop, rampPoint);
|
2011-04-07 11:01:49 -04:00
|
|
|
}
|
|
|
|
}
|
2011-02-19 16:50:37 -05:00
|
|
|
this._stops = stops;
|
2011-05-05 15:28:28 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
equals: function(gradient) {
|
|
|
|
if (this.stops.length == gradient.stops.length) {
|
|
|
|
for (var i = 0, l = this.stops.length; i < l; i++) {
|
|
|
|
if (!this.stops[i].equals(gradient.stops[i]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-02-19 16:50:37 -05:00
|
|
|
}
|
2011-03-03 11:32:55 -05:00
|
|
|
});
|