paper.js/src/color/GradientColor.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

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.
* http://scriptographer.org/
*
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* All rights reserved. See LICENSE file for details.
*/
var GradientColor = this.GradientColor = Color.extend({
beans: true,
initialize: function(gradient, origin, destination, hilite) {
this.gradient = gradient || new Gradient();
this.setOrigin(origin);
this.setDestination(destination);
2011-02-20 21:32:39 -05:00
if (hilite)
this.setHilite(hilite);
},
getOrigin: function() {
return this._origin;
},
setOrigin: function() {
this._origin = Point.read(arguments);
2011-02-20 21:32:39 -05:00
if (this._destination)
this._radius = this._destination.getDistance(this._origin);
},
getDestination: function() {
return this._destination;
},
setDestination: function() {
this._destination = Point.read(arguments);
this._radius = this._destination.getDistance(this._origin);
},
getHilite: function() {
return this._hilite;
},
setHilite: function() {
var hilite = Point.read(arguments);
var vector = hilite.subtract(this._origin);
2011-03-04 21:40:38 -05:00
if (vector.getLength() > this._radius) {
this._hilite = this._origin.add(vector.normalize(
this._radius - 0.1));
} else {
this._hilite = hilite;
}
},
getCanvasStyle: function(ctx) {
var gradient;
2011-02-20 21:32:39 -05:00
if (this.gradient.type == 'linear') {
gradient = ctx.createLinearGradient(this._origin.x, this._origin.y,
this.destination.x, this.destination.y);
} else {
var origin = this._hilite || this._origin;
gradient = ctx.createRadialGradient(origin.x, origin.y,
0, this._origin.x, this._origin.y, this._radius);
}
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());
}
return gradient;
}
2011-03-03 11:32:55 -05:00
});