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-05-26 07:04:47 -04:00
|
|
|
// TODO: Support midPoint? (initial tests didn't look nice)
|
2011-06-22 18:56:05 -04:00
|
|
|
/**
|
|
|
|
* @name GradientStop
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-22 18:56:05 -04:00
|
|
|
* @class The GradientStop object.
|
|
|
|
*/
|
|
|
|
var GradientStop = this.GradientStop = Base.extend(/** @lends GradientStop# */{
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* Creates a GradientStop object.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-11-10 13:16:34 -05:00
|
|
|
* @param {Color} [color=new RgbColor(0, 0, 0)] the color of the stop
|
2011-05-27 15:21:49 -04:00
|
|
|
* @param {Number} [rampPoint=0] the position of the stop on the gradient
|
2011-05-25 18:55:44 -04:00
|
|
|
* ramp {@default 0}
|
|
|
|
*/
|
2011-05-19 15:51:09 -04:00
|
|
|
initialize: function(arg0, arg1) {
|
2012-11-08 08:29:48 -05:00
|
|
|
if (arg0) {
|
|
|
|
if (arg1 === undefined && Array.isArray(arg0)) {
|
|
|
|
// [color, rampPoint]
|
|
|
|
this.setColor(arg0[0]);
|
|
|
|
this.setRampPoint(arg0[1]);
|
|
|
|
} else if (arg0 && arg0.color) {
|
|
|
|
// stop
|
|
|
|
this.setColor(arg0.color);
|
|
|
|
this.setRampPoint(arg0.rampPoint);
|
|
|
|
} else {
|
|
|
|
// color [, rampPoint]
|
|
|
|
this.setColor(arg0);
|
|
|
|
this.setRampPoint(arg1);
|
|
|
|
}
|
2011-05-19 15:51:09 -04:00
|
|
|
}
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2012-03-04 13:14:13 -05:00
|
|
|
// TODO: Do we really need to also clone the color here?
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* @return {GradientColor} a copy of the gradient-stop
|
|
|
|
*/
|
2011-05-19 17:02:26 -04:00
|
|
|
clone: function() {
|
|
|
|
return new GradientStop(this._color.clone(), this._rampPoint);
|
|
|
|
},
|
|
|
|
|
2012-03-04 13:14:13 -05:00
|
|
|
/**
|
|
|
|
* Called by various setters whenever a value changes
|
|
|
|
*/
|
|
|
|
_changed: function() {
|
|
|
|
// Loop through the gradients that use this stop and notify them about
|
|
|
|
// the change, so they can notify their gradient colors, which in turn
|
|
|
|
// will notify the items they are used in:
|
2012-10-10 22:27:14 -04:00
|
|
|
if (this._owner)
|
2012-11-05 21:11:44 -05:00
|
|
|
this._owner._changed(/*#=*/ Change.STYLE);
|
2012-03-04 13:14:13 -05:00
|
|
|
},
|
|
|
|
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
2011-05-31 08:28:42 -04:00
|
|
|
* The ramp-point of the gradient stop as a value between {@code 0} and
|
|
|
|
* {@code 1}.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-27 14:15:15 -04:00
|
|
|
* @type Number
|
2011-05-25 18:55:44 -04:00
|
|
|
* @bean
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* @example {@paperscript height=300}
|
|
|
|
* // Animating a gradient's ramp points:
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* // Create a circle shaped path at the center of the view,
|
|
|
|
* // using 40% of the height of the view as its radius
|
|
|
|
* // and fill it with a radial gradient color:
|
|
|
|
* var path = new Path.Circle(view.center, view.bounds.height * 0.4);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* // Prepare the gradient color and apply it to the path:
|
|
|
|
* var colors = [['yellow', 0.05], ['red', 0.2], ['black', 1]];
|
|
|
|
* var gradient = new Gradient(colors, 'radial');
|
|
|
|
* var from = path.position;
|
|
|
|
* var to = path.bounds.rightCenter;
|
|
|
|
* var gradientColor = new GradientColor(gradient, from, to);
|
|
|
|
* path.fillColor = gradientColor;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* // This function is called each frame of the animation:
|
|
|
|
* function onFrame(event) {
|
|
|
|
* var blackStop = gradient.stops[2];
|
|
|
|
* // Animate the rampPoint between 0.7 and 0.9:
|
|
|
|
* blackStop.rampPoint = Math.sin(event.time * 5) * 0.1 + 0.8;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* // Animate the rampPoint between 0.2 and 0.4
|
|
|
|
* var redStop = gradient.stops[1];
|
|
|
|
* redStop.rampPoint = Math.sin(event.time * 3) * 0.1 + 0.3;
|
|
|
|
* }
|
2011-05-25 18:55:44 -04:00
|
|
|
*/
|
2011-02-19 16:50:37 -05:00
|
|
|
getRampPoint: function() {
|
|
|
|
return this._rampPoint;
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
setRampPoint: function(rampPoint) {
|
2011-05-19 15:51:09 -04:00
|
|
|
this._defaultRamp = rampPoint == null;
|
2011-05-18 04:38:20 -04:00
|
|
|
this._rampPoint = rampPoint || 0;
|
2012-03-04 13:14:13 -05:00
|
|
|
this._changed();
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-05-25 18:55:44 -04:00
|
|
|
/**
|
|
|
|
* The color of the gradient stop.
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-05-25 18:55:44 -04:00
|
|
|
* @type Color
|
|
|
|
* @bean
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* @example {@paperscript height=300}
|
|
|
|
* // Animating a gradient's ramp points:
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* // Create a circle shaped path at the center of the view,
|
|
|
|
* // using 40% of the height of the view as its radius
|
|
|
|
* // and fill it with a radial gradient color:
|
|
|
|
* var path = new Path.Circle(view.center, view.bounds.height * 0.4);
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* // Create a radial gradient that mixes red and black evenly:
|
|
|
|
* var gradient = new Gradient(['red', 'black'], 'radial');
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* // Fill the path with a gradient color that runs from its center,
|
|
|
|
* // to the right center of its bounding rectangle:
|
|
|
|
* var from = path.position;
|
|
|
|
* var to = path.bounds.rightCenter;
|
|
|
|
* var gradientColor = new GradientColor(gradient, from, to);
|
|
|
|
* path.fillColor = gradientColor;
|
2011-06-30 06:01:51 -04:00
|
|
|
*
|
2011-06-09 17:21:06 -04:00
|
|
|
* // This function is called each frame of the animation:
|
|
|
|
* function onFrame(event) {
|
|
|
|
* // Change the hue of the first stop's color:
|
|
|
|
* gradient.stops[0].color.hue += 1;
|
|
|
|
* }
|
2011-05-25 18:55:44 -04:00
|
|
|
*/
|
2011-02-19 16:50:37 -05:00
|
|
|
getColor: function() {
|
|
|
|
return this._color;
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-03-08 12:17:36 -05:00
|
|
|
setColor: function(color) {
|
2012-10-10 22:27:14 -04:00
|
|
|
// Make sure newly set colors are cloned, since they can only have
|
|
|
|
// one owner.
|
2011-02-19 16:50:37 -05:00
|
|
|
this._color = Color.read(arguments);
|
2012-10-10 22:27:14 -04:00
|
|
|
if (this._color === color)
|
|
|
|
this._color = color.clone();
|
|
|
|
this._color._owner = this;
|
2012-03-04 13:14:13 -05:00
|
|
|
this._changed();
|
2011-05-05 15:28:28 -04:00
|
|
|
},
|
2011-05-18 04:40:03 -04:00
|
|
|
|
2011-05-05 15:28:28 -04:00
|
|
|
equals: function(stop) {
|
2011-05-18 04:40:03 -04:00
|
|
|
return stop == this || stop instanceof GradientStop
|
|
|
|
&& this._color.equals(stop._color)
|
2011-05-18 05:04:46 -04:00
|
|
|
&& this._rampPoint == stop._rampPoint;
|
2011-02-19 16:50:37 -05:00
|
|
|
}
|
2011-03-03 11:32:55 -05:00
|
|
|
});
|