2011-03-06 19:50:44 -05:00
|
|
|
/*
|
|
|
|
* Paper.js
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* 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-06-30 06:01:51 -04:00
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-07-01 06:17:45 -04:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name Gradient
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @class The Gradient object.
|
|
|
|
*/
|
|
|
|
var Gradient = this.Gradient = Base.extend(/** @lends Gradient# */{
|
2011-06-01 05:49:43 -04:00
|
|
|
// TODO: Should type here be called 'radial' and have it receive a
|
|
|
|
// boolean value?
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* Creates a gradient object
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:55:44 -04:00
|
|
|
* @param {GradientStop[]} stops
|
2011-05-27 15:21:49 -04:00
|
|
|
* @param {String} [type='linear'] 'linear' or 'radial'
|
2011-05-25 18:55:44 -04:00
|
|
|
*/
|
2011-04-07 11:01:49 -04:00
|
|
|
initialize: function(stops, type) {
|
2011-05-19 15:54:52 -04:00
|
|
|
this.setStops(stops || ['white', 'black']);
|
|
|
|
this.type = type || 'linear';
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2012-03-04 13:14:13 -05:00
|
|
|
/**
|
|
|
|
* Called by various setters whenever a gradient value changes
|
|
|
|
*/
|
|
|
|
_changed: function() {
|
|
|
|
// Loop through the gradient-colors that use this gradient and notify
|
|
|
|
// them, so they can notify the items they belong to.
|
|
|
|
for (var i = 0, l = this._owners && this._owners.length; i < l; i++)
|
|
|
|
this._owners[i]._changed();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by GradientColor#initialize
|
|
|
|
* This is required to pass on _changed() notifications to the _owners.
|
|
|
|
*/
|
|
|
|
_addOwner: function(color) {
|
|
|
|
if (!this._owners)
|
|
|
|
this._owners = [];
|
|
|
|
this._owners.push(color);
|
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: Where and when should this be called:
|
|
|
|
/**
|
|
|
|
* Called by GradientColor whenever this gradient stops being used.
|
|
|
|
*/
|
|
|
|
_removeOwner: function(color) {
|
|
|
|
var index = this._owners ? this._owners.indexOf(color) : -1;
|
|
|
|
if (index != -1) {
|
|
|
|
this._owners.splice(index, 1);
|
|
|
|
if (this._owners.length == 0)
|
|
|
|
delete this._owners;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* @return {Gradient} a copy of the gradient
|
|
|
|
*/
|
2011-05-19 17:02:26 -04:00
|
|
|
clone: function() {
|
|
|
|
var stops = [];
|
|
|
|
for (var i = 0, l = this._stops.length; i < l; i++)
|
|
|
|
stops[i] = this._stops[i].clone();
|
|
|
|
return new Gradient(stops, this.type);
|
|
|
|
},
|
|
|
|
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* The gradient stops on the gradient ramp.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-26 14:09:25 -04:00
|
|
|
* @type GradientStop[]
|
2011-05-25 18:55:44 -04:00
|
|
|
* @bean
|
|
|
|
*/
|
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) {
|
2012-03-04 13:14:13 -05:00
|
|
|
// If this gradient already contains stops, first remove
|
|
|
|
// this gradient as their owner.
|
|
|
|
if (this.stops) {
|
2012-10-10 22:27:14 -04:00
|
|
|
for (var i = 0, l = this._stops.length; i < l; i++)
|
|
|
|
delete this._stops[i]._owner;
|
2012-03-04 13:14:13 -05:00
|
|
|
}
|
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.');
|
2012-10-10 22:27:14 -04:00
|
|
|
this._stops = GradientStop.readAll(stops, 0, true); // clone
|
2011-05-19 15:51:09 -04:00
|
|
|
// Now reassign ramp points if they were not specified.
|
|
|
|
for (var i = 0, l = this._stops.length; i < l; i++) {
|
|
|
|
var stop = this._stops[i];
|
2012-10-10 22:27:14 -04:00
|
|
|
stop._owner = this;
|
2011-05-19 15:51:09 -04:00
|
|
|
if (stop._defaultRamp)
|
|
|
|
stop.setRampPoint(i / (l - 1));
|
2011-04-07 11:01:49 -04:00
|
|
|
}
|
2012-03-04 13:14:13 -05:00
|
|
|
this._changed();
|
2012-12-31 14:07:28 -05:00
|
|
|
return this;
|
2011-05-05 15:28:28 -04:00
|
|
|
},
|
2011-05-19 15:51:09 -04:00
|
|
|
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* Checks whether the gradient is equal to the supplied gradient.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:55:44 -04:00
|
|
|
* @param {Gradient} gradient
|
2011-05-30 13:42:17 -04:00
|
|
|
* @return {Boolean} {@true they are equal}
|
2011-05-25 18:55:44 -04:00
|
|
|
*/
|
2011-05-05 15:28:28 -04:00
|
|
|
equals: function(gradient) {
|
2011-05-21 06:06:37 -04:00
|
|
|
if (gradient.type != this.type)
|
|
|
|
return false;
|
2011-05-19 17:00:03 -04:00
|
|
|
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]))
|
2011-05-05 15:28:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2011-02-19 16:50:37 -05:00
|
|
|
}
|
2011-03-03 11:32:55 -05:00
|
|
|
});
|