-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
150 lines (126 loc) · 3.71 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
const canvas = document.querySelector('#canvas');
const width = 1900;
const height = 800;
canvas.width = width;
canvas.height = height;
/** @type {CanvasRenderingContext2D} */
const ctx = canvas.getContext('2d');
function add(a, b) {
return [a[0] + b[0], a[1] + b[1]];
}
function subtract(a, b) {
return [a[0] - b[0], a[1] - b[1]];
}
function multiply(a, s) {
return [s * a[0], s * a[1]];
}
function length(a) {
return Math.sqrt((a[0] * a[0]) + (a[1] * a[1]));
}
function normalize(a) {
const len = length(a);
return [a[0] / len, a[1] / len];
}
function dot(a, b) {
return (a[0] * b[0]) + (a[1] * b[1]);
}
function drawLine(a, b) {
ctx.beginPath();
ctx.moveTo(a[0], a[1]);
ctx.lineTo(b[0], b[1]);
ctx.stroke();
}
function drawPoint(a, r) {
ctx.beginPath();
ctx.arc(a[0], a[1], r, 0, Math.PI * 2);
ctx.fill();
}
function drawOffsetLine(a, b, w) {
const v = subtract(b, a);
const perp = normalize([-v[1], v[0]]);
const width = multiply(perp, w);
const aW = add(a, width);
const bW = add(b, width);
drawLine(aW, bW);
}
function drawThickLine(a, b, w) {
ctx.strokeStyle = '#000';
ctx.lineWidth = 2;
drawLine(a, b);
drawOffsetLine(a, b, w);
drawOffsetLine(a, b, -w);
ctx.strokeStyle = '#f00';
ctx.lineWidth = 1;
drawOffsetLine(b, add(b, multiply(subtract(b, a), 4)), w);
drawOffsetLine(b, add(b, multiply(subtract(b, a), 4)), -w);
}
function drawVector(vec, at, col) {
ctx.lineWidth = 3;
ctx.strokeStyle = col;
const end = add(at, multiply(vec, 60));
drawLine(at, end);
}
function drawCanvasLine(a, b, c) {
ctx.lineWidth = lineWidth * 2;
ctx.globalAlpha = 0.2;
ctx.strokeStyle = '#0000ff';
ctx.beginPath();
ctx.moveTo(...a);
ctx.lineTo(...b);
ctx.lineTo(...c);
ctx.stroke();
ctx.globalAlpha = 1.0;
}
let pointA = [150, 150];
let pointB = [700, 350];
let pointC = [1000, 600];
const lineWidth = 32;
const miterLimit = 5;
let clicked = false;
canvas.addEventListener('mousedown', () => {
clicked = true;
});
canvas.addEventListener('mouseup', () => {
clicked = false;
});
canvas.addEventListener('mousemove', (e) => {
if (clicked) {
pointA = [e.offsetX, e.offsetY];
requestAnimationFrame(render);
}
})
function render() {
ctx.miterLimit = miterLimit;
ctx.globalAlpha = 1;
ctx.clearRect(0, 0, width, height);
drawThickLine(pointA, pointB, lineWidth);
drawThickLine(pointC, pointB, lineWidth);
const vecA = normalize(subtract(pointB, pointA));
drawVector(vecA, pointB, '#65dd65');
const vecB = normalize(subtract(pointC, pointB));
drawVector(vecB, pointB, '#65dd65');
const tangent = normalize(add(vecA, vecB));
drawVector(tangent, pointB, '#23bbdd');
const miter = [-tangent[1], tangent[0]];
drawVector(miter, pointB, '#23bbdd');
const normalA = [-vecA[1], vecA[0]];
const normalB = [-vecB[1], vecB[0]];
const miterLength = 1 / dot(miter, normalA);
const miterA = add(pointB, multiply(miter, miterLength * lineWidth));
const miterB = add(pointB, multiply(miter, -miterLength * lineWidth));
if (miterLength < miterLimit || Math.sign(dot(normalA, tangent)) > 0) {
drawPoint(miterA, 5);
}
if (miterLength < miterLimit || Math.sign(dot(normalB, tangent)) > 0) {
drawPoint(miterB, 5);
}
if (miterLength > ctx.miterLimit) {
const side = Math.sign(dot(vecA, miter));
const clipPointA = add(pointB, multiply(normalize(normalA), side * lineWidth));
const clipPointB = add(pointB, multiply(normalize(normalB), side * lineWidth));
drawPoint(clipPointA, 5);
drawPoint(clipPointB, 5);
}
drawCanvasLine(pointA, pointB, pointC);
}
render();