-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.ts
55 lines (47 loc) · 1.36 KB
/
render.ts
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
import { scenario } from 'awai';
import { drawCurve, drawLine, drawRectangle } from '../lib';
import {
canvasElementState,
currentLineLayerState,
currentPencilLayerState,
currentRectangleLayerState,
layersState,
} from '../state';
scenario(
() =>
Promise.race([
canvasElementState.events.changed,
currentLineLayerState.events.changed,
currentRectangleLayerState.events.changed,
currentPencilLayerState.events.changed,
layersState.events.changed,
]),
() => {
const canvas = canvasElementState.get();
const context = canvas?.getContext('2d');
if (!context) {
return;
}
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
const layers = [
...layersState.get(),
currentLineLayerState.get(),
currentRectangleLayerState.get(),
currentPencilLayerState.get(),
];
for (const layer of layers) {
if (layer.tool === 'line') {
const { config, endPoint, startPoint } = layer;
drawLine(context, startPoint, endPoint, config);
}
if (layer.tool === 'pencil') {
const { config, points } = layer;
drawCurve(context, points, config);
}
if (layer.tool === 'rectangle') {
const { config, endPoint, startPoint } = layer;
drawRectangle(context, startPoint, endPoint, config);
}
}
},
);