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 GradientColor = this.GradientColor = Color.extend({
|
2011-02-19 16:50:37 -05:00
|
|
|
beans: true,
|
|
|
|
|
|
|
|
initialize: function(gradient, origin, destination, hilite) {
|
|
|
|
this.gradient = gradient || new Gradient();
|
2011-03-04 20:26:12 -05:00
|
|
|
this.setOrigin(origin);
|
|
|
|
this.setDestination(destination);
|
2011-02-20 21:32:39 -05:00
|
|
|
if (hilite)
|
2011-03-04 20:26:12 -05:00
|
|
|
this.setHilite(hilite);
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
getOrigin: function() {
|
|
|
|
return this._origin;
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-03-08 12:17:36 -05:00
|
|
|
setOrigin: function(origin) {
|
2011-03-13 13:31:00 -04:00
|
|
|
origin = Point.read(arguments);
|
|
|
|
this._origin = origin;
|
|
|
|
if (this._destination)
|
|
|
|
this._radius = this._destination.getDistance(this._origin);
|
2011-03-08 12:17:36 -05:00
|
|
|
return this;
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
getDestination: function() {
|
|
|
|
return this._destination;
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-03-08 12:17:36 -05:00
|
|
|
setDestination: function(destination) {
|
2011-03-13 13:31:00 -04:00
|
|
|
destination = Point.read(arguments);
|
|
|
|
this._destination = destination;
|
|
|
|
this._radius = this._destination.getDistance(this._origin);
|
2011-03-08 12:17:36 -05:00
|
|
|
return this;
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
getHilite: function() {
|
|
|
|
return this._hilite;
|
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-03-08 12:17:36 -05:00
|
|
|
setHilite: function(hilite) {
|
2011-03-13 13:31:00 -04:00
|
|
|
hilite = Point.read(arguments);
|
|
|
|
var vector = hilite.subtract(this._origin);
|
|
|
|
if (vector.getLength() > this._radius) {
|
|
|
|
this._hilite = this._origin.add(vector.normalize(
|
|
|
|
this._radius - 0.1));
|
|
|
|
} else {
|
|
|
|
this._hilite = hilite;
|
2011-02-19 16:50:37 -05:00
|
|
|
}
|
2011-03-08 12:17:36 -05:00
|
|
|
return this;
|
2011-02-19 16:50:37 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
getCanvasStyle: function(ctx) {
|
|
|
|
var gradient;
|
2011-04-28 08:23:17 -04:00
|
|
|
if (this.gradient.type === 'linear') {
|
2011-03-04 20:26:12 -05:00
|
|
|
gradient = ctx.createLinearGradient(this._origin.x, this._origin.y,
|
2011-02-19 16:50:37 -05:00
|
|
|
this.destination.x, this.destination.y);
|
|
|
|
} else {
|
2011-03-04 20:26:12 -05:00
|
|
|
var origin = this._hilite || this._origin;
|
2011-02-19 16:50:37 -05:00
|
|
|
gradient = ctx.createRadialGradient(origin.x, origin.y,
|
2011-03-04 20:26:12 -05:00
|
|
|
0, this._origin.x, this._origin.y, this._radius);
|
2011-02-19 16:50:37 -05:00
|
|
|
}
|
2011-03-04 20:26:12 -05:00
|
|
|
for (var i = 0, l = this.gradient._stops.length; i < l; i++) {
|
|
|
|
var stop = this.gradient._stops[i];
|
|
|
|
gradient.addColorStop(stop._rampPoint, stop.color.toCssString());
|
2011-02-19 16:50:37 -05:00
|
|
|
}
|
|
|
|
return gradient;
|
|
|
|
}
|
2011-03-03 11:32:55 -05:00
|
|
|
});
|
2011-03-09 08:31:40 -05:00
|
|
|
|