This repository was archived by the owner on Jul 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (60 loc) · 2.2 KB
/
script.js
File metadata and controls
73 lines (60 loc) · 2.2 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
import { drawGridLines, findClickedCell, canvas, drawToken, drawWeights, drawPath } from './script/utils/canvas.js';
import { brush, buildBrushMenu } from './script/utils/brushes.js';
import { grid } from './script/store/grid.js';
import { eventHandler, events } from './script/utils/events.js';
import { setCssStyles, styles } from './script/store/style.js';
import { findPath } from './script/utils/pathfinder.js';
import { measurePerformance } from './script/utils/performance.js';
import { updateInfo } from './script/utils/info.js';
import { tryRestoreFromQuery, updateUrlQuery } from './script/utils/deeplink.js';
let isDragging = false;
let draggedCells = new Set();
const updateFrame = () => {
drawGridLines();
const [path, performance] = measurePerformance(() => findPath(grid));
if (path && path.length > 2) {
drawPath({ path, color: styles.colorMid });
updateInfo(`The path took ${performance}ms to calculate.`);
}
for (const token of grid.allTokens) drawToken(token);
drawWeights();
updateUrlQuery();
};
const applyBrushAction = (event) => {
if (event.target !== canvas) return;
const posEvent = event.touches ? event.touches[0] : event;
const cell = findClickedCell(posEvent);
const cellId = `${cell.x},${cell.y}`;
if (!draggedCells.has(cellId)) {
brush.data[brush.current].action(cell);
draggedCells.add(cellId);
}
};
const handleStart = (event) => {
if (event.target !== canvas || event.button !== 0) return;
isDragging = true;
draggedCells.clear();
applyBrushAction(event);
};
const handleMove = (event) => {
if (isDragging) applyBrushAction(event);
};
const handleEnd = () => {
isDragging = false;
draggedCells.clear();
};
const initiateState = () => {
window.addEventListener('resize', updateFrame);
window.addEventListener('mousedown', handleStart);
window.addEventListener('mousemove', handleMove);
window.addEventListener('mouseup', handleEnd);
window.addEventListener('touchstart', handleStart);
window.addEventListener('touchmove', handleMove);
window.addEventListener('touchend', handleEnd);
eventHandler.on(events.DATA_CHANGED, updateFrame);
setCssStyles();
buildBrushMenu();
tryRestoreFromQuery();
updateFrame();
};
initiateState();