-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
203 lines (179 loc) · 4.67 KB
/
Copy pathscript.js
File metadata and controls
203 lines (179 loc) · 4.67 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import QuadTree from "./QuadTree.js"
const CURSOR_RADIUS = 300
const canvas = document.querySelector('canvas')
if(!canvas)
throw new Error('No canvas found')
canvas.width = window.innerWidth * devicePixelRatio
canvas.height = window.innerHeight * devicePixelRatio
const ctx = canvas.getContext('2d')
if(!ctx)
throw new Error('No context found')
const form = document.querySelector('form')
if(!form)
throw new Error('No form found')
const numberOfPoints = 2 * Math.round(Math.sqrt(window.innerWidth * window.innerHeight))
start(ctx, numberOfPoints, form)
async function start(ctx, count, form) {
const formData = ui(form)
const points = Array(count).fill(0).map((_,id) => ({
id,
x: Math.random() * ctx.canvas.width,
y: Math.random() * ctx.canvas.height,
xSpeed: Math.random() * 2 - 1,
ySpeed: Math.random() * 2 - 1,
r: Math.random() * 2 + 1
}))
/** @type {QuadTree<typeof points[0]>} */
const tree = new QuadTree(0, 0, ctx.canvas.width, ctx.canvas.height)
points.forEach(point => tree.insert(point))
const mousePos = {
x: 200,
y: 200,
r: CURSOR_RADIUS,
}
const world = {
points,
tree,
mousePos,
formData,
}
trackMousePos(ctx, world.mousePos)
loop(ctx, world)
}
function ui(form) {
function getFormData(form) {
return {
geometry: form.elements.geometry.checked,
}
}
const formData = getFormData(form)
form.addEventListener('input', () => {
Object.assign(formData, getFormData(form))
})
return formData
}
function trackMousePos(ctx, mousePos) {
ctx.canvas.addEventListener('mousemove', e => {
mousePos.x = e.offsetX * devicePixelRatio
mousePos.y = e.offsetY * devicePixelRatio
})
}
async function loop(ctx, world) {
await frame()
const data = update(ctx, world)
draw(ctx, world, data)
loop(ctx, world)
}
function frame() {
return new Promise(resolve => requestAnimationFrame(resolve))
}
function update(ctx, world) {
world.points.forEach(point => {
point.x += point.xSpeed
point.y += point.ySpeed
world.tree.displace(point)
if(point.x < 0 || point.x > ctx.canvas.width) {
point.xSpeed *= -1
}
if(point.y < 0 || point.y > ctx.canvas.height) {
point.ySpeed *= -1
}
})
const candidates = world.tree.filter(
(quadrant) => rectCircleOverlap(quadrant, world.mousePos)
)
const points = candidates
.filter(
point => Math.hypot(point.x - world.mousePos.x, point.y - world.mousePos.y) < world.mousePos.r
)
return {points, candidates}
}
function draw(ctx, world, data) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
if (world.formData.geometry) {
drawTree(ctx, world.tree)
drawPoints(ctx, world.points)
}
drawPoints(ctx, data.candidates, '#808')
drawPoints(ctx, data.points, '#0f0')
if (world.formData.geometry) {
drawMouse(ctx, world.mousePos)
}
drawConnections(ctx, data.points, world.tree)
}
function drawConnections(ctx, points, tree) {
const distance = 60
ctx.save()
points.forEach(point => {
tree
.filter(
(quadrant) => rectCircleOverlap(quadrant, {...point, r: distance})
)
.filter(
neighbor => Math.hypot(neighbor.x - point.x, neighbor.y - point.y) < distance
)
.forEach(neighbor => {
ctx.strokeStyle = `rgba(80,0,80,${Math.hypot(neighbor.x - point.x, neighbor.y - point.y) / distance / 2})`
ctx.beginPath()
ctx.moveTo(point.x, point.y)
ctx.lineTo(neighbor.x, neighbor.y)
ctx.stroke()
})
})
ctx.restore()
}
function rectCircleOverlap(rect, circle) {
// Find the nearest point on the rectangle to the center of the circle
const x = Math.max(rect.x, Math.min(circle.x, rect.x + rect.width))
const y = Math.max(rect.y, Math.min(circle.y, rect.y + rect.height))
const dx = x - circle.x
const dy = y - circle.y
const r = circle.r
return dx**2 + dy**2 <= r**2
}
function drawMouse(ctx, mousePos) {
ctx.save()
ctx.strokeStyle = '#0f0'
ctx.beginPath()
ctx.arc(mousePos.x, mousePos.y, mousePos.r, 0, Math.PI * 2)
ctx.stroke()
ctx.restore()
}
function drawPoints(ctx, points, color = '#f00') {
ctx.save()
ctx.fillStyle = color
points.forEach(point => {
ctx.beginPath()
ctx.arc(point.x, point.y, point.r, 0, Math.PI * 2)
ctx.fill()
})
ctx.restore()
}
function drawTree(ctx, tree) {
ctx.save()
ctx.strokeStyle = '#fff3'
ctx.beginPath()
ctx.rect(tree.x, tree.y, tree.width, tree.height)
ctx.stroke()
if(tree.nodes) {
tree.nodes.forEach(node => {
drawTree(ctx, node)
})
}
ctx.restore()
}
function drawBranch(ctx, quadrant) {
drawQuadrant(ctx, quadrant)
if(quadrant.parent) {
drawBranch(ctx, quadrant.parent)
}
}
function drawQuadrant(ctx, quadrant) {
ctx.save()
ctx.fillStyle = '#8085'
// ctx.lineWidth = QuadTree.MAX_DEPTH - quadrant.depth
ctx.beginPath()
ctx.rect(quadrant.x, quadrant.y, quadrant.width, quadrant.height)
ctx.fill()
ctx.restore()
}