-
Notifications
You must be signed in to change notification settings - Fork 0
/
necker-worklet.js
55 lines (45 loc) · 1.39 KB
/
necker-worklet.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
class NeckerCubePainter {
// Typed OM
static get inputProperties() {
return [
'--side-length',
'--custom-color',
];
}
paint(ctx, geom, props) {
const lineWidth = geom.width * 0.01;
ctx.beginPath();
ctx.lineWidth = lineWidth;
ctx.strokeStyle = props.get('--custom-color');
let sideLength = props.get('--side-length');
let x = geom.width * 0.1
let y = x
let length = geom.width * 0.35
let points = [
x,
y,
length,
length
]
ctx.rect(...points)
let shiftedPoints = points.map(oldPoint => oldPoint + sideLength * 10)
ctx.rect(...shiftedPoints)
let x_prime = shiftedPoints[0]
let y_prime = shiftedPoints[1]
let length_prime = shiftedPoints[2]
// Top left stroke
ctx.moveTo(x, y);
ctx.lineTo(x_prime, y_prime);
// Top right stroke
ctx.moveTo(x + length, y);
ctx.lineTo(x_prime + length_prime, y_prime);
// Bottom left stroke
ctx.moveTo(x, y + length);
ctx.lineTo(x_prime, y_prime + length_prime);
// Bottom right stroke
ctx.moveTo(x + length, y + length);
ctx.lineTo(x_prime + length_prime, y_prime + length_prime);
ctx.stroke()
}
}
registerPaint('necker-cube', NeckerCubePainter);