Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 3x 3x 3x 3x 3x 23x 23x 23x 23x 3x 3x 3x 23x 30x 30x 30x 23x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 2x 1x 1x 1x 23x 23x 23x 23x 2x 2x 2x 23x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 23x 1x 1x 1x 1x 23x 23x 19x | import type { Ref } from "vue";
const MIN_ZOOM = 0.25;
const MAX_ZOOM = 1.25;
/** Below this a node's text is too small to read, so framing stops shrinking and pans instead. */
const READABLE_ZOOM = 0.75;
/** A press that moves further than this is a drag, and doesn't select what it started on. */
const DRAG_THRESHOLD = 4;
/** A mouse notch reports a deltaY near 100, against a trackpad's handful per event. */
const WHEEL_STEP = 25;
/** Pan and zoom over a laid-out graph, framing whichever blocks `lit` returns. */
export const useGraphCamera = (
viewport: Ref<HTMLElement | null>,
lit: () => { x: number; y: number }[],
) => {
const camera = reactive({ x: 0, y: 0, k: 1 });
const easing = ref(false);
let easeTimer: ReturnType<typeof setTimeout> | undefined;
const clampZoom = (k: number) => Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, k));
const ease = (animate: boolean) => {
easing.value = animate;
clearTimeout(easeTimer);
Iif (animate) easeTimer = setTimeout(() => (easing.value = false), 500);
};
/** Centres the lit blocks, the selected path or the whole map, as large as fits but never unreadable. */
const fit = (animate: boolean) => {
const box = viewport.value?.getBoundingClientRect();
const nodes = lit();
Eif (!box?.width || nodes.length === 0) return;
const left = Math.min(...nodes.map(({ x }) => x));
const top = Math.min(...nodes.map(({ y }) => y));
const right = Math.max(...nodes.map(({ x }) => x + NODE_WIDTH));
const bottom = Math.max(...nodes.map(({ y }) => y + NODE_HEIGHT));
const k = clampZoom(
Math.max(
READABLE_ZOOM,
Math.min(
(box.width - PADDING * 2) / (right - left),
(box.height - PADDING * 2) / (bottom - top),
),
),
);
ease(animate);
camera.k = k;
camera.x = box.width / 2 - ((left + right) / 2) * k;
camera.y = box.height / 2 - ((top + bottom) / 2) * k;
};
/** Zooms keeping the map point under `(px, py)` in place; the viewport's centre by default. */
const zoomBy = (factor: number, px?: number, py?: number) => {
const box = viewport.value?.getBoundingClientRect();
Iif (!box) return;
const cx = px ?? box.width / 2;
const cy = py ?? box.height / 2;
const k = clampZoom(camera.k * factor);
ease(px === undefined);
camera.x = cx - ((cx - camera.x) / camera.k) * k;
camera.y = cy - ((cy - camera.y) / camera.k) * k;
camera.k = k;
};
/** Pinch on a trackpad arrives as a ctrl-wheel; a plain wheel zooms only when `free`, since
* otherwise it scrolls the page. */
const wheel = (event: WheelEvent, free = false) => {
if (!free && !event.ctrlKey && !event.metaKey) return;
event.preventDefault();
const box = viewport.value!.getBoundingClientRect();
zoomBy(
Math.exp(-Math.max(-WHEEL_STEP, Math.min(WHEEL_STEP, event.deltaY)) * 0.01),
event.clientX - box.left,
event.clientY - box.top,
);
};
const dragging = ref(false);
let pressed: { x: number; y: number; cameraX: number; cameraY: number; id: number } | null =
null;
let dragged = false;
const press = (event: PointerEvent) => {
Iif (event.button !== 0) return;
pressed = {
x: event.clientX,
y: event.clientY,
cameraX: camera.x,
cameraY: camera.y,
id: event.pointerId,
};
dragged = false;
};
const move = (event: PointerEvent) => {
Iif (pressed?.id !== event.pointerId) return;
const dx = event.clientX - pressed.x;
const dy = event.clientY - pressed.y;
Eif (!dragged && Math.hypot(dx, dy) > DRAG_THRESHOLD) {
dragged = true;
dragging.value = true;
ease(false);
viewport.value?.setPointerCapture(event.pointerId);
}
Eif (dragged) {
camera.x = pressed.cameraX + dx;
camera.y = pressed.cameraY + dy;
}
};
const release = (event: PointerEvent) => {
Eif (pressed && viewport.value?.hasPointerCapture(event.pointerId)) {
viewport.value.releasePointerCapture(event.pointerId);
}
pressed = null;
dragging.value = false;
};
onBeforeUnmount(() => clearTimeout(easeTimer));
return {
camera,
easing,
dragging,
wasDragged: () => dragged,
fit,
zoomBy,
wheel,
press,
move,
release,
};
};
|