03군집 색 구분 — 그룹끼리 더 세게 당겨 뭉친다
같은 그룹 노드끼리 인력을 더 세게, 다른 그룹끼리는 척력을 더 세게 주면 색깔별 덩어리가 화면에 자연히 나뉘는데, 데이터 안의 군집을 힘만으로 드러내는 항목입니다.
쓰임커뮤니티 탐지 시각화, 태그 클러스터
arcfillRectrequestAnimationFrameMath.hypot

<main class="stage">
<div class="gc is-demo" id="gcRoot" role="img" aria-label="색으로 나뉜 세 군집이 힘으로 뭉치는 관계도" tabindex="0">
<canvas class="gc__cv" id="gcCv"></canvas>
<p class="gc__hint">
<span class="gc__title">군집 색 구분</span>
<span class="gc__desc">같은 색끼리 더 세게 당기고 다른 색끼리 더 세게 밀어냅니다</span>
</p>
<div class="gc__bar">
<span class="gc__meta">노드 30개 · 3군집 · rAF</span>
<span class="gc__meta gc__meta--dim">arc · fillRect · Math.hypot</span>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 군집 색 구분(03) — 무대 테라코타. @keyframes 없음, 전부 rAF 물리 계산.
// 같은 그룹은 무게중심으로 약하게, 다른 그룹은 척력을 세게 — 색 덩어리가 스스로 갈라진다.
$gc-stage: #c49d92;
@include stage($gc-stage);
$gc-panel: #ead9d1; // 캔버스 바닥 — 무대보다 밝은 테라코타
$gc-text: #3a241c; // 라벨 글자 — 짙은 브라운
$gc-g1: #6f8f7c; // 그룹 1 — 세이지
$gc-g2: #7c85a8; // 그룹 2 — 더스티 바이올렛
$gc-g3: #b0725f; // 그룹 3 — 러스트 로즈
$duration: $dur-loop;
$easing: $ease-out;
$color: $gc-g2;
.gc {
--gc-panel: #{$gc-panel};
--gc-g1: #{$gc-g1};
--gc-g2: #{$gc-g2};
--gc-g3: #{$gc-g3};
position: relative;
display: flex; flex-direction: column;
width: 100%; height: 100%; min-height: 164px;
overflow: hidden;
}
.gc__cv { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }
.gc__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: $gc-text;
}
.gc__title { text-shadow: 0 1px 6px rgba($gc-panel, .9); }
.gc__desc {
margin-top: $sp-1;
font-weight: 500; font-size: clamp(9px, 2.4vw, 10px); line-height: 1.4;
color: rgba($gc-text, .78);
text-shadow: 0 1px 6px rgba($gc-panel, .9);
}
.gc__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;
}
.gc__meta {
font-size: 10px; font-weight: 500; color: rgba($gc-text, .8);
background: rgba($gc-panel, .82);
border-radius: $r-xs;
padding: 4px $sp-1;
}
.gc:focus-visible { outline: 2px solid $gc-g2; outline-offset: -2px; }
@media (max-width: 340px) {
.gc__meta--dim { display: none; }
}
@media (prefers-reduced-motion: reduce) {
.gc.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: #c49d92;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.gc {
--gc-panel: #ead9d1;
--gc-g1: #6f8f7c;
--gc-g2: #7c85a8;
--gc-g3: #b0725f;
position: relative;
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
min-height: 164px;
overflow: hidden;
}
.gc__cv {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
}
.gc__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: #3a241c;
}
.gc__title {
text-shadow: 0 1px 6px rgba(234, 217, 209, 0.9);
}
.gc__desc {
margin-top: 4px;
font-weight: 500;
font-size: clamp(9px, 2.4vw, 10px);
line-height: 1.4;
color: rgba(58, 36, 28, 0.78);
text-shadow: 0 1px 6px rgba(234, 217, 209, 0.9);
}
.gc__bar {
position: relative;
z-index: 1;
margin-top: auto;
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
padding: 0 12px 8px;
}
.gc__meta {
font-size: 10px;
font-weight: 500;
color: rgba(58, 36, 28, 0.8);
background: rgba(234, 217, 209, 0.82);
border-radius: 4px;
padding: 4px 4px;
}
.gc:focus-visible {
outline: 2px solid #7c85a8;
outline-offset: -2px;
}
@media (max-width: 340px) {
.gc__meta--dim {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.gc.is-still {
cursor: default !important;
}
}// 군집 색 구분(03) — 엣지는 없다. 대신 같은 그룹 노드는 그룹 무게중심 쪽으로
// 약하게 끌리고, 다른 그룹끼리는 척력이 더 세다. 이 비대칭 힘만으로 색깔별
// 덩어리가 화면 위에서 스스로 갈라진다 — 매 프레임 중심을 다시 구해 계속 움직인다.
(function () {
// ── 변형 상수 블록 ──
var N = 30;
var GROUPS = 3;
var INTRA_REPEL = 0.5; // 같은 그룹 척력 배율(작을수록 잘 뭉친다)
var INTER_REPEL = 1.9; // 다른 그룹 척력 배율(클수록 잘 갈라진다)
// ────────────────────────
var REPEL = 640;
var CENTROID_K = 0.05; // 그룹 무게중심으로 끌리는 힘
var CENTER_K = 0.0006;
var DAMP = 0.955;
var JITTER = 46;
var NR = 4.6;
var OMEGA = 0.75; // 전체를 천천히 돌려 색 덩어리가 화면을 넓게 훑게 한다
var root = document.getElementById('gcRoot');
var cvEl = document.getElementById('gcCv');
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('--gc-panel');
var C_G = [tokRGB('--gc-g1'), tokRGB('--gc-g2'), tokRGB('--gc-g3')];
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(0x327003);
var px, py, vx, vy, grp;
function buildGraph() {
px = new Float32Array(N); py = new Float32Array(N);
vx = new Float32Array(N); vy = new Float32Array(N);
grp = new Int8Array(N);
for (var i = 0; i < N; i++) {
px[i] = rand() * W; py[i] = rand() * H;
grp[i] = i % GROUPS;
}
}
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);
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
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 d = Math.hypot(dx, dy) || 0.001;
var d2 = d * d; if (d2 < 4) d2 = 4;
var mult = grp[i] === grp[j] ? INTRA_REPEL : INTER_REPEL;
var f = (REPEL * mult) / d2;
var ux = dx / d, uy = dy / d;
fx[i] -= ux * f; fy[i] -= uy * f;
fx[j] += ux * f; fy[j] += uy * f;
}
}
// 그룹 무게중심 — 매 프레임 다시 구해 그 쪽으로 약하게 당긴다
var cxg = new Float32Array(GROUPS), cyg = new Float32Array(GROUPS), cnt = new Float32Array(GROUPS);
for (var n = 0; n < N; n++) { cxg[grp[n]] += px[n]; cyg[grp[n]] += py[n]; cnt[grp[n]]++; }
for (var g = 0; g < GROUPS; g++) { if (cnt[g] > 0) { cxg[g] /= cnt[g]; cyg[g] /= cnt[g]; } }
var cx = W / 2, cy = H / 2;
for (var m = 0; m < N; m++) {
fx[m] += (cxg[grp[m]] - px[m]) * CENTROID_K;
fy[m] += (cyg[grp[m]] - py[m]) * CENTROID_K;
fx[m] += (cx - px[m]) * CENTER_K;
fy[m] += (cy - py[m]) * CENTER_K;
fx[m] += (rand() - 0.5) * JITTER;
fy[m] += (rand() - 0.5) * JITTER;
vx[m] = (vx[m] + fx[m] * dt) * DAMP;
vy[m] = (vy[m] + fy[m] * dt) * DAMP;
px[m] += vx[m] * dt; py[m] += vy[m] * dt;
}
}
function rotateAll(dtheta) {
var cx = W / 2, cy = H / 2, cs = Math.cos(dtheta), sn = Math.sin(dtheta);
for (var i = 0; i < N; i++) {
var rx = px[i] - cx, ry = py[i] - cy;
px[i] = cx + rx * cs - ry * sn;
py[i] = cy + rx * sn + ry * cs;
}
}
function draw(t) {
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = 'rgb(' + C_PANEL.join(',') + ')';
ctx.fillRect(0, 0, W, H);
for (var i = 0; i < N; i++) {
var c = C_G[grp[i]];
var pulse = 0.5 + 0.5 * Math.sin(t * 1.8 + i * 0.9);
ctx.fillStyle = 'rgba(' + c.join(',') + ',' + (0.16 + 0.18 * pulse).toFixed(3) + ')';
ctx.beginPath(); ctx.arc(px[i], py[i], NR + 5 + pulse * 6, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = 'rgb(' + c.join(',') + ')';
ctx.beginPath(); ctx.arc(px[i], py[i], NR, 0, Math.PI * 2); ctx.fill();
}
}
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);
rotateAll(OMEGA * dt);
draw(t);
requestAnimationFrame(frame);
}
function settle() {
for (var s = 0; s < 260; s++) step(1 / 60);
draw(0);
}
window.addEventListener('resize', function () {
fit();
if (reduce) settle();
});
fit();
if (reduce) {
settle();
root.classList.add('is-still');
} else {
requestAnimationFrame(frame);
}
})();// GraphForceClusterColor — vanilla 판(run/327/demo/03-graph-force-cluster-color/index.html) 의 React 포트.
// 엣지는 없다. 대신 같은 그룹 노드는 그룹 무게중심 쪽으로 약하게 끌리고, 다른 그룹끼리는
// 척력이 더 세다. 이 비대칭 힘만으로 색깔별 덩어리가 화면 위에서 스스로 갈라진다 —
// 매 프레임 중심을 다시 구해 계속 움직인다. 전체는 rotateAll 로 천천히 강체 회전한다.
import { useEffect, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./GraphForceClusterColor.module.scss";
export type GraphForceClusterColorProps = {
duration?: string;
easing?: string;
color?: string;
n?: number;
intraRepel?: number; // 같은 그룹 척력 배율(작을수록 잘 뭉친다)
interRepel?: number; // 다른 그룹 척력 배율(클수록 잘 갈라진다)
};
// ── 변형 상수(기본값) — vanilla 판과 동일 ──
const N = 30;
const GROUPS = 3;
const INTRA_REPEL = 0.5;
const INTER_REPEL = 1.9;
const REPEL = 640;
const CENTROID_K = 0.05; // 그룹 무게중심으로 끌리는 힘
const CENTER_K = 0.0006;
const DAMP = 0.955;
const JITTER = 46;
const NR = 4.6;
const OMEGA = 0.75; // 전체를 천천히 돌려 색 덩어리가 화면을 넓게 훑게 한다
const SEED = 0x327003;
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type RGB = [number, number, number];
const BOARD_LABEL = "색으로 나뉜 세 군집이 힘으로 뭉치는 관계도";
const META_LEFT = "노드 30개 · 3군집 · rAF";
const META_RIGHT = "arc · fillRect · 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 GraphForceClusterColor({
duration = "2s",
easing = "ease-out",
color = "#7c85a8",
n = N,
intraRepel = INTRA_REPEL,
interRepel = INTER_REPEL
}: GraphForceClusterColorProps) {
const rootRef = useRef<HTMLDivElement | null>(null);
const cvRef = useRef<HTMLCanvasElement | null>(null);
const ctxRef = useRef<CanvasRenderingContext2D | null>(null);
const colRef = useRef<{ panel: RGB; g: 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 grpRef = useRef<Int8Array | null>(null);
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 W = wRef.current, H = hRef.current;
const px = new Float32Array(n), py = new Float32Array(n);
const vx = new Float32Array(n), vy = new Float32Array(n);
const grp = new Int8Array(n);
for (let i = 0; i < n; i++) {
px[i] = rand() * W; py[i] = rand() * H;
grp[i] = i % GROUPS;
}
pxRef.current = px; pyRef.current = py; vxRef.current = vx; vyRef.current = vy;
grpRef.current = grp;
}
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, grp = grpRef.current;
const rand = randRef.current;
const W = wRef.current, H = hRef.current;
if (!px || !py || !vx || !vy || !grp) 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];
const d = Math.hypot(dx, dy) || 0.001;
let d2 = d * d; if (d2 < 4) d2 = 4;
const mult = grp[i] === grp[j] ? intraRepel : interRepel;
const f = (REPEL * mult) / d2;
const ux = dx / d, uy = dy / d;
fx[i] -= ux * f; fy[i] -= uy * f;
fx[j] += ux * f; fy[j] += uy * f;
}
}
// 그룹 무게중심 — 매 프레임 다시 구해 그 쪽으로 약하게 당긴다
const cxg = new Float32Array(GROUPS), cyg = new Float32Array(GROUPS), cnt = new Float32Array(GROUPS);
for (let i = 0; i < n; i++) { cxg[grp[i]] += px[i]; cyg[grp[i]] += py[i]; cnt[grp[i]]++; }
for (let g = 0; g < GROUPS; g++) { if (cnt[g] > 0) { cxg[g] /= cnt[g]; cyg[g] /= cnt[g]; } }
const cx = W / 2, cy = H / 2;
for (let m = 0; m < n; m++) {
fx[m] += (cxg[grp[m]] - px[m]) * CENTROID_K;
fy[m] += (cyg[grp[m]] - py[m]) * CENTROID_K;
fx[m] += (cx - px[m]) * CENTER_K;
fy[m] += (cy - py[m]) * CENTER_K;
fx[m] += (rand() - 0.5) * JITTER;
fy[m] += (rand() - 0.5) * JITTER;
vx[m] = (vx[m] + fx[m] * dt) * DAMP;
vy[m] = (vy[m] + fy[m] * dt) * DAMP;
px[m] += vx[m] * dt; py[m] += vy[m] * dt;
}
}
function rotateAll(dtheta: number): void {
const px = pxRef.current, py = pyRef.current;
if (!px || !py) return;
const W = wRef.current, H = hRef.current;
const cx = W / 2, cy = H / 2, cs = Math.cos(dtheta), sn = Math.sin(dtheta);
for (let i = 0; i < n; i++) {
const rx = px[i] - cx, ry = py[i] - cy;
px[i] = cx + rx * cs - ry * sn;
py[i] = cy + rx * sn + ry * cs;
}
}
function draw(t: number): void {
const ctx = ctxRef.current;
const col = colRef.current;
const px = pxRef.current, py = pyRef.current, grp = grpRef.current;
const W = wRef.current, H = hRef.current;
if (!ctx || !col || !px || !py || !grp) return;
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = "rgb(" + col.panel.join(",") + ")";
ctx.fillRect(0, 0, W, H);
for (let i = 0; i < n; i++) {
const c = col.g[grp[i]];
const pulse = 0.5 + 0.5 * Math.sin(t * 1.8 + i * 0.9);
ctx.fillStyle = "rgba(" + c.join(",") + "," + (0.16 + 0.18 * pulse).toFixed(3) + ")";
ctx.beginPath(); ctx.arc(px[i], py[i], NR + 5 + pulse * 6, 0, Math.PI * 2); ctx.fill();
ctx.fillStyle = "rgb(" + c.join(",") + ")";
ctx.beginPath(); ctx.arc(px[i], py[i], NR, 0, Math.PI * 2); ctx.fill();
}
}
function settle(): void {
for (let s = 0; s < 260; s++) step(1 / 60);
draw(0);
}
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);
rotateAll(OMEGA * dt);
draw(t);
rafRef.current = requestAnimationFrame(frame);
}
useEffect(() => {
const root = rootRef.current;
const cv = cvRef.current;
if (!root || !cv) return;
colRef.current = {
panel: tokRGB(root, "--gc-panel"),
g: [tokRGB(root, "--gc-g1"), tokRGB(root, "--gc-g2"), tokRGB(root, "--gc-g3")]
};
ctxRef.current = cv.getContext("2d");
fit();
function onResize(): void {
fit();
if (reduced()) settle();
}
window.addEventListener("resize", onResize);
if (reduced()) {
settle();
setStill(true);
} else {
t0 = -1; last = -1;
rafRef.current = requestAnimationFrame(frame);
}
return () => {
window.removeEventListener("resize", onResize);
cancelAnimationFrame(rafRef.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [n, intraRepel, interRepel]);
const rootCls = still ? `${styles.root} ${styles.isStill}` : styles.root;
return (
<main className={styles.stage}>
<div
className={rootCls}
ref={rootRef}
style={vars}
role="img"
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>
);
}03 군집 색 구분 — 그룹끼리 더 세게 당겨 뭉친다 — 측정 누적면적 55.851% · 움직인 프레임 23/23GODRICH 원작 · zip 에 SCSS·React 포함 · 네트워크 그래프 9종 — 힘으로 밀고 당겨 스스로 배치되는 캔버스
이 데모가 속한 글
네트워크 그래프 9종 — 힘으로 밀고 당겨 스스로 배치되는 캔버스네트워크 그래프는 점(노드)과 그 사이를 잇는 선(엣지)으로 관계를 나타낸 그림인데, 이 글은 그 점들의 자리를 사람이 정하지 않고 힘 계산만으로 매 프레임 다시 잡는 캔버스 엔진 아홉 판을 줍니다. 점과 선 몇 개 이 글의 데모 전부 보기

