08대규모 팬줌 — 노드 수백 개를 캔버스 좌표로 훑는다
휠과 드래그로 캔버스 변환 값만 바꾸고 힘 계산은 원래 좌표계에서 그대로 돌리면 노드 수백 개짜리 그래프도 화면 안으로 자유롭게 훑을 수 있는데, 작은 데모를 실제 규모로 늘리는 항목입니다.
쓰임대규모 지식 그래프, 코드 의존성 맵
requestAnimationFramearclineToMath.hypot

<main class="stage">
<div class="gh is-demo" id="ghRoot" role="application" aria-label="노드 수백 개짜리 그래프를 훑는 팬줌 실험판" tabindex="0">
<canvas class="gh__cv" id="ghCv"></canvas>
<p class="gh__hint">
<span class="gh__title">대규모 팬줌</span>
<span class="gh__desc">캔버스 변환만 바꾸고 힘 계산은 원래 좌표계에서 그대로 돕니다</span>
</p>
<div class="gh__bar">
<span class="gh__meta">노드 140개 · 휠·드래그 팬줌 · rAF</span>
<span class="gh__meta gh__meta--dim">arc · lineTo · Math.hypot</span>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 대규모 팬줌(08) — 무대 종이. 카메라 변환·물리 둘 다 rAF 매 프레임 재계산.
$gh-stage: #f1ecdf;
@include stage($gh-stage);
$gh-panel: #e4dcc8;
$gh-text: #3c3324;
$gh-edge: #a99b7c;
$gh-node: #6a5a70;
$duration: $dur-loop;
$easing: $ease-out;
$color: $gh-node;
.gh {
--gh-panel: #{$gh-panel};
--gh-edge: #{$gh-edge};
--gh-node: #{$gh-node};
position: relative;
display: flex; flex-direction: column;
width: 100%; height: 100%; min-height: 164px;
overflow: hidden;
}
.gh__cv { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
.gh__cv:active { cursor: grabbing; }
.gh__hint {
position: relative; z-index: 1;
display: flex; flex-direction: column; gap: 0; margin: 0;
padding: $sp-2 $sp-3 0;
font-weight: 700; font-size: clamp(10px, 2.8vw, 12px); line-height: 1.35;
color: $gh-text;
}
.gh__title { text-shadow: 0 1px 6px rgba($gh-panel, .9); }
.gh__desc {
margin-top: $sp-1;
font-weight: 500; font-size: clamp(9px, 2.4vw, 10px); line-height: 1.4;
color: rgba($gh-text, .78);
text-shadow: 0 1px 6px rgba($gh-panel, .9);
}
.gh__bar {
position: relative; z-index: 1;
margin-top: auto;
display: flex; align-items: baseline; justify-content: space-between; gap: $sp-2;
padding: 0 $sp-3 $sp-2;
}
.gh__meta {
font-size: 10px; font-weight: 500; color: rgba($gh-text, .8);
background: rgba($gh-panel, .82);
border-radius: $r-xs;
padding: 4px $sp-1;
}
.gh:focus-visible { outline: 2px solid $gh-node; outline-offset: -2px; }
@media (max-width: 340px) {
.gh__meta--dim { display: none; }
}
@media (prefers-reduced-motion: reduce) {
.gh.is-still { cursor: default !important; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #f1ecdf;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.gh {
--gh-panel: #e4dcc8;
--gh-edge: #a99b7c;
--gh-node: #6a5a70;
position: relative;
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
min-height: 164px;
overflow: hidden;
}
.gh__cv {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
.gh__cv:active {
cursor: grabbing;
}
.gh__hint {
position: relative;
z-index: 1;
display: flex;
flex-direction: column;
gap: 0;
margin: 0;
padding: 8px 12px 0;
font-weight: 700;
font-size: clamp(10px, 2.8vw, 12px);
line-height: 1.35;
color: #3c3324;
}
.gh__title {
text-shadow: 0 1px 6px rgba(228, 220, 200, 0.9);
}
.gh__desc {
margin-top: 4px;
font-weight: 500;
font-size: clamp(9px, 2.4vw, 10px);
line-height: 1.4;
color: rgba(60, 51, 36, 0.78);
text-shadow: 0 1px 6px rgba(228, 220, 200, 0.9);
}
.gh__bar {
position: relative;
z-index: 1;
margin-top: auto;
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
padding: 0 12px 8px;
}
.gh__meta {
font-size: 10px;
font-weight: 500;
color: rgba(60, 51, 36, 0.8);
background: rgba(228, 220, 200, 0.82);
border-radius: 4px;
padding: 4px 4px;
}
.gh:focus-visible {
outline: 2px solid #6a5a70;
outline-offset: -2px;
}
@media (max-width: 340px) {
.gh__meta--dim {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.gh.is-still {
cursor: default !important;
}
}// 대규모 팬줌(08) — 물리 계산은 항상 원래(월드) 좌표계에서 돈다. 화면에 보이는
// 것은 카메라 변환(이동 camX·camY, 배율 zoom)을 씌운 결과일 뿐이다. .is-demo 동안은
// 카메라가 스스로 서서히 훑고, 실제 휠·드래그가 들어오면 그 값을 그대로 이어받는다.
(function () {
// ── 변형 상수 블록 ──
var N = 140;
var WORLD_W = 1400, WORLD_H = 900;
// ────────────────────────
var REPEL = 2600;
var ATTRACT = 0.018;
var REST = 42;
var DAMP = 0.93;
var CENTER_K = 0.0007;
var JITTER = 30;
var NR = 3.2;
var ZOOM_MIN = 0.28, ZOOM_MAX = 1.6;
var root = document.getElementById('ghRoot');
var cvEl = document.getElementById('ghCv');
if (!root || !cvEl) return;
var css = getComputedStyle(root);
function tokRGB(name) {
var s = css.getPropertyValue(name).trim();
var hx = s.length === 4
? s.charAt(1) + s.charAt(1) + s.charAt(2) + s.charAt(2) + s.charAt(3) + s.charAt(3)
: s.slice(1);
return [parseInt(hx.slice(0, 2), 16), parseInt(hx.slice(2, 4), 16), parseInt(hx.slice(4, 6), 16)];
}
var C_PANEL = tokRGB('--gh-panel');
var C_EDGE = tokRGB('--gh-edge');
var C_NODE = tokRGB('--gh-node');
var ctx = cvEl.getContext('2d');
var W = 0, H = 0, dpr = Math.min(window.devicePixelRatio || 1, 2);
function makeRand(seed) {
var st = seed >>> 0;
return function () {
st = (st + 0x6D2B79F5) >>> 0;
var t = st;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var rand = makeRand(0x327008);
var px, py, vx, vy, edges;
var camX = WORLD_W / 2, camY = WORLD_H / 2, zoom = 0.32;
var userDrag = false, lastPX = 0, lastPY = 0, userActive = false;
function buildGraph() {
px = new Float32Array(N); py = new Float32Array(N);
vx = new Float32Array(N); vy = new Float32Array(N);
for (var i = 0; i < N; i++) { px[i] = rand() * WORLD_W; py[i] = rand() * WORLD_H; }
edges = [];
for (var a = 1; a < N; a++) edges.push([a, Math.floor(rand() * a)]);
var extra = Math.round(N * 0.25);
for (var k = 0; k < extra; k++) {
var u = Math.floor(rand() * N), v = Math.floor(rand() * N);
if (u !== v) edges.push([u, v]);
}
}
function fit() {
var r = root.getBoundingClientRect();
W = Math.round(r.width); H = Math.round(r.height);
cvEl.width = Math.round(W * dpr); cvEl.height = Math.round(H * dpr);
buildGraph();
}
function step(dt) {
var fx = new Float32Array(N), fy = new Float32Array(N);
for (var i = 0; i < N; i++) {
for (var j = i + 1; j < N; j++) {
var dx = px[j] - px[i], dy = py[j] - py[i];
var d2 = dx * dx + dy * dy; if (d2 < 9) d2 = 9;
if (d2 > 40000) continue; // 먼 쌍은 생략 — 큰 그래프 성능 보호
var d = Math.sqrt(d2);
var f = REPEL / d2;
var ux = dx / d, uy = dy / d;
fx[i] -= ux * f; fy[i] -= uy * f;
fx[j] += ux * f; fy[j] += uy * f;
}
}
for (var e = 0; e < edges.length; e++) {
var a = edges[e][0], b = edges[e][1];
var dx2 = px[b] - px[a], dy2 = py[b] - py[a];
var d3 = Math.hypot(dx2, dy2) || 0.001;
var diff = (d3 - REST) * ATTRACT;
var ux2 = dx2 / d3, uy2 = dy2 / d3;
fx[a] += ux2 * diff; fy[a] += uy2 * diff;
fx[b] -= ux2 * diff; fy[b] -= uy2 * diff;
}
var cx = WORLD_W / 2, cy = WORLD_H / 2;
for (var n = 0; n < N; n++) {
fx[n] += (cx - px[n]) * CENTER_K;
fy[n] += (cy - py[n]) * CENTER_K;
fx[n] += (rand() - 0.5) * JITTER;
fy[n] += (rand() - 0.5) * JITTER;
vx[n] = (vx[n] + fx[n] * dt) * DAMP;
vy[n] = (vy[n] + fy[n] * dt) * DAMP;
px[n] += vx[n] * dt; py[n] += vy[n] * dt;
}
}
// .is-demo 자동 카메라 훑기 — 사람이 조작하지 않는 동안 서서히 팬·줌한다
function autoCam(t) {
camX = WORLD_W / 2 + Math.sin(t * 0.3) * WORLD_W * 0.32;
camY = WORLD_H / 2 + Math.cos(t * 0.24) * WORLD_H * 0.28;
zoom = 0.62 + 0.4 * Math.sin(t * 0.5);
}
function draw() {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = 'rgb(' + C_PANEL.join(',') + ')';
ctx.fillRect(0, 0, W, H);
ctx.save();
ctx.translate(W / 2, H / 2);
ctx.scale(zoom, zoom);
ctx.translate(-camX, -camY);
ctx.lineWidth = 1 / zoom;
ctx.strokeStyle = 'rgba(' + C_EDGE.join(',') + ',0.5)';
ctx.beginPath();
for (var e = 0; e < edges.length; e++) {
var a = edges[e][0], b = edges[e][1];
ctx.moveTo(px[a], py[a]); ctx.lineTo(px[b], py[b]);
}
ctx.stroke();
ctx.fillStyle = 'rgb(' + C_NODE.join(',') + ')';
for (var i = 0; i < N; i++) {
ctx.beginPath(); ctx.arc(px[i], py[i], NR, 0, Math.PI * 2); ctx.fill();
}
ctx.restore();
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var t0 = -1, last = -1;
function frame(now) {
if (t0 < 0) { t0 = now; last = now; }
var dt = Math.min((now - last) / 1000, 0.05); last = now;
var t = (now - t0) / 1000;
step(dt);
if (!userActive && root.classList.contains('is-demo')) autoCam(t);
draw();
requestAnimationFrame(frame);
}
function worldXY(clientX, clientY) {
var r = cvEl.getBoundingClientRect();
var sx = (clientX - r.left) - W / 2;
var sy = (clientY - r.top) - H / 2;
return [camX + sx / zoom, camY + sy / zoom];
}
cvEl.addEventListener('pointerdown', function (e) {
userDrag = true; userActive = true;
lastPX = e.clientX; lastPY = e.clientY;
cvEl.setPointerCapture(e.pointerId);
});
cvEl.addEventListener('pointermove', function (e) {
if (!userDrag) return;
camX -= (e.clientX - lastPX) / zoom;
camY -= (e.clientY - lastPY) / zoom;
lastPX = e.clientX; lastPY = e.clientY;
});
window.addEventListener('pointerup', function () { userDrag = false; });
cvEl.addEventListener('wheel', function (e) {
e.preventDefault();
userActive = true;
var before = worldXY(e.clientX, e.clientY);
zoom = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, zoom * (e.deltaY < 0 ? 1.12 : 0.89)));
var after = worldXY(e.clientX, e.clientY);
camX += before[0] - after[0];
camY += before[1] - after[1];
}, { passive: false });
function settle() {
for (var s = 0; s < 200; s++) step(1 / 60);
autoCam(1.4);
draw();
}
window.addEventListener('resize', function () {
fit();
if (reduce) settle();
});
fit();
if (reduce) {
settle();
root.classList.add('is-still');
} else {
requestAnimationFrame(frame);
}
})();// GraphForcePanZoomLarge — vanilla 판(run/327/demo/08-graph-force-pan-zoom-large/index.html) 의 React 포트.
// 물리 계산은 항상 원래(월드) 좌표계에서 돈다. 화면에 보이는 것은 카메라 변환
// (이동 camX·camY, 배율 zoom)을 씌운 결과일 뿐이다. 자동 상태에서는 카메라가 스스로
// 서서히 훑고, 실제 휠·드래그가 들어오면 그 값을 그대로 이어받는다.
import { useEffect, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./GraphForcePanZoomLarge.module.scss";
export type GraphForcePanZoomLargeProps = {
duration?: string;
easing?: string;
color?: string;
n?: number;
worldW?: number;
worldH?: number;
};
// ── 변형 상수(기본값) — vanilla 판과 동일 ──
const N = 140;
const WORLD_W = 1400, WORLD_H = 900;
const REPEL = 2600;
const ATTRACT = 0.018;
const REST = 42;
const DAMP = 0.93;
const CENTER_K = 0.0007;
const JITTER = 30;
const NR = 3.2;
const ZOOM_MIN = 0.28, ZOOM_MAX = 1.6;
const SEED = 0x327008;
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type RGB = [number, number, number];
type Edge = [number, number];
const BOARD_LABEL = "노드 수백 개짜리 그래프를 훑는 팬줌 실험판";
const META_LEFT = "노드 140개 · 휠·드래그 팬줌 · rAF";
const META_RIGHT = "arc · lineTo · Math.hypot";
const HINT_TITLE = "대규모 팬줌";
const HINT_DESC = "캔버스 변환만 바꾸고 힘 계산은 원래 좌표계에서 그대로 돕니다";
function makeRand(seed: number): () => number {
let st = seed >>> 0;
return function () {
st = (st + 0x6d2b79f5) >>> 0;
let t = st;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
export default function GraphForcePanZoomLarge({
duration = "2s",
easing = "ease-out",
color = "#6a5a70",
n = N,
worldW = WORLD_W,
worldH = WORLD_H
}: GraphForcePanZoomLargeProps) {
const rootRef = useRef<HTMLDivElement | null>(null);
const cvRef = useRef<HTMLCanvasElement | null>(null);
const ctxRef = useRef<CanvasRenderingContext2D | null>(null);
const colRef = useRef<{ panel: RGB; edge: RGB; node: RGB } | null>(null);
const wRef = useRef(0) as Box<number>;
const hRef = useRef(0) as Box<number>;
const rafRef = useRef(0) as Box<number>;
const randRef = useRef(makeRand(SEED)) as Box<() => number>;
const pxRef = useRef<Float32Array | null>(null);
const pyRef = useRef<Float32Array | null>(null);
const vxRef = useRef<Float32Array | null>(null);
const vyRef = useRef<Float32Array | null>(null);
const edgesRef = useRef<Edge[] | null>(null);
const camRef = useRef({ x: worldW / 2, y: worldH / 2, zoom: 0.32 }) as Box<{ x: number; y: number; zoom: number }>;
const userDragRef = useRef(false) as Box<boolean>;
const lastPXRef = useRef(0) as Box<number>;
const lastPYRef = useRef(0) as Box<number>;
const userActiveRef = useRef(false) as Box<boolean>;
const [still, setStill] = useState(false);
const vars: Vars = { "--duration": duration, "--easing": easing, "--color": color };
function reduced(): boolean {
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
}
function tokRGB(root: HTMLDivElement, name: string): RGB {
const s = getComputedStyle(root).getPropertyValue(name).trim();
const hx = s.length === 4
? s.charAt(1) + s.charAt(1) + s.charAt(2) + s.charAt(2) + s.charAt(3) + s.charAt(3)
: s.slice(1);
return [parseInt(hx.slice(0, 2), 16), parseInt(hx.slice(2, 4), 16), parseInt(hx.slice(4, 6), 16)];
}
function buildGraph(): void {
const rand = randRef.current;
const px = new Float32Array(n), py = new Float32Array(n);
const vx = new Float32Array(n), vy = new Float32Array(n);
for (let i = 0; i < n; i++) { px[i] = rand() * worldW; py[i] = rand() * worldH; }
const edges: Edge[] = [];
for (let a = 1; a < n; a++) edges.push([a, Math.floor(rand() * a)]);
const extra = Math.round(n * 0.25);
for (let k = 0; k < extra; k++) {
const u = Math.floor(rand() * n), v = Math.floor(rand() * n);
if (u !== v) edges.push([u, v]);
}
pxRef.current = px; pyRef.current = py; vxRef.current = vx; vyRef.current = vy;
edgesRef.current = edges;
}
function fit(): void {
const root = rootRef.current;
const cv = cvRef.current;
const ctx = ctxRef.current;
if (!root || !cv || !ctx) return;
const r = root.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
wRef.current = Math.round(r.width);
hRef.current = Math.round(r.height);
cv.width = Math.round(wRef.current * dpr);
cv.height = Math.round(hRef.current * dpr);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
buildGraph();
}
function step(dt: number): void {
const px = pxRef.current, py = pyRef.current, vx = vxRef.current, vy = vyRef.current, edges = edgesRef.current;
const rand = randRef.current;
if (!px || !py || !vx || !vy || !edges) return;
const fx = new Float32Array(n), fy = new Float32Array(n);
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const dx = px[j] - px[i], dy = py[j] - py[i];
let d2 = dx * dx + dy * dy; if (d2 < 9) d2 = 9;
if (d2 > 40000) continue; // 먼 쌍은 생략 — 큰 그래프 성능 보호
const d = Math.sqrt(d2);
const f = REPEL / d2;
const ux = dx / d, uy = dy / d;
fx[i] -= ux * f; fy[i] -= uy * f;
fx[j] += ux * f; fy[j] += uy * f;
}
}
for (let e = 0; e < edges.length; e++) {
const a = edges[e][0], b = edges[e][1];
const dx2 = px[b] - px[a], dy2 = py[b] - py[a];
const d3 = Math.hypot(dx2, dy2) || 0.001;
const diff = (d3 - REST) * ATTRACT;
const ux2 = dx2 / d3, uy2 = dy2 / d3;
fx[a] += ux2 * diff; fy[a] += uy2 * diff;
fx[b] -= ux2 * diff; fy[b] -= uy2 * diff;
}
const cx = worldW / 2, cy = worldH / 2;
for (let i = 0; i < n; i++) {
fx[i] += (cx - px[i]) * CENTER_K;
fy[i] += (cy - py[i]) * CENTER_K;
fx[i] += (rand() - 0.5) * JITTER;
fy[i] += (rand() - 0.5) * JITTER;
vx[i] = (vx[i] + fx[i] * dt) * DAMP;
vy[i] = (vy[i] + fy[i] * dt) * DAMP;
px[i] += vx[i] * dt; py[i] += vy[i] * dt;
}
}
// 자동 카메라 훑기 — 사람이 조작하지 않는 동안 서서히 팬·줌한다(vanilla .is-demo 상태 그대로)
function autoCam(t: number): void {
const cam = camRef.current;
cam.x = worldW / 2 + Math.sin(t * 0.3) * worldW * 0.32;
cam.y = worldH / 2 + Math.cos(t * 0.24) * worldH * 0.28;
cam.zoom = 0.62 + 0.4 * Math.sin(t * 0.5);
}
function draw(): void {
const ctx = ctxRef.current;
const col = colRef.current;
const px = pxRef.current, py = pyRef.current, edges = edgesRef.current;
const W = wRef.current, H = hRef.current;
const cam = camRef.current;
if (!ctx || !col || !px || !py || !edges) return;
const dpr = Math.min(window.devicePixelRatio || 1, 2);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = "rgb(" + col.panel.join(",") + ")";
ctx.fillRect(0, 0, W, H);
ctx.save();
ctx.translate(W / 2, H / 2);
ctx.scale(cam.zoom, cam.zoom);
ctx.translate(-cam.x, -cam.y);
ctx.lineWidth = 1 / cam.zoom;
ctx.strokeStyle = "rgba(" + col.edge.join(",") + ",0.5)";
ctx.beginPath();
for (let e = 0; e < edges.length; e++) {
const a = edges[e][0], b = edges[e][1];
ctx.moveTo(px[a], py[a]); ctx.lineTo(px[b], py[b]);
}
ctx.stroke();
ctx.fillStyle = "rgb(" + col.node.join(",") + ")";
for (let i = 0; i < n; i++) {
ctx.beginPath(); ctx.arc(px[i], py[i], NR, 0, Math.PI * 2); ctx.fill();
}
ctx.restore();
}
function worldXY(clientX: number, clientY: number): [number, number] {
const cv = cvRef.current;
const cam = camRef.current;
const W = wRef.current, H = hRef.current;
if (!cv) return [cam.x, cam.y];
const r = cv.getBoundingClientRect();
const sx = (clientX - r.left) - W / 2;
const sy = (clientY - r.top) - H / 2;
return [cam.x + sx / cam.zoom, cam.y + sy / cam.zoom];
}
function settle(): void {
for (let s = 0; s < 200; s++) step(1 / 60);
autoCam(1.4);
draw();
}
let t0 = -1, last = -1;
function frame(now: number): void {
if (t0 < 0) { t0 = now; last = now; }
const dt = Math.min((now - last) / 1000, 0.05); last = now;
const t = (now - t0) / 1000;
step(dt);
if (!userActiveRef.current) autoCam(t);
draw();
rafRef.current = requestAnimationFrame(frame);
}
useEffect(() => {
const root = rootRef.current;
const cv = cvRef.current;
if (!root || !cv) return;
colRef.current = {
panel: tokRGB(root, "--gh-panel"),
edge: tokRGB(root, "--gh-edge"),
node: tokRGB(root, "--gh-node")
};
ctxRef.current = cv.getContext("2d");
fit();
function onResize(): void {
fit();
if (reduced()) settle();
}
function onPointerDown(e: PointerEvent): void {
const cv2 = cvRef.current;
if (!cv2) return;
userDragRef.current = true; userActiveRef.current = true;
lastPXRef.current = e.clientX; lastPYRef.current = e.clientY;
cv2.setPointerCapture(e.pointerId);
}
function onPointerMove(e: PointerEvent): void {
if (!userDragRef.current) return;
const cam = camRef.current;
cam.x -= (e.clientX - lastPXRef.current) / cam.zoom;
cam.y -= (e.clientY - lastPYRef.current) / cam.zoom;
lastPXRef.current = e.clientX; lastPYRef.current = e.clientY;
}
function onPointerUp(): void {
userDragRef.current = false;
}
function onWheel(e: WheelEvent): void {
e.preventDefault();
userActiveRef.current = true;
const cam = camRef.current;
const before = worldXY(e.clientX, e.clientY);
cam.zoom = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, cam.zoom * (e.deltaY < 0 ? 1.12 : 0.89)));
const after = worldXY(e.clientX, e.clientY);
cam.x += before[0] - after[0];
cam.y += before[1] - after[1];
}
window.addEventListener("resize", onResize);
cv.addEventListener("pointerdown", onPointerDown);
cv.addEventListener("pointermove", onPointerMove);
window.addEventListener("pointerup", onPointerUp);
cv.addEventListener("wheel", onWheel, { passive: false });
if (reduced()) {
settle();
setStill(true);
} else {
t0 = -1; last = -1;
rafRef.current = requestAnimationFrame(frame);
}
return () => {
window.removeEventListener("resize", onResize);
cv.removeEventListener("pointerdown", onPointerDown);
cv.removeEventListener("pointermove", onPointerMove);
window.removeEventListener("pointerup", onPointerUp);
cv.removeEventListener("wheel", onWheel);
cancelAnimationFrame(rafRef.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [n, worldW, worldH]);
const rootCls = still ? `${styles.root} ${styles.isStill}` : styles.root;
return (
<main className={styles.stage}>
<div
className={rootCls}
ref={rootRef}
style={vars}
role="application"
aria-label={BOARD_LABEL}
tabIndex={0}
>
<canvas className={styles.cv} ref={cvRef}></canvas>
<p className={styles.hint}>
<span className={styles.title}>{HINT_TITLE}</span>
<span className={styles.desc}>{HINT_DESC}</span>
</p>
<div className={styles.bar}>
<span className={styles.meta}>{META_LEFT}</span>
<span className={styles.metaDim}>{META_RIGHT}</span>
</div>
</div>
</main>
);
}08 대규모 팬줌 — 노드 수백 개를 캔버스 좌표로 훑는다 — 측정 누적면적 70.841% · 움직인 프레임 23/23GODRICH 원작 · zip 에 SCSS·React 포함 · 네트워크 그래프 9종 — 힘으로 밀고 당겨 스스로 배치되는 캔버스
이 데모가 속한 글
네트워크 그래프 9종 — 힘으로 밀고 당겨 스스로 배치되는 캔버스네트워크 그래프는 점(노드)과 그 사이를 잇는 선(엣지)으로 관계를 나타낸 그림인데, 이 글은 그 점들의 자리를 사람이 정하지 않고 힘 계산만으로 매 프레임 다시 잡는 캔버스 엔진 아홉 판을 줍니다. 점과 선 몇 개 이 글의 데모 전부 보기


