02보로노이 셀 타일
흩뿌린 점마다 가장 가까운 영역을 칠해 유리 조각처럼 갈라진 면을 만듭니다.
쓰임가격표 섹션의 배경, 카드 뒤에 까는 장식 면
animation-delaytransform-boxtransform-origin

<main class="stage">
<div class="vo is-demo" id="root">
<svg class="vo__art" id="art" viewBox="0 0 240 150" role="img" data-label="흩뿌린 점마다 가장 가까운 영역을 칠한 셀 타일, 시드 {n}"></svg>
<div class="vo__bar">
<p class="vo__seed">
<svg xmlns="http://www.w3.org/2000/svg" class="vo__ic" viewBox="0 0 256 256" aria-hidden="true"><g fill="currentColor"><path d="M216 64v128a24 24 0 0 1-24 24H64a24 24 0 0 1-24-24V64a24 24 0 0 1 24-24h128a24 24 0 0 1 24 24" opacity=".2"/><path d="M192 32H64a32 32 0 0 0-32 32v128a32 32 0 0 0 32 32h128a32 32 0 0 0 32-32V64a32 32 0 0 0-32-32m16 160a16 16 0 0 1-16 16H64a16 16 0 0 1-16-16V64a16 16 0 0 1 16-16h128a16 16 0 0 1 16 16ZM104 92a12 12 0 1 1-12-12a12 12 0 0 1 12 12m72 0a12 12 0 1 1-12-12a12 12 0 0 1 12 12m-72 72a12 12 0 1 1-12-12a12 12 0 0 1 12 12m36-36a12 12 0 1 1-12-12a12 12 0 0 1 12 12m36 36a12 12 0 1 1-12-12a12 12 0 0 1 12 12"/></g></svg>
시드 <b id="num">7</b>
</p>
<button class="vo__btn" type="button" id="again">
<svg xmlns="http://www.w3.org/2000/svg" class="vo__ic" viewBox="0 0 256 256" aria-hidden="true"><g fill="currentColor"><path d="M107.14 98.8L128 128l-20.86 29.2A64 64 0 0 1 55.06 184H32V72h23.06a64 64 0 0 1 52.08 26.8m93.8-26.8a64 64 0 0 0-52.08 26.8L128 128l20.86 29.2a64 64 0 0 0 52.08 26.8H232V72Z" opacity=".2"/><path d="M237.66 178.34a8 8 0 0 1 0 11.32l-24 24a8 8 0 0 1-11.32-11.32L212.69 192h-11.75a72.12 72.12 0 0 1-58.59-30.15l-41.72-58.4A56.1 56.1 0 0 0 55.06 80H32a8 8 0 0 1 0-16h23.06a72.12 72.12 0 0 1 58.59 30.15l41.72 58.4A56.1 56.1 0 0 0 200.94 176h11.75l-10.35-10.34a8 8 0 0 1 11.32-11.32ZM143 107a8 8 0 0 0 11.16-1.86l1.2-1.67A56.1 56.1 0 0 1 200.94 80h11.75l-10.35 10.34a8 8 0 0 0 11.32 11.32l24-24a8 8 0 0 0 0-11.32l-24-24a8 8 0 0 0-11.32 11.32L212.69 64h-11.75a72.12 72.12 0 0 0-58.59 30.15l-1.2 1.67A8 8 0 0 0 143 107m-30 42a8 8 0 0 0-11.16 1.86l-1.2 1.67A56.1 56.1 0 0 1 55.06 176H32a8 8 0 0 0 0 16h23.06a72.12 72.12 0 0 0 58.59-30.15l1.2-1.67A8 8 0 0 0 113 149"/></g></svg>
<span class="vo__label">다시 생성</span>
</button>
<button class="vo__btn" type="button" id="copy" data-done="복사됨">
<svg xmlns="http://www.w3.org/2000/svg" class="vo__ic" viewBox="0 0 256 256" aria-hidden="true"><g fill="currentColor"><path d="M216 40v128h-48V88H88V40Z" opacity=".2"/><path d="M216 32H88a8 8 0 0 0-8 8v40H40a8 8 0 0 0-8 8v128a8 8 0 0 0 8 8h128a8 8 0 0 0 8-8v-40h40a8 8 0 0 0 8-8V40a8 8 0 0 0-8-8m-56 176H48V96h112Zm48-48h-32V88a8 8 0 0 0-8-8H96V48h112Z"/></g></svg>
<span class="vo__label">SVG 복사</span>
</button>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 보로노이 셀 타일 — 흩뿌린 점마다 바깥 사각형을 다른 점과의 수직이등분선으로 계속 잘라 셀을 남긴다.
// 면 생성은 JS, 연출은 CSS. 미리보기 루프는 셀이 **사라졌다 나타나는** 것이 아니라 한 칸씩 눌렸다
// 펴지는 물결이다 — 시작 상태와 끝 상태가 같은 완성 모자이크라 한 바퀴가 끝날 때 튀지 않고,
// poster(앞 장과 가장 크게 달라진 장)에도 빈 화면이 뽑히지 않는다.
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-blue;
@include stage($stage-paper);
.vo { display: grid; justify-items: center; gap: $sp-3; width: 100%; }
.vo__art { display: block; width: clamp(176px, 60vw, 320px); height: auto; }
// transform-box: fill-box 가 없으면 SVG 도형의 % 원점이 뷰포트 기준이 되어 셀이 제자리에서 안 커진다
.vo__cell {
stroke: $stage-paper;
stroke-width: 1.2;
transform-box: fill-box;
transform-origin: 50% 50%;
}
.vo__cell--a { fill: $color; }
.vo__cell--b { fill: $color; opacity: .46; }
.vo__cell--c { fill: $subject-ink; }
.vo__cell--d { fill: $subject-cream; }
.vo__bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: $sp-2; }
.vo__seed {
margin: 0;
display: inline-flex; align-items: center; gap: $sp-1;
color: rgba(23, 20, 26, .72);
font-weight: 700; font-size: clamp(9px, 2.4vw, 11px);
}
.vo__btn {
display: inline-flex; align-items: center; gap: $sp-1;
border: 0; border-radius: $r-pill;
padding: $sp-1 $sp-3;
background: $subject-ink; color: $subject-cream;
font-family: inherit; font-weight: 700; font-size: clamp(9px, 2.4vw, 11px);
cursor: pointer;
}
.vo__btn:focus-visible { outline: 3px solid $color; outline-offset: 2px; }
.vo__ic { width: 12px; height: 12px; flex: none; }
// ── 미리보기 루프: 셀 번호 순으로 물결이 지나간다. 같은 셀 크기로 시작해 같은 크기로 끝난다
.vo.is-demo .vo__cell {
animation-name: voPress;
animation-duration: $duration;
animation-timing-function: $easing;
animation-iteration-count: infinite;
animation-fill-mode: backwards;
animation-delay: calc(var(--i) * -46ms); // 음수 지연이라 첫 프레임부터 물결이 이미 퍼져 있다
}
// 사람이 「다시 생성」을 누르면 새 시드의 셀이 한 칸씩 펼쳐진다 — 루프와 달리 한 번만 돈다
.vo.is-draw .vo__cell {
animation-name: voPop;
animation-duration: $dur-enter;
animation-timing-function: $ease-pop;
animation-iteration-count: 1;
animation-fill-mode: backwards;
animation-delay: calc(var(--i) * 18ms);
}
@keyframes voPress {
0% { transform: scale(1); }
22% { transform: scale(.72); }
46% { transform: scale(1.04); }
70% { transform: scale(1); }
100% { transform: scale(1); }
}
@keyframes voPop {
from { transform: scale(.2); opacity: 0; }
to { transform: scale(1); opacity: 1; }
}
@media (max-width: 340px) {
.vo { gap: $sp-2; }
.vo__ic { width: 10px; height: 10px; }
}
@media (prefers-reduced-motion: reduce) {
.vo.is-demo .vo__cell,
.vo.is-draw .vo__cell { animation: none !important; transform: scale(1) !important; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ffffff;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.vo {
display: grid;
justify-items: center;
gap: 12px;
width: 100%;
}
.vo__art {
display: block;
width: clamp(176px, 60vw, 320px);
height: auto;
}
.vo__cell {
stroke: #ffffff;
stroke-width: 1.2;
transform-box: fill-box;
transform-origin: 50% 50%;
}
.vo__cell--a {
fill: #2f6df6;
}
.vo__cell--b {
fill: #2f6df6;
opacity: 0.46;
}
.vo__cell--c {
fill: #17141a;
}
.vo__cell--d {
fill: #fff7e6;
}
.vo__bar {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 8px;
}
.vo__seed {
margin: 0;
display: inline-flex;
align-items: center;
gap: 4px;
color: rgba(23, 20, 26, 0.72);
font-weight: 700;
font-size: clamp(9px, 2.4vw, 11px);
}
.vo__btn {
display: inline-flex;
align-items: center;
gap: 4px;
border: 0;
border-radius: 999px;
padding: 4px 12px;
background: #17141a;
color: #fff7e6;
font-family: inherit;
font-weight: 700;
font-size: clamp(9px, 2.4vw, 11px);
cursor: pointer;
}
.vo__btn:focus-visible {
outline: 3px solid #2f6df6;
outline-offset: 2px;
}
.vo__ic {
width: 12px;
height: 12px;
flex: none;
}
.vo.is-demo .vo__cell {
animation-name: voPress;
animation-duration: 2s;
animation-timing-function: cubic-bezier(0.2, 0.8, 0.2, 1);
animation-iteration-count: infinite;
animation-fill-mode: backwards;
animation-delay: calc(var(--i) * -46ms);
}
.vo.is-draw .vo__cell {
animation-name: voPop;
animation-duration: 600ms;
animation-timing-function: cubic-bezier(0.2, 1.4, 0.4, 1);
animation-iteration-count: 1;
animation-fill-mode: backwards;
animation-delay: calc(var(--i) * 18ms);
}
@keyframes voPress {
0% {
transform: scale(1);
}
22% {
transform: scale(0.72);
}
46% {
transform: scale(1.04);
}
70% {
transform: scale(1);
}
100% {
transform: scale(1);
}
}
@keyframes voPop {
from {
transform: scale(0.2);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
@media (max-width: 340px) {
.vo {
gap: 8px;
}
.vo__ic {
width: 10px;
height: 10px;
}
}
@media (prefers-reduced-motion: reduce) {
.vo.is-demo .vo__cell,
.vo.is-draw .vo__cell {
animation: none !important;
transform: scale(1) !important;
}
}var root = document.getElementById('root');
var art = document.getElementById('art');
var num = document.getElementById('num');
var W = 240, H = 150, SITES = 26;
var seed = 7;
// mulberry32 — 32비트 정수 시드 하나로 언제나 같은 난수열을 돌려준다. 재현 가능한 생성의 뿌리
function mulberry32(a) {
return function () {
a = a + 0x6D2B79F5 | 0;
var t = Math.imul(a ^ a >>> 15, 1 | a);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
// 두 점의 수직이등분선으로 다각형을 잘라 a 쪽 반평면만 남긴다 (서덜랜드-호지먼 클리핑)
function clip(poly, a, b) {
var mx = (a[0] + b[0]) / 2, my = (a[1] + b[1]) / 2;
var dx = b[0] - a[0], dy = b[1] - a[1], out = [], i;
function side(p) { return (p[0] - mx) * dx + (p[1] - my) * dy; }
for (i = 0; i < poly.length; i++) {
var p = poly[i], q = poly[(i + 1) % poly.length], sp = side(p), sq = side(q);
if (sp <= 0) { out.push(p); }
if ((sp < 0 && sq > 0) || (sp > 0 && sq < 0)) {
var t = sp / (sp - sq);
out.push([p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t]);
}
}
return out;
}
var TONES = ['a', 'b', 'c', 'd'];
function build() {
var rand = mulberry32(seed), pts = [], parts = [], i, j;
for (i = 0; i < SITES; i++) { pts.push([10 + rand() * (W - 20), 8 + rand() * (H - 16)]); }
for (i = 0; i < SITES; i++) {
var poly = [[0, 0], [W, 0], [W, H], [0, H]];
for (j = 0; j < SITES && poly.length; j++) { if (j !== i) { poly = clip(poly, pts[i], pts[j]); } }
if (poly.length < 3) { continue; }
var tone = TONES[Math.min(3, Math.floor(rand() * 4))];
var d = poly.map(function (p) { return p[0].toFixed(1) + ',' + p[1].toFixed(1); }).join(' ');
parts.push('<polygon class="vo__cell vo__cell--' + tone + '" style="--i:' + i + '" points="' + d + '"/>');
}
art.innerHTML = parts.join('');
art.setAttribute('aria-label', art.getAttribute('data-label').replace('{n}', seed));
num.textContent = seed;
}
document.getElementById('again').addEventListener('click', function () {
seed = (seed * 7 + 13) % 991 + 1;
build();
root.classList.remove('is-demo');
root.classList.remove('is-draw');
void art.getBoundingClientRect().width; // 한 번 읽어 리플로를 일으켜야 같은 애니메이션이 처음부터 다시 돈다
root.classList.add('is-draw');
});
var copyBtn = document.getElementById('copy');
copyBtn.addEventListener('click', function () {
var label = copyBtn.querySelector('.vo__label');
var before = label.textContent;
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(art.outerHTML).catch(function () {});
}
label.textContent = copyBtn.getAttribute('data-done');
setTimeout(function () { label.textContent = before; }, 1200);
});
build();// VoronoiCellTiles — vanilla 판(run/293/demo/02-generative-voronoi-cells/index.html) 의 React 포트.
// 흩뿌린 점마다 바깥 사각형을 "다른 점과의 수직이등분선" 으로 계속 잘라, 남은 조각이 그 점의 셀이다.
// 면 생성은 JS, 연출은 CSS. 시드·점 개수·자르는 순서는 vanilla 와 같은 값이다.
import { useMemo, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./VoronoiCellTiles.module.scss";
export type VoronoiCellTilesProps = {
duration?: number;
easing?: string;
color?: string;
label?: string;
seedText?: string;
regenerateText?: string;
copyText?: string;
copiedText?: string;
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
// useRef 는 T | null 을 돌려준다. 처음부터 값이 있고 절대 비지 않는 상자는 이렇게 좁혀 쓴다
type Box<T> = { current: T };
type Point = [number, number];
type Cell = { i: number; tone: string; points: string };
const W = 240;
const H = 150;
const SITES = 26;
// 네 가지 칠. 뽑는 순서와 확률은 vanilla 의 'a' 'b' 'c' 'd' 와 같고 이름만 CSS 모듈식이다
const TONES: string[] = ["cellA", "cellB", "cellC", "cellD"];
// mulberry32 — 32비트 정수 시드 하나로 언제나 같은 난수열을 돌려준다. 재현 가능한 생성의 뿌리
function mulberry32(a: number): () => number {
return function (): number {
a = a + 0x6D2B79F5 | 0;
let t = Math.imul(a ^ a >>> 15, 1 | a);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
// 두 점의 수직이등분선으로 다각형을 잘라 a 쪽 반평면만 남긴다 (서덜랜드-호지먼 클리핑)
function clip(poly: Point[], a: Point, b: Point): Point[] {
const mx = (a[0] + b[0]) / 2;
const my = (a[1] + b[1]) / 2;
const dx = b[0] - a[0];
const dy = b[1] - a[1];
const out: Point[] = [];
function side(p: Point): number { return (p[0] - mx) * dx + (p[1] - my) * dy; }
for (let i = 0; i < poly.length; i++) {
const p = poly[i];
const q = poly[(i + 1) % poly.length];
const sp = side(p);
const sq = side(q);
if (sp <= 0) { out.push(p); }
if ((sp < 0 && sq > 0) || (sp > 0 && sq < 0)) {
const t = sp / (sp - sq);
out.push([p[0] + (q[0] - p[0]) * t, p[1] + (q[1] - p[1]) * t]);
}
}
return out;
}
export default function VoronoiCellTiles({
duration = 2000,
easing = "cubic-bezier(.2, .8, .2, 1)",
color = "#2f6df6",
label = "Cell tiles where each scattered point owns its nearest area, seed {n}",
seedText = "Seed",
regenerateText = "Regenerate",
copyText = "Copy SVG",
copiedText = "Copied",
}: VoronoiCellTilesProps) {
const artRef = useRef<SVGSVGElement | null>(null);
const timerRef = useRef<number>(0) as Box<number>;
const [seed, setSeed] = useState(7);
const [mode, setMode] = useState("demo");
const [copyLabel, setCopyLabel] = useState(copyText);
const vars: Vars = { "--duration": `${duration}ms`, "--easing": easing, "--color": color };
// 시드가 바뀔 때만 다시 계산한다 — 같은 시드면 언제나 같은 셀이 나온다.
// 칠(tone)을 뽑는 rand() 는 셀이 살아남은 뒤에만 부른다: vanilla 와 난수 소비 순서를 맞춰야
// 같은 시드에서 두 판의 색이 어긋나지 않는다
const cells = useMemo(() => {
const rand = mulberry32(seed);
const pts: Point[] = [];
const out: Cell[] = [];
for (let i = 0; i < SITES; i++) { pts.push([10 + rand() * (W - 20), 8 + rand() * (H - 16)]); }
for (let i = 0; i < SITES; i++) {
let poly: Point[] = [[0, 0], [W, 0], [W, H], [0, H]];
for (let j = 0; j < SITES && poly.length > 0; j++) { if (j !== i) { poly = clip(poly, pts[i], pts[j]); } }
if (poly.length < 3) { continue; }
const tone = TONES[Math.min(3, Math.floor(rand() * 4))];
const d = poly.map((p: Point) => p[0].toFixed(1) + "," + p[1].toFixed(1)).join(" ");
out.push({ i, tone, points: d });
}
return out;
}, [seed]);
function regenerate(): void {
setSeed((prev: number) => (prev * 7 + 13) % 991 + 1);
setMode("draw");
}
function copySvg(): void {
const art = artRef.current;
if (!art) { return; }
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(art.outerHTML).catch(function () {});
}
setCopyLabel(copiedText);
clearTimeout(timerRef.current);
timerRef.current = window.setTimeout(() => setCopyLabel(copyText), 1200);
}
const rootClass = `${styles.vo} ${mode === "demo" ? styles.isDemo : styles.isDraw}`;
return (
<div className={rootClass} style={vars}>
<svg
ref={artRef}
className={styles.art}
viewBox={`0 0 ${W} ${H}`}
role="img"
aria-label={label.replace("{n}", String(seed))}
>
{cells.map((c: Cell) => (
<polygon
key={c.i}
className={`${styles.cell} ${styles[c.tone]}`}
style={{ "--i": c.i } as CSSProperties}
points={c.points}
/>
))}
</svg>
<div className={styles.bar}>
<p className={styles.seed}>{seedText} <b>{seed}</b></p>
<button className={styles.btn} type="button" onClick={regenerate}>{regenerateText}</button>
<button className={styles.btn} type="button" onClick={copySvg}>{copyLabel}</button>
</div>
</div>
);
}02 보로노이 셀 타일 — 측정 누적면적 19.066% · 움직인 프레임 23/23GODRICH 원작 · zip 에 SCSS·React 포함 · CSS 배경 패턴 9종 — 시드 하나로 다시 그리기
이 데모가 속한 글
CSS 배경 패턴 9종 — 시드 하나로 다시 그리기css 배경 패턴을 이미지 파일로 깔면 화면마다 같은 그림이 나옵니다. 여기 아홉 개는 숫자 하나(시드)를 넣으면 그 자리에서 무늬를 새로 계산해 SVG 로 뱉고, 같은 숫자를 다시 넣으면 똑같은 그림이 돌아옵니다. 이 글의 데모 전부 보기


