06Hex grid painting
Hexagons are laid like a honeycomb and each one takes a color step from its position and a random draw.
Best forBackground for an empty dashboard state, a header band on a statistics page
polygonscaleanimation-delay

<main class="stage">
<div class="hx is-demo" id="root">
<svg class="hx__art" id="art" viewBox="0 0 240 150" role="img" data-label="A honeycomb grid colored by position and a random draw, seed {n}"></svg>
<div class="hx__bar">
<p class="hx__seed">
<svg xmlns="http://www.w3.org/2000/svg" class="hx__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>
Seed <b id="num">7</b>
</p>
<button class="hx__btn" type="button" id="again">
<svg xmlns="http://www.w3.org/2000/svg" class="hx__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="hx__label">Regenerate</span>
</button>
<button class="hx__btn" type="button" id="copy" data-done="Copied">
<svg xmlns="http://www.w3.org/2000/svg" class="hx__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="hx__label">Copy SVG</span>
</button>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 육각 격자 색칠 — 자리(왼쪽에서 오른쪽으로 기운 확률)와 난수를 섞어 칸마다 색 단계를 고른다.
// 미리보기 루프는 대각선을 타고 지나가는 물결이라 시작과 끝이 같은 완성 격자다 —
// 칸이 사라지지 않으니 루프 끝에서 튀지 않고 poster 에도 빈 화면이 안 뽑힌다.
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-cream;
@include stage($stage-orange);
.hx { display: grid; justify-items: center; gap: $sp-3; width: 100%; }
.hx__art { display: block; width: clamp(176px, 60vw, 320px); height: auto; }
// transform-box: fill-box 가 없으면 % 원점이 SVG 뷰포트 기준이라 칸이 제자리에서 안 커진다
.hx__cell {
stroke: $stage-orange;
stroke-width: 1.6;
transform-box: fill-box;
transform-origin: 50% 50%;
}
.hx__cell--a { fill: $color; }
.hx__cell--b { fill: $color; opacity: .54; }
.hx__cell--c { fill: $subject-blue; }
.hx__cell--d { fill: $subject-ink; }
.hx__bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: $sp-2; }
.hx__seed {
margin: 0;
display: inline-flex; align-items: center; gap: $sp-1;
color: $subject-ink;
font-weight: 700; font-size: clamp(9px, 2.4vw, 11px);
}
.hx__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;
}
.hx__btn:focus-visible { outline: 3px solid $subject-cream; outline-offset: 2px; }
.hx__ic { width: 12px; height: 12px; flex: none; }
// ── 미리보기 루프: 대각선을 따라 칸이 한 번씩 눌렸다 펴진다
.hx.is-demo .hx__cell {
animation-name: hxWave;
animation-duration: $duration;
animation-timing-function: $easing;
animation-iteration-count: infinite;
animation-fill-mode: backwards;
animation-delay: calc(var(--i) * -108ms); // 음수 지연이라 첫 프레임부터 물결이 이미 퍼져 있다
}
// 사람이 「다시 생성」을 누르면 새 시드의 격자가 대각선 순으로 펼쳐진다
.hx.is-draw .hx__cell {
animation-name: hxPop;
animation-duration: $dur-enter;
animation-timing-function: $ease-pop;
animation-iteration-count: 1;
animation-fill-mode: backwards;
animation-delay: calc(var(--i) * 22ms);
}
@keyframes hxWave {
0% { transform: scale(1); }
26% { transform: scale(.58); }
52% { transform: scale(1.06); }
78% { transform: scale(1); }
100% { transform: scale(1); }
}
@keyframes hxPop {
from { transform: scale(.2); opacity: 0; }
to { transform: scale(1); opacity: 1; }
}
@media (max-width: 340px) {
.hx { gap: $sp-2; }
.hx__ic { width: 10px; height: 10px; }
}
@media (prefers-reduced-motion: reduce) {
.hx.is-demo .hx__cell,
.hx.is-draw .hx__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: #ff4d1f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.hx {
display: grid;
justify-items: center;
gap: 12px;
width: 100%;
}
.hx__art {
display: block;
width: clamp(176px, 60vw, 320px);
height: auto;
}
.hx__cell {
stroke: #ff4d1f;
stroke-width: 1.6;
transform-box: fill-box;
transform-origin: 50% 50%;
}
.hx__cell--a {
fill: #fff7e6;
}
.hx__cell--b {
fill: #fff7e6;
opacity: 0.54;
}
.hx__cell--c {
fill: #2f6df6;
}
.hx__cell--d {
fill: #17141a;
}
.hx__bar {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 8px;
}
.hx__seed {
margin: 0;
display: inline-flex;
align-items: center;
gap: 4px;
color: #17141a;
font-weight: 700;
font-size: clamp(9px, 2.4vw, 11px);
}
.hx__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;
}
.hx__btn:focus-visible {
outline: 3px solid #fff7e6;
outline-offset: 2px;
}
.hx__ic {
width: 12px;
height: 12px;
flex: none;
}
.hx.is-demo .hx__cell {
animation-name: hxWave;
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) * -108ms);
}
.hx.is-draw .hx__cell {
animation-name: hxPop;
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) * 22ms);
}
@keyframes hxWave {
0% {
transform: scale(1);
}
26% {
transform: scale(0.58);
}
52% {
transform: scale(1.06);
}
78% {
transform: scale(1);
}
100% {
transform: scale(1);
}
}
@keyframes hxPop {
from {
transform: scale(0.2);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
@media (max-width: 340px) {
.hx {
gap: 8px;
}
.hx__ic {
width: 10px;
height: 10px;
}
}
@media (prefers-reduced-motion: reduce) {
.hx.is-demo .hx__cell,
.hx.is-draw .hx__cell {
animation: none !important;
transform: scale(1) !important;
}
}var root = document.getElementById('root');
var art = document.getElementById('art');
var num = document.getElementById('num');
var R = 15, COLS = 11, ROWS = 7;
var HSTEP = R * 1.5, VSTEP = R * Math.sqrt(3);
var TONES = ['a', 'b', 'c', 'd'];
var seed = 7;
// mulberry32 — 32비트 정수 Seed 하나로 언제나 같은 난수열을 돌려준다. 재현 가능한 생성의 뿌리
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;
};
}
// 납작한 육각형 여섯 꼭짓점 — 0도부터 60도씩
function hexPoints(cx, cy) {
var pts = [], k;
for (k = 0; k < 6; k++) {
var a = k * Math.PI / 3;
pts.push((cx + R * Math.cos(a)).toFixed(1) + ',' + (cy + R * Math.sin(a)).toFixed(1));
}
return pts.join(' ');
}
function build() {
var rand = mulberry32(seed), parts = [], col, row;
for (col = 0; col < COLS; col++) {
for (row = 0; row < ROWS; row++) {
var cx = col * HSTEP, cy = row * VSTEP + (col % 2 ? VSTEP / 2 : 0) - VSTEP / 2;
// 색 단계는 왼쪽에서 오른쪽으로 기운 확률에 난수를 섞어 고른다 — 자리가 결과에 남는다
var bias = col / (COLS - 1) * 1.6 + rand() * 2.4;
var tone = TONES[Math.max(0, Math.min(3, Math.floor(bias)))];
parts.push('<polygon class="hx__cell hx__cell--' + tone + '" style="--i:' + (col + row) +
'" points="' + hexPoints(cx, cy) + '"/>');
}
}
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('.hx__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();// HexGridPaint — vanilla 판(run/293/demo/06-generative-hex-grid-paint/index.html) 의 React 포트.
// 벌집처럼 깔린 육각형을 「자리(왼쪽→오른쪽으로 기운 확률) + 난수」로 칠한다. 자리가 결과에 남는다.
// 시드·반지름·격자 수·기울기(1.6 / 2.4)는 vanilla 와 같은 값이다.
import { useMemo, useRef, useState } from "react";
import type { CSSProperties } from "react";
import styles from "./HexGridPaint.module.scss";
export type HexGridPaintProps = {
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 Cell = { points: string; tone: number; i: number };
const W = 240;
const H = 150;
const R = 15;
const COLS = 11;
const ROWS = 7;
const HSTEP = R * 1.5;
const VSTEP = R * Math.sqrt(3);
// 색 단계 네 가지 — vanilla 의 TONES = ['a','b','c','d'] 와 차례가 같다
const TONES: string[] = ["toneA", "toneB", "toneC", "toneD"];
// 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;
};
}
// 납작한 육각형 여섯 꼭짓점 — 0도부터 60도씩
function hexPoints(cx: number, cy: number): string {
const pts: string[] = [];
for (let k = 0; k < 6; k++) {
const a = k * Math.PI / 3;
pts.push((cx + R * Math.cos(a)).toFixed(1) + "," + (cy + R * Math.sin(a)).toFixed(1));
}
return pts.join(" ");
}
function build(s: number): Cell[] {
const rand = mulberry32(s);
const out: Cell[] = [];
for (let col = 0; col < COLS; col++) {
for (let row = 0; row < ROWS; row++) {
const cx = col * HSTEP;
const cy = row * VSTEP + (col % 2 ? VSTEP / 2 : 0) - VSTEP / 2;
// 색 단계는 왼쪽에서 오른쪽으로 기운 확률에 난수를 섞어 고른다 — 자리가 결과에 남는다
const bias = col / (COLS - 1) * 1.6 + rand() * 2.4;
const tone = Math.max(0, Math.min(3, Math.floor(bias)));
out.push({ points: hexPoints(cx, cy), tone: tone, i: col + row });
}
}
return out;
}
export default function HexGridPaint({
duration = 2000,
easing = "cubic-bezier(.2, .8, .2, 1)",
color = "#fff7e6",
label = "A honeycomb grid colored by position and a random draw, seed {n}",
seedText = "Seed",
regenerateText = "Regenerate",
copyText = "Copy SVG",
copiedText = "Copied",
}: HexGridPaintProps) {
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 };
// 시드가 바뀔 때만 다시 계산한다 — 같은 시드면 언제나 같은 격자가 나온다
const cells = useMemo(() => build(seed), [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.hx} ${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, k: number) => (
<polygon
key={k}
className={`${styles.cell} ${styles[TONES[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>
);
}06 Hex grid painting — Measured area 25.931% · moving frames 23/23Original by GODRICH · SCSS and React ship in the zip · CSS Background Pattern Generator: 9 Seeded Scenes
The post this demo lives in
CSS Background Pattern Generator: 9 Seeded ScenesA CSS background pattern generator turns one number into a drawing instead of an image file. Feed these nine the same se See all demos in this post


