-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
342 lines (313 loc) · 12.5 KB
/
Copy pathindex.html
File metadata and controls
342 lines (313 loc) · 12.5 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<!doctype html>
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>simplequad — Query Explorer</title>
<meta name="description" content="Query a mixed-shape scene by moving the cursor. Hit shapes light up with MTV arrows. Toggle the tree overlay to see live spatial partitioning." />
<meta property="og:type" content="website" />
<meta property="og:title" content="simplequad — Query Explorer" />
<meta property="og:description" content="Query a mixed-shape scene by moving the cursor. Hit shapes light up with MTV arrows. Toggle the tree overlay to see live spatial partitioning." />
<meta property="og:image" content="https://rcasto.github.io/simplequad/simplequad.gif" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="simplequad — Query Explorer" />
<meta name="twitter:description" content="Query a mixed-shape scene by moving the cursor. Hit shapes light up with MTV arrows. Toggle the tree overlay to see live spatial partitioning." />
<meta name="twitter:image" content="https://rcasto.github.io/simplequad/simplequad.gif" />
<link rel="stylesheet" href="../engine.css" />
<style>
:root {
--bg: #080814;
--border: #1a1a3e;
--text: #eee;
--muted: #666;
--muted2: #3a3a5a;
--accent: #7c4dff;
--btn-bg: #10101e;
--btn-border: #2a2a4a;
--btn-text: #666;
}
[data-theme="light"] {
--bg: #f0f0f8;
--border: #c8c8e0;
--text: #1a1a2e;
--muted: #778;
--muted2: #aaa;
--accent: #5c2ddf;
--btn-bg: #eeeef8;
--btn-border: #c0c0d8;
--btn-text: #555;
}
canvas {
cursor: none;
aspect-ratio: 800 / 500;
touch-action: none;
}
#hud { gap: 20px; }
button {
background: var(--btn-bg);
border: 1px solid var(--btn-border);
color: var(--btn-text);
padding: 3px 10px;
font-family: monospace;
font-size: 12px;
border-radius: 4px;
cursor: pointer;
transition: border-color 0.15s, color 0.15s;
}
button.on {
border-color: var(--accent);
color: var(--accent);
}
.legend {
display: flex;
gap: 14px;
align-items: center;
}
.legend-item {
display: flex;
align-items: center;
gap: 5px;
font-size: 12px;
color: var(--muted);
}
.swatch-box {
width: 11px;
height: 11px;
border: 1.5px solid #4488ff;
}
.swatch-circle {
width: 11px;
height: 11px;
border: 1.5px solid #44ccaa;
border-radius: 50%;
}
.swatch-point {
width: 10px;
height: 10px;
background: #ff8844;
transform: rotate(45deg);
}
</style>
<script>(function(){var t=localStorage.getItem('sq-theme');if(t)document.documentElement.setAttribute('data-theme',t);})()</script>
</head>
<body>
<canvas id="c" width="800" height="500"></canvas>
<div id="hud">
<div class="legend">
<div class="legend-item"><div class="swatch-box"></div>BoundingBox</div>
<div class="legend-item"><div class="swatch-circle"></div>Circle</div>
<div class="legend-item"><div class="swatch-point"></div>Point</div>
</div>
<span>hits: <b id="hits">0</b></span>
<span>fps: <b id="fps">0</b></span>
<button id="btn-tree">tree overlay</button>
<button id="btn-shape">query: box</button>
<button id="btn-theme" title="toggle theme">☀</button>
</div>
<div id="controls">move cursor over shapes · yellow arrows show MTV · points have no depth (MTV n/a)</div>
<script src="../../dist/simplequad.umd.js"></script>
<script src="../engine.js"></script>
<script>
const canvas = document.getElementById("c");
const ctx = canvas.getContext("2d");
const W = 800, H = 500;
const toCanvas = SQEngine.makeToCanvas(canvas, W, H);
const fpsTick = SQEngine.createFpsTracker(document.getElementById("fps"), canvas);
SQEngine.initTheme();
// ── shapes ────────────────────────────────────────────────────────────
const shapes = [
{ x: 55, y: 95, width: 140, height: 95 },
{ x: 585, y: 60, width: 130, height: 120 },
{ x: 300, y: 310, width: 160, height: 100 },
{ x: 192, y: 318, r: 70 },
{ x: 512, y: 282, r: 58 },
{ x: 672, y: 402, r: 46 },
{ x: 400, y: 148 },
{ x: 148, y: 435 },
];
function isBox(s) { return s.width !== undefined; }
function isCircle(s) { return s.r !== undefined; }
function center(s) {
if (isBox(s)) return { x: s.x + s.width / 2, y: s.y + s.height / 2 };
return { x: s.x, y: s.y };
}
const tree = SimpleQuad.createQuadTree({ x: 0, y: 0, width: W, height: H });
for (const s of shapes) tree.add(s);
// ── state ─────────────────────────────────────────────────────────────
let mx = W / 2, my = H / 2;
let showTree = false;
let circleQuery = false;
const QW = 110, QR = 62;
// ── input ─────────────────────────────────────────────────────────────
canvas.addEventListener("mousemove", e => {
const p = toCanvas(e.clientX, e.clientY);
mx = p.x; my = p.y;
});
canvas.addEventListener("touchstart", e => {
e.preventDefault();
const p = toCanvas(e.touches[0].clientX, e.touches[0].clientY);
mx = p.x; my = p.y;
}, { passive: false });
canvas.addEventListener("touchmove", e => {
e.preventDefault();
const p = toCanvas(e.touches[0].clientX, e.touches[0].clientY);
mx = p.x; my = p.y;
}, { passive: false });
document.getElementById("btn-tree").addEventListener("click", e => {
showTree = !showTree;
e.currentTarget.classList.toggle("on", showTree);
});
document.getElementById("btn-shape").addEventListener("click", e => {
circleQuery = !circleQuery;
e.currentTarget.textContent = circleQuery ? "query: circle" : "query: box";
});
// ── colors ────────────────────────────────────────────────────────────
const C_BOX = "#4488ff";
const C_CIRCLE = "#44ccaa";
const C_POINT = "#ff8844";
const C_HIT = "#ffffff";
const C_MTV = "#ffdd00";
// ── draw helpers ──────────────────────────────────────────────────────
function drawShape(s, color, hit) {
ctx.lineWidth = hit ? 2.5 : 1.5;
ctx.strokeStyle = color;
ctx.fillStyle = color;
if (isBox(s)) {
ctx.globalAlpha = hit ? 0.22 : 0.08;
ctx.fillRect(s.x, s.y, s.width, s.height);
ctx.globalAlpha = 1;
ctx.strokeRect(s.x, s.y, s.width, s.height);
} else if (isCircle(s)) {
ctx.beginPath();
ctx.arc(s.x, s.y, s.r, 0, Math.PI * 2);
ctx.globalAlpha = hit ? 0.22 : 0.08;
ctx.fill();
ctx.globalAlpha = 1;
ctx.stroke();
} else {
const sz = hit ? 9 : 7;
ctx.beginPath();
ctx.moveTo(s.x, s.y - sz);
ctx.lineTo(s.x + sz, s.y);
ctx.lineTo(s.x, s.y + sz);
ctx.lineTo(s.x - sz, s.y);
ctx.closePath();
ctx.fill();
}
ctx.globalAlpha = 1;
}
function drawLabel(s, color, hit) {
const label = isBox(s) ? "BoundingBox" : isCircle(s) ? "Circle" : "Point";
const c = center(s);
const yOff = isBox(s) ? s.height / 2 + 15
: isCircle(s) ? s.r + 15
: 18;
ctx.fillStyle = hit ? "rgba(255,255,255,0.85)" : color + "66";
ctx.font = "11px monospace";
ctx.textAlign = "center";
ctx.fillText(label, c.x, c.y + yOff);
// Points don't have geometric depth, so no MTV applies
if (!isBox(s) && !isCircle(s) && hit) {
ctx.fillStyle = "rgba(255,221,0,0.6)";
ctx.fillText("mtv: n/a", c.x, c.y + yOff + 14);
}
}
function drawArrow(cx, cy, dx, dy, len) {
const ex = cx + dx * len;
const ey = cy + dy * len;
ctx.strokeStyle = C_MTV;
ctx.fillStyle = C_MTV;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(cx, cy);
ctx.lineTo(ex, ey);
ctx.stroke();
const a = Math.atan2(dy, dx);
const hw = 9;
ctx.beginPath();
ctx.moveTo(ex, ey);
ctx.lineTo(ex - hw * Math.cos(a - 0.42), ey - hw * Math.sin(a - 0.42));
ctx.lineTo(ex - hw * Math.cos(a + 0.42), ey - hw * Math.sin(a + 0.42));
ctx.closePath();
ctx.fill();
}
function drawMtvLabel(cx, cy, dx, dy, len, mag) {
const lx = cx + dx * (len * 0.55);
const ly = cy + dy * (len * 0.55) - 8;
ctx.fillStyle = C_MTV + "cc";
ctx.font = "10px monospace";
ctx.textAlign = "center";
ctx.fillText(mag.toFixed(1) + "px", lx, ly);
}
function drawTreeNode(node) {
ctx.strokeRect(node.bounds.x, node.bounds.y, node.bounds.width, node.bounds.height);
for (const q of node.quadrants) drawTreeNode(q);
}
// ── frame loop ────────────────────────────────────────────────────────
function frame(ts) {
fpsTick(ts);
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = "#080814";
ctx.fillRect(0, 0, W, H);
if (showTree) {
ctx.strokeStyle = "rgba(124,77,255,0.2)";
ctx.lineWidth = 0.5;
drawTreeNode(tree);
}
const qb = circleQuery
? { x: mx, y: my, r: QR }
: { x: mx - QW / 2, y: my - QW / 2, width: QW, height: QW };
const hits = tree.query(qb);
const hitSet = new Set(hits.map(h => h.object));
const hitMtv = new Map(hits.map(h => [h.object, h.mtv]));
for (const s of shapes) {
if (hitSet.has(s)) continue;
const base = isBox(s) ? C_BOX : isCircle(s) ? C_CIRCLE : C_POINT;
drawShape(s, base, false);
}
for (const s of shapes) {
if (!hitSet.has(s)) continue;
drawShape(s, C_HIT, true);
}
// MTV arrows — only for shapes that have geometric extent (box, circle)
for (const s of shapes) {
if (!hitSet.has(s)) continue;
if (!isBox(s) && !isCircle(s)) continue;
const mtv = hitMtv.get(s);
const c = center(s);
const len = Math.max(40, mtv.magnitude * 2.5);
drawArrow(c.x, c.y, mtv.direction.x, mtv.direction.y, len);
drawMtvLabel(c.x, c.y, mtv.direction.x, mtv.direction.y, len, mtv.magnitude);
}
for (const s of shapes) {
const base = isBox(s) ? C_BOX : isCircle(s) ? C_CIRCLE : C_POINT;
drawLabel(s, base, hitSet.has(s));
}
// query window
ctx.save();
ctx.setLineDash([6, 4]);
ctx.lineWidth = 1.5;
ctx.strokeStyle = "rgba(255,255,255,0.5)";
ctx.fillStyle = "rgba(255,255,255,0.03)";
if (circleQuery) {
ctx.beginPath();
ctx.arc(qb.x, qb.y, qb.r, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
} else {
ctx.fillRect(qb.x, qb.y, qb.width, qb.height);
ctx.strokeRect(qb.x, qb.y, qb.width, qb.height);
}
ctx.restore();
// cursor dot
ctx.fillStyle = "rgba(255,255,255,0.75)";
ctx.beginPath();
ctx.arc(mx, my, 2.5, 0, Math.PI * 2);
ctx.fill();
document.getElementById("hits").textContent = hits.length;
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>