08倍音スライダー — 円の数を減らすと絵がどう滲むか
使う円の数をスライダーで1から全体まで動かすと、ハートが水滴からくっきりしたハートへ蘇ります。JPEGや音楽圧縮と同じ『どこまで減らしても本質が残るか』の実験です。
使いどころ教育用インタラクティブ、ローディングの気の利いた使い方
input[type=range]CanvasRenderingContext2DrequestAnimationFrame

<main class="stage">
<div class="fe is-demo" id="feRoot">
<p class="fe__hint">
<span>使う円の数をスライダーで1から全体まで動かすと、ハートが水滴からくっきりしたハートへ蘇ります。JPEGや音楽圧縮と同じ、どこまで減らしても本質が残るかの実験です</span>
</p>
<div class="fe__field" id="feField" role="application" aria-label="倍音スライダー 実験版" tabindex="0">
<canvas class="fe__cv" id="feCv"></canvas>
</div>
<p class="fe__panel">
<label class="fe__label" for="feSlider">円の数</label>
<input class="fe__slider" id="feSlider" type="range" min="1" max="64" step="1" value="1" aria-label="円の数">
</p>
<div class="fe__bar">
<span class="fe__meta"><b class="fe__val" id="feN">1</b><span id="feUnit">使用中・ハート再合成</span></span>
<span class="fe__meta fe__meta--dim">canvas · truncated series · slider</span>
</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 배음 슬라이더 실험(08). @keyframes 를 두지 않는다 —
// 움직임은 전부 캔버스 rAF 루프(하트 DFT 계수 체인 → 원 개수 변화)가 만든다.
// 무대는 심야 관측 팔레트. 파스텔 주색을 --fe-* 로 편 전체(01~09) 공통 규격.
$duration: $dur-loop;
$easing: $ease-spring;
$color: $subject-lilac;
// ── 편 공통 심야 관측 팔레트(BD9~11 원칙: 편마다 다시 정하지 않는다) ──
$fe-stage: #141824; // 심야 그래프지 — 저채도 짙은 남색 (파스텔 검사 무관: 저채도·저명도)
@include stage($fe-stage);
$fe-ink: #10141f; // 필드 바닥 — 무대보다 한 톤 어두운 남흑
$fe-glow: #b7c8ff; // 궤적 주색 — 파스텔 페리윙클(고명도라 vivid 아님)
$fe-hot: #ffe9c4; // 펜 절정 — 파스텔 크림
$fe-text: #d6d9e6; // 라벨 글자 — 밝은 회청
.fe {
// 렌더러가 읽는 색 — JS 는 getComputedStyle 로 --fe-* 를 받아 쓴다
--fe-ink: #{$fe-ink};
--fe-glow: #{$fe-glow};
--fe-hot: #{$fe-hot};
display: flex; flex-direction: column; gap: $sp-2;
width: 100%; max-width: 424px;
}
// 좁은 폭(320px) — 슬라이더 패널 한 줄까지 넣고도 무대 안에 들어오게 여백을 당긴다
@media (max-width: 359px) {
.fe { gap: $sp-1; }
}
.fe__hint {
display: flex; align-items: center; gap: $sp-1; margin: 0;
font-weight: 700; font-size: clamp(10px, 2.8vw, 12px); line-height: 1.35;
color: $fe-text;
}
.fe__field {
position: relative; height: 140px; // 슬라이더 패널 한 줄이 들어와 필드는 낮게(480x300·320px 넘침 방지)
border-radius: $r-card; overflow: hidden;
background: $fe-ink;
box-shadow: inset 0 0 0 1px rgba($fe-text, .18);
}
.fe__field:focus-visible { outline: 2px solid $fe-glow; outline-offset: 2px; }
.fe__cv { display: block; width: 100%; height: 100%; }
// 슬라이더 — 원 개수를 1~전체로 움직인다. 라벨 글자는 밝은 회청(ink 면 위 대비 확보)
.fe__panel {
display: flex; align-items: center; gap: $sp-2; margin: 0;
}
.fe__label { font-size: 10px; font-weight: 700; color: $fe-text; white-space: nowrap; }
.fe__panel input[type=range] {
flex: 1 1 auto; -webkit-appearance: none; appearance: none;
height: 4px; border-radius: $r-pill; margin: 0;
background: rgba($fe-glow, .24);
}
.fe__panel input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none; appearance: none;
width: 14px; height: 14px; border-radius: 50%;
background: $fe-hot; border: 0; cursor: pointer;
}
.fe__panel input[type=range]::-moz-range-thumb {
width: 14px; height: 14px; border-radius: 50%;
background: $fe-hot; border: 0; cursor: pointer;
}
.fe__panel input[type=range]:focus-visible { outline: 2px solid $fe-glow; outline-offset: 2px; }
.fe__bar { display: flex; align-items: baseline; justify-content: space-between; gap: $sp-2; }
.fe__meta { font-size: 10px; font-weight: 500; color: rgba($fe-text, .72); }
.fe__meta--dim { font-size: 10px; }
@media (max-width: 340px) {
.fe { gap: $sp-1; }
.fe__field { height: 84px; }
.fe__meta--dim { display: none; }
}
// reduce — rAF 를 돌리지 않고 궤적이 다 쌓인 한 장만 남는다.
@media (prefers-reduced-motion: reduce) {
.fe.is-still .fe__field { 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: #141824;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.fe {
--fe-ink: #10141f;
--fe-glow: #b7c8ff;
--fe-hot: #ffe9c4;
display: flex;
flex-direction: column;
gap: 8px;
width: 100%;
max-width: 424px;
}
@media (max-width: 359px) {
.fe {
gap: 4px;
}
}
.fe__hint {
display: flex;
align-items: center;
gap: 4px;
margin: 0;
font-weight: 700;
font-size: clamp(10px, 2.8vw, 12px);
line-height: 1.35;
color: #d6d9e6;
}
.fe__field {
position: relative;
height: 140px;
border-radius: 16px;
overflow: hidden;
background: #10141f;
box-shadow: inset 0 0 0 1px rgba(214, 217, 230, 0.18);
}
.fe__field:focus-visible {
outline: 2px solid #b7c8ff;
outline-offset: 2px;
}
.fe__cv {
display: block;
width: 100%;
height: 100%;
}
.fe__panel {
display: flex;
align-items: center;
gap: 8px;
margin: 0;
}
.fe__label {
font-size: 10px;
font-weight: 700;
color: #d6d9e6;
white-space: nowrap;
}
.fe__panel input[type=range] {
flex: 1 1 auto;
-webkit-appearance: none;
appearance: none;
height: 4px;
border-radius: 999px;
margin: 0;
background: rgba(183, 200, 255, 0.24);
}
.fe__panel input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 14px;
height: 14px;
border-radius: 50%;
background: #ffe9c4;
border: 0;
cursor: pointer;
}
.fe__panel input[type=range]::-moz-range-thumb {
width: 14px;
height: 14px;
border-radius: 50%;
background: #ffe9c4;
border: 0;
cursor: pointer;
}
.fe__panel input[type=range]:focus-visible {
outline: 2px solid #b7c8ff;
outline-offset: 2px;
}
.fe__bar {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
}
.fe__meta {
font-size: 10px;
font-weight: 500;
color: rgba(214, 217, 230, 0.72);
}
.fe__meta--dim {
font-size: 10px;
}
@media (max-width: 340px) {
.fe {
gap: 4px;
}
.fe__field {
height: 84px;
}
.fe__meta--dim {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.fe.is-still .fe__field {
cursor: default !important;
}
}// 배음 슬라이더 실험(08). 몇 개의 원만 남겨도 본떼가 남는가 — 계수 절약(압축)의 실험판.
// ① 하트 곡선(04 와 동일)의 DFT 계수를 크기 순으로 준비한다.
// ② 슬라이더(1~64)가 체인에 얹을 계수 개수를 정한다. 1개면 원 하나 — 물방울 한 바퀴.
// ③ 늘릴수록 디테일이 되살아나 오목한 하트가 완성된다 — JPEG·MP3 의 "버려도 되는 계수"와 같은 원리.
// 자동 모드(.is-demo)에서는 슬라이더 값이 스스로 1→64→1 로 왕복해 렌더·측정이 가능하다.
// 사람이 직접 잡으면 자동 왕복을 멈춘다(pointerdown).
//
// 화면이 사는 장치 — 077 기준:
// · 원 개수가 매 순간 변하므로 체인·궤적 전체가 매 프레임 다시 계산된다.
// · 밑바탕 방사 스윕·스포크가 매 프레임 재계산된다. 궤적은 투명 캔버스에 쌓이며 혜성 감쇠.
(function () {
// ── 변형 상수 블록 ──
var N = 256; // 경로 샘플 수
var MAXSHOW = 64; // 슬라이더 최대(체인에 얹을 계수 수)
var SWEEP = 3.2; // 자동 왕복 주기(초)
// ──────────────────
var TAU = Math.PI * 2;
var root = document.getElementById('feRoot');
var field = document.getElementById('feField');
var cv = document.getElementById('feCv');
var slider = document.getElementById('feSlider');
var nOut = document.getElementById('feN');
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_INK = tokRGB('--fe-ink');
var C_GLOW = tokRGB('--fe-glow');
var C_HOT = tokRGB('--fe-hot');
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var raf = 0, t0 = -1;
var W = 0, H = 0, dpr = 1;
var trailCv, trailCtx;
function heartPath(u) {
var t = u * TAU;
var x = 16 * Math.pow(Math.sin(t), 3);
var y = -(13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t));
return { x: x / 17, y: y / 17 };
}
function dft(path) {
var M = path.length;
var out = [];
for (var k = 0; k < M; k++) {
var freq = (k <= M / 2) ? k : k - M;
var re = 0, im = 0;
for (var n = 0; n < M; n++) {
var a = -TAU * k * n / M;
var c = Math.cos(a), s = Math.sin(a);
re += path[n].x * c - path[n].y * s;
im += path[n].x * s + path[n].y * c;
}
re /= M; im /= M;
out.push({ freq: freq, re: re, im: im, amp: Math.sqrt(re * re + im * im) });
}
out.sort(function (a, b) { return b.amp - a.amp; });
return out;
}
var PATH = [], COEF = [];
function fit() {
var r = field.getBoundingClientRect();
W = Math.round(r.width); H = Math.round(r.height);
dpr = Math.min(2, window.devicePixelRatio || 1);
cv.width = W * dpr; cv.height = H * dpr;
cv.style.width = '100%'; cv.style.height = '100%';
trailCv = document.createElement('canvas');
trailCv.width = W * dpr; trailCv.height = H * dpr;
trailCtx = trailCv.getContext('2d');
trailCtx.scale(dpr, dpr);
if (!PATH.length) {
for (var i = 0; i < N; i++) PATH.push(heartPath(i / N));
COEF = dft(PATH);
}
}
function rgba(c, a) { return 'rgba(' + c[0] + ',' + c[1] + ',' + c[2] + ',' + a + ')'; }
function chainAt(u, show) {
var base = Math.min(W, H) * 0.4;
var cx = W * 0.5, cy = H * 0.5;
var pts = [{ x: cx, y: cy }];
for (var i = 0; i < show && i < COEF.length; i++) {
var c = COEF[i];
var ph = Math.atan2(c.im, c.re);
var ang = c.freq * u * TAU + ph;
pts.push({
x: pts[i].x + Math.cos(ang) * c.amp * base,
y: pts[i].y + Math.sin(ang) * c.amp * base
});
}
return pts;
}
function drawGrid(t) {
var ctx = cv.getContext('2d');
var cx = W * 0.5, cy = H * 0.5;
var maxR = Math.sqrt(W * W + H * H) * 0.5;
var sweep = t * 0.85;
for (var b = 0; b < 18; b++) {
ctx.fillStyle = rgba(C_GLOW, 0.095 * (1 - b / 18));
ctx.beginPath();
ctx.moveTo(cx, cy);
ctx.arc(cx, cy, maxR, sweep - (b + 1) * 0.055, sweep - b * 0.055);
ctx.closePath();
ctx.fill();
}
var spin = t * 0.28;
for (var i = 0; i < 16; i++) {
var a = spin + (i / 16) * TAU;
ctx.strokeStyle = rgba(C_GLOW, i % 2 === 0 ? 0.095 : 0.055);
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(cx + Math.cos(a) * 12, cy + Math.sin(a) * 12);
ctx.lineTo(cx + Math.cos(a) * maxR, cy + Math.sin(a) * maxR);
ctx.stroke();
}
}
function draw(u, pulse, show, gt) {
var ctx = cv.getContext('2d');
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.fillStyle = 'rgb(' + C_INK[0] + ',' + C_INK[1] + ',' + C_INK[2] + ')';
ctx.fillRect(0, 0, W, H);
drawGrid(gt);
ctx.drawImage(trailCv, 0, 0, W, H);
var base = Math.min(W, H) * 0.4;
var pts = chainAt(u, show);
for (var i = 0; i < pts.length - 1; i++) {
var r = COEF[i].amp * base;
if (r > 2) {
ctx.strokeStyle = rgba(C_GLOW, 0.09 + 0.01 * i);
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(pts[i].x, pts[i].y, r, 0, TAU);
ctx.stroke();
}
if (i < 4 || r > 3) {
ctx.strokeStyle = rgba(C_GLOW, 0.46);
ctx.beginPath();
ctx.moveTo(pts[i].x, pts[i].y);
ctx.lineTo(pts[i + 1].x, pts[i + 1].y);
ctx.stroke();
}
}
var pen = pts[pts.length - 1];
var pr = 2.4 + pulse * 2.2;
ctx.fillStyle = rgba(C_HOT, 0.3);
ctx.beginPath();
ctx.arc(pen.x, pen.y, pr * 2.3, 0, TAU);
ctx.fill();
ctx.fillStyle = rgba(C_HOT, 1);
ctx.beginPath();
ctx.arc(pen.x, pen.y, pr, 0, TAU);
ctx.fill();
return pen;
}
var prevPen = null;
function stampTrail(pen) {
if (!prevPen) { prevPen = pen; return; }
trailCtx.strokeStyle = rgba(C_HOT, 0.8);
trailCtx.lineWidth = 1.7;
trailCtx.lineCap = 'round';
trailCtx.beginPath();
trailCtx.moveTo(prevPen.x, prevPen.y);
trailCtx.lineTo(pen.x, pen.y);
trailCtx.stroke();
trailCtx.strokeStyle = rgba(C_HOT, 0.12);
trailCtx.lineWidth = 4.6;
trailCtx.stroke();
prevPen = pen;
}
function decayTrail() {
trailCtx.setTransform(1, 0, 0, 1, 0, 0);
trailCtx.globalCompositeOperation = 'destination-out';
trailCtx.fillStyle = 'rgba(0,0,0,0.03)';
trailCtx.fillRect(0, 0, W * dpr, H * dpr);
trailCtx.globalCompositeOperation = 'source-over';
trailCtx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
var autoMode = true;
function frame(now) {
if (t0 < 0) t0 = now;
var lt = (now - t0) / 1000;
var u = (lt * 0.55) % 1;
var pulse = 0.5 + 0.5 * Math.sin(lt * 8.0);
var show;
if (autoMode) {
// 자동 왕복 — 1→64→1. 사람이 잡으면 멈춘다
var tri = 0.5 - 0.5 * Math.cos(lt * TAU / SWEEP);
show = 1 + Math.round(tri * (MAXSHOW - 1));
if (slider && String(slider.value) !== String(show)) slider.value = String(show);
} else {
show = slider ? parseInt(slider.value, 10) : MAXSHOW;
}
decayTrail();
var pen = draw(u, pulse, show, lt * TAU);
stampTrail(pen);
if (nOut) nOut.textContent = String(show);
raf = requestAnimationFrame(frame);
}
if (slider) {
slider.addEventListener('pointerdown', function () { autoMode = false; });
slider.addEventListener('input', function () {
autoMode = false;
prevPen = null;
if (nOut) nOut.textContent = slider.value;
});
slider.addEventListener('keydown', function () { autoMode = false; });
}
function stillFrame() {
// reduce — 원 8개 궤적을 미리 쌓은 뒤 한 장 고정
var show = 8;
var prev = chainAt(0, show)[show];
for (var i = 1; i <= 200; i++) {
var pen = chainAt(i / 200, show)[show];
stampTrail(pen);
prev = pen;
}
draw(0.5, 0.5, show, 2.1);
}
window.addEventListener('resize', function () {
fit(); prevPen = null;
if (reduce) stillFrame();
});
fit();
if (reduce) {
stillFrame();
root.classList.add('is-still');
} else {
raf = requestAnimationFrame(frame);
}
})();// FourierHarmonicSliderLab — vanilla 판(run/321/demo/08-fourier-harmonic-slider-lab/index.html) 의 React 포트.
// 배음 슬라이더 실험 — 몇 개의 원만 남겨도 본떼가 남는가(계수 절약·압축)의 실험판.
// ① 하트 곡선(04 와 동일)의 DFT 계수를 크기 순으로 준비한다.
// ② 슬라이더(1~64)가 체인에 얹을 계수 개수를 정한다. 1개면 원 하나 — 물방울 한 바퀴.
// ③ 늘릴수록 디테일이 되살아나 오목한 하트가 완성된다 — JPEG·MP3 의 "버려도 되는 계수"와 같은 원리.
// 자동 모드에서는 슬라이더 값이 스스로 1→64→1 로 왕복한다. 사람이 직접 잡으면(pointerdown·input)
// 자동 왕복을 멈춘다.
// 화면이 사는 장치 — 원 개수가 매 순간 변하므로 체인·궤적 전체가 매 프레임 다시 계산된다.
import { useEffect, useRef, useState } from "react";
import type { ChangeEvent, CSSProperties, PointerEvent as ReactPointerEvent } from "react";
import styles from "./FourierHarmonicSliderLab.module.scss";
export type FourierHarmonicSliderLabProps = {
duration?: string;
easing?: string;
color?: string;
n?: number; // 경로 샘플 수
maxShow?: number; // 슬라이더 최대(체인에 얹을 계수 수)
sweep?: number; // 자동 왕복 주기(초)
};
type Vars = CSSProperties & { "--duration"?: string; "--easing"?: string; "--color"?: string };
type Box<T> = { current: T };
type RGB = [number, number, number];
type Pt = { x: number; y: number };
type Coef = { freq: number; re: number; im: number; amp: number };
const FIELD_LABEL = "배음 슬라이더 실험판";
const TAU = Math.PI * 2;
function heartPath(u: number): Pt {
const t = u * TAU;
const x = 16 * Math.pow(Math.sin(t), 3);
const y = -(13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t));
return { x: x / 17, y: y / 17 };
}
function dft(path: Pt[]): Coef[] {
const M = path.length;
const out: Coef[] = [];
for (let k = 0; k < M; k++) {
const freq = (k <= M / 2) ? k : k - M;
let re = 0, im = 0;
for (let n = 0; n < M; n++) {
const a = -TAU * k * n / M;
const c = Math.cos(a), s = Math.sin(a);
re += path[n].x * c - path[n].y * s;
im += path[n].x * s + path[n].y * c;
}
re /= M; im /= M;
out.push({ freq: freq, re: re, im: im, amp: Math.sqrt(re * re + im * im) });
}
out.sort((a, b) => b.amp - a.amp);
return out;
}
export default function FourierHarmonicSliderLab({
duration = "2s",
easing = "cubic-bezier(.2, .8, .2, 1)",
color = "#b7c8ff",
n = 256,
maxShow = 64,
sweep = 3.2
}: FourierHarmonicSliderLabProps) {
const rootRef = useRef<HTMLDivElement | null>(null);
const fieldRef = useRef<HTMLDivElement | null>(null);
const cvRef = useRef<HTMLCanvasElement | null>(null);
const ctxRef = useRef<CanvasRenderingContext2D | null>(null);
const colRef = useRef<{ ink: RGB; glow: RGB; hot: RGB } | null>(null);
const trailCvRef = useRef<HTMLCanvasElement | null>(null);
const trailCtxRef = useRef<CanvasRenderingContext2D | null>(null);
const sliderRef = useRef<HTMLInputElement | null>(null);
const nRef = useRef(n) as Box<number>;
const maxShowRef = useRef(maxShow) as Box<number>;
const sweepRef = useRef(sweep) as Box<number>;
const pathRef = useRef<Pt[]>([]) as Box<Pt[]>;
const coefRef = useRef<Coef[]>([]) as Box<Coef[]>;
const autoModeRef = useRef(true) as Box<boolean>;
const wRef = useRef(0) as Box<number>;
const hRef = useRef(0) as Box<number>;
const dprRef = useRef(1) as Box<number>;
const rafRef = useRef(0) as Box<number>;
const [cnt, setCnt] = useState("1");
const [sliderVal, setSliderVal] = useState(1);
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 rgba(c: RGB, a: number): string {
return "rgba(" + c[0] + "," + c[1] + "," + c[2] + "," + a + ")";
}
function fit(): void {
const field = fieldRef.current;
const cv = cvRef.current;
if (!field || !cv) return;
const r = field.getBoundingClientRect();
const W = Math.round(r.width), H = Math.round(r.height);
const dpr = Math.min(2, window.devicePixelRatio || 1);
wRef.current = W; hRef.current = H; dprRef.current = dpr;
cv.width = W * dpr; cv.height = H * dpr;
cv.style.width = "100%"; cv.style.height = "100%";
ctxRef.current = cv.getContext("2d");
const trailCv = document.createElement("canvas");
trailCv.width = W * dpr; trailCv.height = H * dpr;
const trailCtx = trailCv.getContext("2d");
if (trailCtx) trailCtx.scale(dpr, dpr);
trailCvRef.current = trailCv;
trailCtxRef.current = trailCtx;
if (!pathRef.current.length) {
const PATH: Pt[] = [];
for (let i = 0; i < nRef.current; i++) PATH.push(heartPath(i / nRef.current));
pathRef.current = PATH;
coefRef.current = dft(PATH);
}
}
function chainAt(u: number, show: number): Pt[] {
const W = wRef.current, H = hRef.current;
const COEF = coefRef.current;
const base = Math.min(W, H) * 0.4;
const cx = W * 0.5, cy = H * 0.5;
const pts: Pt[] = [{ x: cx, y: cy }];
for (let i = 0; i < show && i < COEF.length; i++) {
const c = COEF[i];
const ph = Math.atan2(c.im, c.re);
const ang = c.freq * u * TAU + ph;
pts.push({
x: pts[i].x + Math.cos(ang) * c.amp * base,
y: pts[i].y + Math.sin(ang) * c.amp * base
});
}
return pts;
}
function drawGrid(ctx: CanvasRenderingContext2D, t: number): void {
const W = wRef.current, H = hRef.current;
const col = colRef.current;
if (!col) return;
const cx = W * 0.5, cy = H * 0.5;
const maxR = Math.sqrt(W * W + H * H) * 0.5;
const sweepA = t * 0.85;
for (let b = 0; b < 18; b++) {
ctx.fillStyle = rgba(col.glow, 0.095 * (1 - b / 18));
ctx.beginPath();
ctx.moveTo(cx, cy);
ctx.arc(cx, cy, maxR, sweepA - (b + 1) * 0.055, sweepA - b * 0.055);
ctx.closePath();
ctx.fill();
}
const spin = t * 0.28;
for (let i = 0; i < 16; i++) {
const a = spin + (i / 16) * TAU;
ctx.strokeStyle = rgba(col.glow, i % 2 === 0 ? 0.095 : 0.055);
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(cx + Math.cos(a) * 12, cy + Math.sin(a) * 12);
ctx.lineTo(cx + Math.cos(a) * maxR, cy + Math.sin(a) * maxR);
ctx.stroke();
}
}
function draw(u: number, pulse: number, show: number, gt: number): Pt {
const ctx = ctxRef.current;
const trailCv = trailCvRef.current;
const col = colRef.current;
const dpr = dprRef.current;
const W = wRef.current, H = hRef.current;
const COEF = coefRef.current;
if (!ctx || !trailCv || !col) return { x: 0, y: 0 };
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.fillStyle = "rgb(" + col.ink[0] + "," + col.ink[1] + "," + col.ink[2] + ")";
ctx.fillRect(0, 0, W, H);
drawGrid(ctx, gt);
ctx.drawImage(trailCv, 0, 0, W, H);
const base = Math.min(W, H) * 0.4;
const pts = chainAt(u, show);
for (let i = 0; i < pts.length - 1; i++) {
const r = COEF[i].amp * base;
if (r > 2) {
ctx.strokeStyle = rgba(col.glow, 0.09 + 0.01 * i);
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(pts[i].x, pts[i].y, r, 0, TAU);
ctx.stroke();
}
if (i < 4 || r > 3) {
ctx.strokeStyle = rgba(col.glow, 0.46);
ctx.beginPath();
ctx.moveTo(pts[i].x, pts[i].y);
ctx.lineTo(pts[i + 1].x, pts[i + 1].y);
ctx.stroke();
}
}
const pen = pts[pts.length - 1];
const pr = 2.4 + pulse * 2.2;
ctx.fillStyle = rgba(col.hot, 0.3);
ctx.beginPath();
ctx.arc(pen.x, pen.y, pr * 2.3, 0, TAU);
ctx.fill();
ctx.fillStyle = rgba(col.hot, 1);
ctx.beginPath();
ctx.arc(pen.x, pen.y, pr, 0, TAU);
ctx.fill();
return pen;
}
const prevPenRef = useRef<Pt | null>(null) as Box<Pt | null>;
function stampTrail(pen: Pt): void {
const trailCtx = trailCtxRef.current;
const col = colRef.current;
const prev = prevPenRef.current;
if (!trailCtx || !col) return;
if (!prev) { prevPenRef.current = pen; return; }
trailCtx.strokeStyle = rgba(col.hot, 0.8);
trailCtx.lineWidth = 1.7;
trailCtx.lineCap = "round";
trailCtx.beginPath();
trailCtx.moveTo(prev.x, prev.y);
trailCtx.lineTo(pen.x, pen.y);
trailCtx.stroke();
trailCtx.strokeStyle = rgba(col.hot, 0.12);
trailCtx.lineWidth = 4.6;
trailCtx.stroke();
prevPenRef.current = pen;
}
function decayTrail(): void {
const trailCtx = trailCtxRef.current;
const dpr = dprRef.current;
const W = wRef.current, H = hRef.current;
if (!trailCtx) return;
trailCtx.setTransform(1, 0, 0, 1, 0, 0);
trailCtx.globalCompositeOperation = "destination-out";
trailCtx.fillStyle = "rgba(0,0,0,0.03)";
trailCtx.fillRect(0, 0, W * dpr, H * dpr);
trailCtx.globalCompositeOperation = "source-over";
trailCtx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
let t0 = -1;
function frame(now: number): void {
if (t0 < 0) t0 = now;
const lt = (now - t0) / 1000;
const u = (lt * 0.55) % 1;
const pulse = 0.5 + 0.5 * Math.sin(lt * 8.0);
let show: number;
if (autoModeRef.current) {
// 자동 왕복 — 1→64→1. 사람이 잡으면 멈춘다
const tri = 0.5 - 0.5 * Math.cos(lt * TAU / sweepRef.current);
show = 1 + Math.round(tri * (maxShowRef.current - 1));
const slider = sliderRef.current;
if (slider && slider.value !== String(show)) {
slider.value = String(show);
setSliderVal(show);
}
} else {
const slider = sliderRef.current;
show = slider ? parseInt(slider.value, 10) : maxShowRef.current;
}
decayTrail();
const pen = draw(u, pulse, show, lt * TAU);
stampTrail(pen);
setCnt(String(show));
rafRef.current = requestAnimationFrame(frame);
}
// 사람이 잡으면 자동 왕복을 멈춘다
function onSliderPointerDown(_e: ReactPointerEvent<HTMLInputElement>): void {
autoModeRef.current = false;
}
function onSliderInput(e: ChangeEvent<HTMLInputElement>): void {
autoModeRef.current = false;
prevPenRef.current = null;
const v = parseInt(e.target.value, 10);
setSliderVal(v);
setCnt(String(v));
}
function stillFrame(): void {
// reduce — 원 8개 궤적을 미리 쌓은 뒤 한 장 고정
const show = 8;
let prev = chainAt(0, show)[show];
for (let i = 1; i <= 200; i++) {
const pen = chainAt(i / 200, show)[show];
stampTrail(pen);
prev = pen;
}
draw(0.5, 0.5, show, 2.1);
}
useEffect(() => {
const root = rootRef.current;
if (!root) return;
nRef.current = n;
maxShowRef.current = maxShow;
sweepRef.current = sweep;
colRef.current = {
ink: tokRGB(root, "--fe-ink"),
glow: tokRGB(root, "--fe-glow"),
hot: tokRGB(root, "--fe-hot"),
};
fit();
function onResize(): void {
fit(); prevPenRef.current = null;
if (reduced()) stillFrame();
}
window.addEventListener("resize", onResize);
if (reduced()) {
stillFrame();
setStill(true);
} else {
t0 = -1;
rafRef.current = requestAnimationFrame(frame);
}
return () => {
window.removeEventListener("resize", onResize);
cancelAnimationFrame(rafRef.current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [n, maxShow, sweep]);
const rootCls = still ? `${styles.root} ${styles.isStill}` : styles.root;
return (
<main className={styles.stage}>
<div className={rootCls} ref={rootRef} style={vars}>
<p className={styles.hint}>
<span>사용할 원 개수를 슬라이더로 1부터 전체까지 움직이면 하트가 물방울에서 점점 또렷한 하트로 되살아나는데, 이것이 JPEG 음악 압축까지 같은 원리의 얼마나 줄여도 본떼가 남는가 실험입니다</span>
</p>
<div
className={styles.field}
ref={fieldRef}
role="application"
aria-label={FIELD_LABEL}
tabIndex={0}
>
<canvas className={styles.cv} ref={cvRef}></canvas>
</div>
<p className={styles.panel}>
<label className={styles.label} htmlFor="feSlider">원 개수</label>
<input
className={styles.slider}
id="feSlider"
ref={sliderRef}
type="range"
min={1}
max={maxShow}
step={1}
value={sliderVal}
aria-label="원 개수"
onPointerDown={onSliderPointerDown}
onChange={onSliderInput}
/>
</p>
<div className={styles.bar}>
<span className={styles.meta}><b className={styles.val}>{cnt}</b>개 사용 중 · 하트 재합성</span>
<span className={styles.metaDim}>canvas · truncated series · slider</span>
</div>
</div>
</main>
);
}08 倍音スライダー — 円の数を減らすと絵がどう滲むか — 計測 累積面積 41.044% · 動いたフレーム 23/23GODRICH オリジナル · SCSS と React は zip に同梱 · フーリエ変換 アニメーション 9種 — 円の連鎖で星とハートを描き直す
このデモが属する記事
フーリエ変換 アニメーション 9種 — 円の連鎖で星とハートを描き直すフーリエ変換 アニメーションとは、絵を描き直す作業ではなく、絵に隠れていた周波数を円の連鎖として巻き戻す計算です。ライブラリ・WebGLなしで、描きたい線の点を複素平面上に置き、サイン・コサインの積分を一度走らせると、円の上に円が載って回る この記事のデモをすべて見る


