/
app.js
70 lines (63 loc) · 1.62 KB
/
app.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
70
const id = location.search.substring(1);
const width = 256;
const height = 256;
const canvas = document.getElementById("canvas");
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
ctx.strokeStyle = "#000000";
ctx.lineJoin = "round";
ctx.lineCap = "round";
let drawingPoints = null;
let strokes = [];
canvas.addEventListener("pointerdown", (e) => {
drawingPoints = [[e.offsetX, e.offsetY, e.pressure * 10]];
});
canvas.addEventListener("pointermove", (e) => {
if (!drawingPoints) {
return;
}
const prevPoint = drawingPoints[drawingPoints.length - 1];
const curPoint = [e.offsetX, e.offsetY, e.pressure * 10];
drawingPoints.push(curPoint);
ctx.lineWidth = curPoint[2];
ctx.beginPath();
ctx.moveTo(prevPoint[0], prevPoint[1]);
ctx.lineTo(curPoint[0], curPoint[1]);
ctx.stroke();
});
canvas.addEventListener("pointerup", (e) => {
if (!drawingPoints) {
return;
}
strokes.push(new Float32Array(drawingPoints.flat()));
drawingPoints = null;
});
window.addEventListener(
"message",
(e) => {
if (location.origin !== e.origin) return;
const data = e.data;
if (data.type === "clear") {
ctx.clearRect(0, 0, width, height);
strokes = [];
drawingPoints = null;
window.parent.postMessage({
type: "clearResponse",
id,
requestId: data.requestId,
});
}
if (data.type === "getStrokes") {
window.parent.postMessage({
type: "getStrokesResponse",
id,
requestId: data.requestId,
strokes,
});
}
},
false
);