-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
conic-gradient.js
40 lines (37 loc) · 1.45 KB
/
conic-gradient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import Position from "@pencil.js/position";
import { radianCircle } from "@pencil.js/math";
/**
* @module ConicGradient
*/
/**
* Conic-gradient class
* <br><img src="./media/examples/conic-gradient.png" alt="conic-gradient demo"/>
* @class
*/
export default class ConicGradient {
/**
* Conic-gradient constructor
* @param {PositionDefinition} positionDefinition - Center of the gradient
* @param {Number} startAngle - Starting angle (0 is top, 0.5 is bottom and 1 is full circle back to top)
* @param {Object} colorStops - Set of colors to go through (key is position [from 0 to 1], value is the color)
* @example new ConicGradient(center, 0, { 0: "red", 0.25: "orange", 0.5: "yellow", 0.75: "green", 1: "blue" });
*/
constructor (positionDefinition, startAngle, colorStops) {
this.position = Position.from(positionDefinition);
this.startAngle = startAngle;
this.colorStops = colorStops;
}
/**
* Return a drawing context compatible gradient
* @param {CanvasRenderingContext2D} ctx - Drawing context
* @return {CanvasGradient}
*/
toString (ctx) {
const from = Position.from(this.position);
const gradient = ctx.createConicGradient((this.startAngle - 0.25) * radianCircle, from.x, from.y);
Object.keys(this.colorStops).forEach((key) => {
gradient.addColorStop(Number(key), this.colorStops[key].toString());
});
return gradient;
}
}