-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (103 loc) · 3.8 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as Triangulation from "./triangulation.js";
import {Vector2} from "./vector.js";
const canvas = document.getElementById("canvas");
const dpr = window.devicePixelRatio;
const rect = canvas.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
const ctx = canvas.getContext("2d");
ctx.scale(dpr, dpr);
const pointSize = 5;
/** @type {Vector2[]} */
const points = [];
canvas.onmousedown = canvas.ontouchstart = e => {
e.stopPropagation();
e.preventDefault();
document.getElementById("hint").style.display = "none";
const bcr = e.target.getBoundingClientRect();
const point = e.touches ? e.touches[0] : e;
points.push(new Vector2(point.clientX - bcr.x, point.clientY - bcr.y));
render();
}
const resizeObserver = new ResizeObserver((e) => {
const rect = canvas.getBoundingClientRect();
const oldSize = new Vector2(canvas.width, canvas.height);
const newSize = new Vector2(rect.width * dpr, rect.height * dpr);
if (oldSize.x === newSize.x && oldSize.y === newSize.y) return;
canvas.width = newSize.x;
canvas.height = newSize.y;
ctx.scale(dpr, dpr);
for (const point of points) {
point.div(oldSize).mul(newSize);
}
render();
})
resizeObserver.observe(canvas);
function render() {
if (points.length === 0) return;
ctx.clearRect(0, 0, rect.width, rect.height);
ctx.strokeStyle = "black";
ctx.font = "14px serif";
ctx.setLineDash([]);
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
for (let i = 1; i <= points.length; i++) {
const index = i % points.length;
const point = points[index];
ctx.lineTo(point.x, point.y);
}
ctx.stroke();
ctx.fillStyle = "red";
for (const point of points) {
ctx.beginPath();
ctx.arc(point.x, point.y, pointSize, 0, Math.PI * 2);
ctx.fill();
}
if (points.length < 3) return;
ctx.fillStyle = "blue"
const isClockwise = Triangulation.isClockwise(points);
for (let i = 1; i <= points.length; i++) {
const index = i % points.length;
const point = points[index];
const prev = points[i - 1];
const next = points[(i + 1) % points.length];
drawArrow(point, point.delta(prev).angleSelf(), pointSize * 2, pointSize);
const tangent = prev.tangent(next);
const offset = tangent.perpendicular()
.scale(pointSize * 3)
.scale(isClockwise ? 1 : -1);
drawLabel(point, index.toString(), offset);
}
const polygons = Triangulation.earClipping(points);
document.getElementById("error").style.display = polygons ? "none" : null;
if (!polygons) return;
ctx.strokeStyle = "#96b29a";
ctx.setLineDash([2, 8]);
for (const poly of polygons) {
ctx.beginPath();
ctx.moveTo(points[poly[0]].x, points[poly[0]].y);
ctx.lineTo(points[poly[1]].x, points[poly[1]].y);
ctx.lineTo(points[poly[2]].x, points[poly[2]].y);
ctx.lineTo(points[poly[0]].x, points[poly[0]].y);
ctx.stroke();
}
}
function drawLabel(point, text, offset) {
const measure = ctx.measureText(text);
const height = measure.actualBoundingBoxAscent - measure.actualBoundingBoxDescent;
ctx.fillText(text, offset.x + point.x - measure.width / 2, offset.y + point.y + height / 2);
}
function drawArrow(point, angle, size, offset) {
ctx.save();
ctx.fillStyle = ctx.strokeStyle;
ctx.translate(point.x, point.y);
ctx.rotate(angle - Math.PI);
ctx.translate(offset, 0);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(size, -size / 2);
ctx.lineTo(size, size / 2);
ctx.lineTo(0, 0);
ctx.fill();
ctx.restore();
}