-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint.js
69 lines (58 loc) · 1.89 KB
/
paint.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class Rays {
static get inputProperties() {
return [
'--num-rays',
'--start-alpha',
'--end-alpha',
];
}
getProps(props) {
let p;
p = props.get('--num-rays');
if ('value' in p)
this.n = p.value; // CSS.registerProperty() supported
else
this.n = parseInt(p.toString())
p = props.get('--start-alpha');
if ('value' in p)
this.startAlpha = p.value; // CSS.registerProperty() supported
else
this.startAlpha = parseFloat(p.toString())
p = props.get('--end-alpha');
if ('value' in p)
this.endAlpha = p.value; // CSS.registerProperty() supported
else
this.endAlpha = parseFloat(p.toString())
}
paint(ctx, size, props) {
this.getProps(props);
let
c = [size.width / 2, size.height / 2],
r = distance(c, [size.width, 0]),
deltaTheta = (2 * Math.PI) / (2*this.n),
i
;
let gradient = ctx.createRadialGradient(c[0], c[1], 0, c[0], c[1], r);
gradient.addColorStop(0, `hsla(0, 0%, 100%, ${this.startAlpha}`);
gradient.addColorStop(1, `hsla(0, 0%, 100%, ${this.endAlpha}`);
let startAngle = 0;
ctx.beginPath();
ctx.fillStyle = gradient;
ctx.moveTo(c[0], c[1]);
for (i = 1; i <= this.n; i++) {
ctx.lineTo(c[0] + r * Math.cos(startAngle), c[1] + r * Math.sin(startAngle));
ctx.arc(c[0], c[1], r, startAngle, startAngle + deltaTheta);
ctx.lineTo(c[0], c[1]);
startAngle += deltaTheta * 2;
}
ctx.fill();
}
}
function distance(p1, p2) {
let
dx = p2[0] - p1[0],
dy = p2[1] - p1[1]
;
return Math.sqrt(dx*dx + dy*dy);
}
registerPaint('rays', Rays);