画像 ドット絵 変換 9パターン — キャンバスでドット絵を作る
画像 ドット絵 変換とは、写真をブロック計算で四角いドットに変える表現です。拡大した写真はなぜ色つきの四角に変わるのか? 01番はブロックの値ひとつでその強さを決め、05番はポインターのそばでそのブロックをさらに大きくし、09番はスライダーひとつで元写真とドット絵を分ける。
自動再生 · タイルを押すとその項目へ · すべて1つのzip
- 01 ブロック平均ダウンサンプル
- 02 パレット色量子化
- 03 誤差拡散ディザリング
- 04 明度バンド・ポスタリゼーション
- 05 ポインター半径ドットレンズ
- 06 段階的ドットリビール遷移
- 07 レトロコンソール・パレット固定
- 08 明度→アスキー文字マッピング
- 09 ビフォーアフター・ヒーロー比較シーン
9つの項目はすべて同じ手続き的に描かれたシーンから始まります。drawScene()がseedRand()というシード付き擬似乱数生成器を使って図形とグラデーションを描くので、ここにアップロードされた写真は一枚もありません。以下の項目は同じシーンをgetImageData()/putImageData()でそれぞれ違う方法に作り替えていて、ピクセルの色だけを変える計算(ブロック平均・パレット量子化・誤差拡散・明度バンド)から、ポインターやタイマーも読み取る計算へ進み、最後は元写真とドット絵変換結果を並べる比較シーンで終わります。
01ブロック値ひとつで決まるブロック平均ダウンサンプル
01番は元画像を正方形ブロックに分け、ブロック内の色を平均して一つの大きなピクセルに変えます。プロフィール写真をドット絵アイコンに変えるときに使える手法です。値を決めているのはPIXEL_BLOCK_SIZE_PXで、キャンバスバッファを走査する毎フレームのループの中、ブロックごとに一度blockAverage()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<input class="vd__range" id="vdRange" type="range" min="2" max="24" value="4" step="1" aria-label="pixel block size">
<p class="vd__caption">ブロックサイズ値ひとつで作るダウンサンプル</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-ink);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
.vd__range {
position: absolute;
left: $sp-3;
right: $sp-3;
top: $sp-3;
margin: 0;
accent-color: $subject-blue;
cursor: pointer;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd { cursor: default; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
.vd__range {
position: absolute;
left: 12px;
right: 12px;
top: 12px;
margin: 0;
accent-color: #2f6df6;
cursor: pointer;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
function blockAverage(buf, w, h, blockPx, outBuf) {
for (var by = 0; by < h; by += blockPx) {
var bh = Math.min(blockPx, h - by);
for (var bx = 0; bx < w; bx += blockPx) {
var bw = Math.min(blockPx, w - bx);
var rs = 0, gs = 0, bs = 0, count = 0;
for (var y = 0; y < bh; y += 1) {
for (var x = 0; x < bw; x += 1) {
var idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
var r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (var y2 = 0; y2 < bh; y2 += 1) {
for (var x2 = 0; x2 < bw; x2 += 1) {
var idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
var range = document.getElementById('vdRange');
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var PIXEL_BLOCK_SIZE_PX = 4;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415011);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var baseBuf = baseImg.data;
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
var outImg = outCtx.createImageData(CALC_W, CALC_H);
var outBuf = outImg.data;
var userBlock = PIXEL_BLOCK_SIZE_PX;
if (range) {
range.value = String(userBlock);
range.addEventListener('input', function () {
userBlock = Number(range.value);
if (reduce) draw(lastElapsed);
});
}
function renderFrame(t) {
var wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 9;
var blockPx = Math.max(2, Math.min(28, Math.round(userBlock + wave)));
blockAverage(baseBuf, CALC_W, CALC_H, blockPx, outBuf);
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelBlockDownsample.module.scss";
export default function PixelBlockDownsample() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const rangeRef = useRef<HTMLInputElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
const range = rangeRef.current;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
const accent = ["#ff5c8a", "#2fe6c6", "#ffb703", "#8a5cd6", "#3ddc97", "#ff7a5e"];
for (let m = 0; m < 8; m += 1) {
const ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
function blockAverage(buf: Uint8ClampedArray, w: number, h: number, blockPx: number, outBuf: Uint8ClampedArray) {
for (let by = 0; by < h; by += blockPx) {
const bh = Math.min(blockPx, h - by);
for (let bx = 0; bx < w; bx += blockPx) {
const bw = Math.min(blockPx, w - bx);
let rs = 0, gs = 0, bs = 0, count = 0;
for (let y = 0; y < bh; y += 1) {
for (let x = 0; x < bw; x += 1) {
const idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
const r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (let y2 = 0; y2 < bh; y2 += 1) {
for (let x2 = 0; x2 < bw; x2 += 1) {
const idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const PIXEL_BLOCK_SIZE_PX = 4;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415011);
const baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const baseBuf = baseImg.data;
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
const outImg = outCtx.createImageData(CALC_W, CALC_H);
const outBuf = outImg.data;
let userBlock = PIXEL_BLOCK_SIZE_PX;
if (range) {
range.value = String(userBlock);
range.addEventListener("input", function () {
userBlock = Number(range.value);
if (reduce) draw(lastElapsed);
});
}
function renderFrame(t: number) {
const wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 9;
const blockPx = Math.max(2, Math.min(28, Math.round(userBlock + wave)));
blockAverage(baseBuf, CALC_W, CALC_H, blockPx, outBuf);
}
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<input className={styles.vd__range} ref={rangeRef} type="range" min={2} max={24} defaultValue={4} step={1} aria-label="pixel block size" />
<p className={styles.vd__caption}>블록 크기 값 하나로 만드는 다운샘플</p>
</div>
</main>
);
}01 · ブロック平均ダウンサンプル — 計測 累積面積 39.534% · 動いたフレーム 15/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
function blockAverage(buf, w, h, blockPx, outBuf) {
for (var by = 0; by < h; by += blockPx) {
var bh = Math.min(blockPx, h - by);
for (var bx = 0; bx < w; bx += blockPx) {
var bw = Math.min(blockPx, w - bx);
var rs = 0, gs = 0, bs = 0, count = 0;
for (var y = 0; y < bh; y += 1) {
for (var x = 0; x < bw; x += 1) {
var idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
var r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (var y2 = 0; y2 < bh; y2 += 1) {
for (var x2 = 0; x2 < bw; x2 += 1) {
var idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
測定値: ループ内でフレームの39.5%が書き換わり、強度は平均42.5、動いたフレームは15/23です。
02色数ひとつで決まるパレット色量子化
02番は各ピクセルの色を決まった数のパレット色の中から一番近い色に置き換え、画面をひと握りの平坦な色だけにまとめます。8ビットゲーム風バナーに向く手法です。値を決めているのはPALETTE_COLOR_COUNTで、キャンバスバッファを走査する毎フレームのループの中、ピクセルごとに一度nearestPaletteColor()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<input class="vd__range" id="vdRange" type="range" min="2" max="16" value="6" step="1" aria-label="palette color count">
<p class="vd__caption">パレット色数を調整するカラー量子化</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-yellow);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
.vd__range {
position: absolute;
left: $sp-3;
right: $sp-3;
top: $sp-3;
margin: 0;
accent-color: $subject-blue;
cursor: pointer;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd { cursor: default; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ffd23f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
.vd__range {
position: absolute;
left: 12px;
right: 12px;
top: 12px;
margin: 0;
accent-color: #2f6df6;
cursor: pointer;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
var PALETTE_HEX = ['#17141a', '#2f6df6', '#ff4d1f', '#ffd23f', '#ffffff', '#2b9d74', '#ff5c8a', '#8a5cd6', '#3ddc97', '#ff7a5e', '#6ec8ff', '#c93000', '#fff7e6', '#0a4fe8', '#2fe6c6', '#b02860'];
function hexToRgb(hex) {
var n = parseInt(hex.slice(1), 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
var PALETTE_RGB = PALETTE_HEX.map(hexToRgb);
function nearestPaletteColor(r, g, b, count) {
var best = 0, bd = Infinity;
for (var i = 0; i < count; i += 1) {
var p = PALETTE_RGB[i];
var dr = r - p[0], dg = g - p[1], db = b - p[2];
var d = dr * dr + dg * dg + db * db;
if (d < bd) { bd = d; best = i; }
}
return PALETTE_RGB[best];
}
function quantizePalette(buf, w, h, count, outBuf) {
for (var i = 0; i < w * h; i += 1) {
var idx = i * 4;
var c = nearestPaletteColor(buf[idx], buf[idx + 1], buf[idx + 2], count);
outBuf[idx] = c[0]; outBuf[idx + 1] = c[1]; outBuf[idx + 2] = c[2]; outBuf[idx + 3] = 255;
}
}
var range = document.getElementById('vdRange');
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var PALETTE_COLOR_COUNT = 6;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415021);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var baseBuf = baseImg.data;
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
var outImg = outCtx.createImageData(CALC_W, CALC_H);
var outBuf = outImg.data;
var userCount = PALETTE_COLOR_COUNT;
if (range) {
range.value = String(userCount);
range.addEventListener('input', function () {
userCount = Number(range.value);
if (reduce) draw(lastElapsed);
});
}
function renderFrame(t) {
var wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 5;
var count = Math.max(2, Math.min(16, Math.round(userCount + wave)));
quantizePalette(baseBuf, CALC_W, CALC_H, count, outBuf);
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelPaletteQuantize.module.scss";
export default function PixelPaletteQuantize() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const rangeRef = useRef<HTMLInputElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
const range = rangeRef.current;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
const accent = ["#ff5c8a", "#2fe6c6", "#ffb703", "#8a5cd6", "#3ddc97", "#ff7a5e"];
for (let m = 0; m < 8; m += 1) {
const ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
const PALETTE_HEX = ["#17141a", "#2f6df6", "#ff4d1f", "#ffd23f", "#ffffff", "#2b9d74", "#ff5c8a", "#8a5cd6", "#3ddc97", "#ff7a5e", "#6ec8ff", "#c93000", "#fff7e6", "#0a4fe8", "#2fe6c6", "#b02860"];
function hexToRgb(hex: string): [number, number, number] {
const n = parseInt(hex.slice(1), 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
const PALETTE_RGB = PALETTE_HEX.map(hexToRgb);
function nearestPaletteColor(r: number, g: number, b: number, count: number) {
let best = 0, bd = Infinity;
for (let i = 0; i < count; i += 1) {
const p = PALETTE_RGB[i];
const dr = r - p[0], dg = g - p[1], db = b - p[2];
const d = dr * dr + dg * dg + db * db;
if (d < bd) { bd = d; best = i; }
}
return PALETTE_RGB[best];
}
function quantizePalette(buf: Uint8ClampedArray, w: number, h: number, count: number, outBuf: Uint8ClampedArray) {
for (let i = 0; i < w * h; i += 1) {
const idx = i * 4;
const c = nearestPaletteColor(buf[idx], buf[idx + 1], buf[idx + 2], count);
outBuf[idx] = c[0]; outBuf[idx + 1] = c[1]; outBuf[idx + 2] = c[2]; outBuf[idx + 3] = 255;
}
}
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const PALETTE_COLOR_COUNT = 6;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415021);
const baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const baseBuf = baseImg.data;
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
const outImg = outCtx.createImageData(CALC_W, CALC_H);
const outBuf = outImg.data;
let userCount = PALETTE_COLOR_COUNT;
if (range) {
range.value = String(userCount);
range.addEventListener("input", function () {
userCount = Number(range.value);
if (reduce) draw(lastElapsed);
});
}
function renderFrame(t: number) {
const wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 5;
const count = Math.max(2, Math.min(16, Math.round(userCount + wave)));
quantizePalette(baseBuf, CALC_W, CALC_H, count, outBuf);
}
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<input className={styles.vd__range} ref={rangeRef} type="range" min={2} max={16} defaultValue={6} step={1} aria-label="palette color count" />
<p className={styles.vd__caption}>팔레트 색 수를 조절하는 색상 양자화</p>
</div>
</main>
);
}02 · パレット色量子化 — 計測 累積面積 59.009% · 動いたフレーム 17/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
function nearestPaletteColor(r, g, b, count) {
var best = 0, bd = Infinity;
for (var i = 0; i < count; i += 1) {
var p = PALETTE_RGB[i];
var dr = r - p[0], dg = g - p[1], db = b - p[2];
var d = dr * dr + dg * dg + db * db;
if (d < bd) { bd = d; best = i; }
}
return PALETTE_RGB[best];
}
function quantizePalette(buf, w, h, count, outBuf) {
for (var i = 0; i < w * h; i += 1) {
var idx = i * 4;
var c = nearestPaletteColor(buf[idx], buf[idx + 1], buf[idx + 2], count);
outBuf[idx] = c[0]; outBuf[idx + 1] = c[1]; outBuf[idx + 2] = c[2]; outBuf[idx + 3] = 255;
}
}
測定値: ループ内でフレームの59.0%が書き換わり、強度は平均163.0、動いたフレームは17/23です。
03誤差拡散の広げ方ひとつで変わるディザリング
03番は量子化で生じた色の誤差を右隣と下側のピクセルへ広げ、点のパターンでなめらかな階調を表現します。グレースケール写真を白黒のドットに変えたいときに向いています。値を決めているのはDITHER_LEVELSで、キャンバスバッファを走査するピクセルごとのループの中、チャンネルごとに一度diffuseError()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<p class="vd__caption">誤差を隣の画素へ広げるディザリング</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-orange);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd { cursor: default; }
}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);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
// 오차 확산: 화소를 앞에서부터 훑으며 양자화 오차를 아직 처리하지 않은 이웃 화소로 나눠 더한다
function diffuseError(buf, w, h, levels, outBuf) {
var work = new Float32Array(w * h * 3);
for (var i = 0, j = 0; i < w * h; i += 1, j += 3) {
work[j] = buf[i * 4]; work[j + 1] = buf[i * 4 + 1]; work[j + 2] = buf[i * 4 + 2];
}
var step = 255 / (levels - 1);
var rowStride = w * 3;
for (var y = 0; y < h; y += 1) {
for (var x = 0; x < w; x += 1) {
var idx = (y * w + x) * 3;
for (var c = 0; c < 3; c += 1) {
var old = work[idx + c];
var q = Math.max(0, Math.min(255, Math.round(Math.round(old / step) * step)));
var err = old - q;
work[idx + c] = q;
if (x + 1 < w) work[idx + 3 + c] += err * 7 / 16;
if (y + 1 < h) {
if (x > 0) work[idx + rowStride - 3 + c] += err * 3 / 16;
work[idx + rowStride + c] += err * 5 / 16;
if (x + 1 < w) work[idx + rowStride + 3 + c] += err * 1 / 16;
}
}
}
}
for (var p = 0, q2 = 0; p < w * h; p += 1, q2 += 3) {
var o = p * 4;
outBuf[o] = work[q2]; outBuf[o + 1] = work[q2 + 1]; outBuf[o + 2] = work[q2 + 2]; outBuf[o + 3] = 255;
}
}
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var DITHER_LEVELS = 3;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415031);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var baseBuf = baseImg.data;
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
var outImg = outCtx.createImageData(CALC_W, CALC_H);
var outBuf = outImg.data;
function renderFrame(t) {
var wave = Math.sin(t * Math.PI * 4 / LOOP_MS);
var levels = Math.max(2, DITHER_LEVELS + wave * 2);
diffuseError(baseBuf, CALC_W, CALC_H, levels, outBuf);
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelDitherDiffuse.module.scss";
export default function PixelDitherDiffuse() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
function diffuseError(buf: Uint8ClampedArray, w: number, h: number, levels: number, outBuf: Uint8ClampedArray) {
const work = new Float32Array(w * h * 3);
for (let i = 0, j = 0; i < w * h; i += 1, j += 3) {
work[j] = buf[i * 4]; work[j + 1] = buf[i * 4 + 1]; work[j + 2] = buf[i * 4 + 2];
}
const step = 255 / (levels - 1);
const rowStride = w * 3;
for (let y = 0; y < h; y += 1) {
for (let x = 0; x < w; x += 1) {
const idx = (y * w + x) * 3;
for (let c = 0; c < 3; c += 1) {
const old = work[idx + c];
const q = Math.max(0, Math.min(255, Math.round(Math.round(old / step) * step)));
const err = old - q;
work[idx + c] = q;
if (x + 1 < w) work[idx + 3 + c] += err * 7 / 16;
if (y + 1 < h) {
if (x > 0) work[idx + rowStride - 3 + c] += err * 3 / 16;
work[idx + rowStride + c] += err * 5 / 16;
if (x + 1 < w) work[idx + rowStride + 3 + c] += err * 1 / 16;
}
}
}
}
for (let p = 0, q2 = 0; p < w * h; p += 1, q2 += 3) {
const o = p * 4;
outBuf[o] = work[q2]; outBuf[o + 1] = work[q2 + 1]; outBuf[o + 2] = work[q2 + 2]; outBuf[o + 3] = 255;
}
}
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const DITHER_LEVELS = 3;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415031);
const baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const baseBuf = baseImg.data;
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
const outImg = outCtx.createImageData(CALC_W, CALC_H);
const outBuf = outImg.data;
function renderFrame(t: number) {
const wave = Math.sin(t * Math.PI * 4 / LOOP_MS);
const levels = Math.max(2, DITHER_LEVELS + wave * 2);
diffuseError(baseBuf, CALC_W, CALC_H, levels, outBuf);
}
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.vd__caption}>오차를 이웃 화소로 퍼뜨리는 디더링</p>
</div>
</main>
);
}03 · 誤差拡散ディザリング — 計測 累積面積 75.294% · 動いたフレーム 18/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
for (var x = 0; x < w; x += 1) {
var idx = (y * w + x) * 3;
for (var c = 0; c < 3; c += 1) {
var old = work[idx + c];
var q = Math.max(0, Math.min(255, Math.round(Math.round(old / step) * step)));
var err = old - q;
work[idx + c] = q;
if (x + 1 < w) work[idx + 3 + c] += err * 7 / 16;
if (y + 1 < h) {
if (x > 0) work[idx + rowStride - 3 + c] += err * 3 / 16;
work[idx + rowStride + c] += err * 5 / 16;
if (x + 1 < w) work[idx + rowStride + 3 + c] += err * 1 / 16;
}
}
}
測定値: ループ内でフレームの75.3%が書き換わり、強度は平均88.6、動いたフレームは18/23です。
04帯の数ひとつで段階が変わる明度バンド・ポスタリゼーション
04番は各チャンネルの明るさを決まった数の帯に区切り、同じ帯の値をひとつの色にまとめます。イラストカードの陰影を段階的に見せたいときに向く手法です。値を決めているのはPOSTERIZE_BAND_COUNTで、キャンバスバッファを走査する毎フレームのループの中、ピクセルごとに一度quantizeBand()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<input class="vd__range" id="vdRange" type="range" min="2" max="8" value="4" step="1" aria-label="posterize band count">
<p class="vd__caption">明度区間を分けるポスタリゼーション</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-paper);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(23, 20, 26, .78);
color: $subject-cream;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
.vd__range {
position: absolute;
left: $sp-3;
right: $sp-3;
top: $sp-3;
margin: 0;
accent-color: $subject-blue-deep;
cursor: pointer;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd { cursor: default; }
}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);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(23, 20, 26, 0.78);
color: #fff7e6;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
.vd__range {
position: absolute;
left: 12px;
right: 12px;
top: 12px;
margin: 0;
accent-color: #0a4fe8;
cursor: pointer;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
function quantizeBand(buf, w, h, bands, outBuf) {
var step = 255 / (bands - 1);
for (var i = 0; i < w * h; i += 1) {
var idx = i * 4;
for (var c = 0; c < 3; c += 1) {
var v = buf[idx + c];
outBuf[idx + c] = Math.max(0, Math.min(255, Math.round(Math.round(v / step) * step)));
}
outBuf[idx + 3] = 255;
}
}
var range = document.getElementById('vdRange');
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var POSTERIZE_BAND_COUNT = 4;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415041);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var baseBuf = baseImg.data;
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
var outImg = outCtx.createImageData(CALC_W, CALC_H);
var outBuf = outImg.data;
var userBands = POSTERIZE_BAND_COUNT;
if (range) {
range.value = String(userBands);
range.addEventListener('input', function () {
userBands = Number(range.value);
if (reduce) draw(lastElapsed);
});
}
function renderFrame(t) {
var wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 2;
var bands = Math.max(2, Math.min(8, Math.round(userBands + wave)));
quantizeBand(baseBuf, CALC_W, CALC_H, bands, outBuf);
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelPosterizeBand.module.scss";
export default function PixelPosterizeBand() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const rangeRef = useRef<HTMLInputElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
const range = rangeRef.current;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
const accent = ["#ff5c8a", "#2fe6c6", "#ffb703", "#8a5cd6", "#3ddc97", "#ff7a5e"];
for (let m = 0; m < 8; m += 1) {
const ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
function quantizeBand(buf: Uint8ClampedArray, w: number, h: number, bands: number, outBuf: Uint8ClampedArray) {
const step = 255 / (bands - 1);
for (let i = 0; i < w * h; i += 1) {
const idx = i * 4;
for (let c = 0; c < 3; c += 1) {
const v = buf[idx + c];
outBuf[idx + c] = Math.max(0, Math.min(255, Math.round(Math.round(v / step) * step)));
}
outBuf[idx + 3] = 255;
}
}
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const POSTERIZE_BAND_COUNT = 4;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415041);
const baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const baseBuf = baseImg.data;
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
const outImg = outCtx.createImageData(CALC_W, CALC_H);
const outBuf = outImg.data;
let userBands = POSTERIZE_BAND_COUNT;
if (range) {
range.value = String(userBands);
range.addEventListener("input", function () {
userBands = Number(range.value);
if (reduce) draw(lastElapsed);
});
}
function renderFrame(t: number) {
const wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 2;
const bands = Math.max(2, Math.min(8, Math.round(userBands + wave)));
quantizeBand(baseBuf, CALC_W, CALC_H, bands, outBuf);
}
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<input className={styles.vd__range} ref={rangeRef} type="range" min={2} max={8} defaultValue={4} step={1} aria-label="posterize band count" />
<p className={styles.vd__caption}>명도 구간을 나누는 포스터라이즈</p>
</div>
</main>
);
}04 · 明度バンド・ポスタリゼーション — 計測 累積面積 70.713% · 動いたフレーム 18/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
function quantizeBand(buf, w, h, bands, outBuf) {
var step = 255 / (bands - 1);
for (var i = 0; i < w * h; i += 1) {
var idx = i * 4;
for (var c = 0; c < 3; c += 1) {
var v = buf[idx + c];
outBuf[idx + c] = Math.max(0, Math.min(255, Math.round(Math.round(v / step) * step)));
}
outBuf[idx + 3] = 255;
}
}
測定値: ループ内でフレームの70.7%が書き換わり、強度は平均63.8、動いたフレームは18/23です。
05半径ひとつで範囲が変わるポインター半径ドットレンズ
05番はポインター周りの半径内だけ大きくブロック化した画像に切り替え、その場所だけドットを大きく見せます。商品写真の上に置く虫眼鏡インタラクションに向いています。値を決めているのはPIXEL_LENS_RADIUS_PXで、ポインターとの距離を比較する毎フレームのループの中、ピクセルごとに一度lensCompose()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<p class="vd__caption">ポインター半径の内側だけ拡大するドットレンズ</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-ink);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
cursor: crosshair;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd__canvas { cursor: default; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
cursor: crosshair;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd__canvas {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
function blockAverage(buf, w, h, blockPx, outBuf) {
for (var by = 0; by < h; by += blockPx) {
var bh = Math.min(blockPx, h - by);
for (var bx = 0; bx < w; bx += blockPx) {
var bw = Math.min(blockPx, w - bx);
var rs = 0, gs = 0, bs = 0, count = 0;
for (var y = 0; y < bh; y += 1) {
for (var x = 0; x < bw; x += 1) {
var idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
var r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (var y2 = 0; y2 < bh; y2 += 1) {
for (var x2 = 0; x2 < bw; x2 += 1) {
var idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
function lensCompose(bigBuf, smallBuf, w, h, px, py, radius, outBuf) {
var r2 = radius * radius;
for (var y = 0; y < h; y += 1) {
for (var x = 0; x < w; x += 1) {
var idx = (y * w + x) * 4;
var dx = x - px, dy = y - py;
var src = (dx * dx + dy * dy) < r2 ? bigBuf : smallBuf;
outBuf[idx] = src[idx]; outBuf[idx + 1] = src[idx + 1]; outBuf[idx + 2] = src[idx + 2]; outBuf[idx + 3] = 255;
}
}
}
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var BASE_BLOCK_PX = 4;
var LENS_BLOCK_PX = 20;
var PIXEL_LENS_RADIUS_PX = 40;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415051);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var baseBuf = baseImg.data;
var bigBuf = new Uint8ClampedArray(baseBuf.length);
var smallBuf = new Uint8ClampedArray(baseBuf.length);
blockAverage(baseBuf, CALC_W, CALC_H, LENS_BLOCK_PX, bigBuf);
blockAverage(baseBuf, CALC_W, CALC_H, BASE_BLOCK_PX, smallBuf);
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
var outImg = outCtx.createImageData(CALC_W, CALC_H);
var outBuf = outImg.data;
var pointerActive = false;
var pointerX = CALC_W * 0.5, pointerY = CALC_H * 0.5;
function toCalc(clientX, clientY) {
var rect = canvas.getBoundingClientRect();
var relX = (clientX - rect.left) / Math.max(1, rect.width);
var relY = (clientY - rect.top) / Math.max(1, rect.height);
return [relX * CALC_W, relY * CALC_H];
}
canvas.addEventListener('pointerenter', function () { pointerActive = true; });
canvas.addEventListener('pointerleave', function () { pointerActive = false; });
canvas.addEventListener('pointermove', function (ev) {
var p = toCalc(ev.clientX, ev.clientY);
pointerX = p[0]; pointerY = p[1];
if (reduce) draw(lastElapsed);
});
function renderFrame(t) {
var px, py;
if (pointerActive) {
px = pointerX; py = pointerY;
} else {
var a = t * Math.PI * 4 / LOOP_MS;
px = CALC_W * 0.5 + Math.cos(a) * CALC_W * 0.28;
py = CALC_H * 0.5 + Math.sin(a) * CALC_H * 0.3;
}
lensCompose(bigBuf, smallBuf, CALC_W, CALC_H, px, py, PIXEL_LENS_RADIUS_PX, outBuf);
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelPointerLensRadius.module.scss";
export default function PixelPointerLensRadius() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
const accent = ["#ff5c8a", "#2fe6c6", "#ffb703", "#8a5cd6", "#3ddc97", "#ff7a5e"];
for (let m = 0; m < 8; m += 1) {
const ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
function blockAverage(buf: Uint8ClampedArray, w: number, h: number, blockPx: number, outBuf: Uint8ClampedArray) {
for (let by = 0; by < h; by += blockPx) {
const bh = Math.min(blockPx, h - by);
for (let bx = 0; bx < w; bx += blockPx) {
const bw = Math.min(blockPx, w - bx);
let rs = 0, gs = 0, bs = 0, count = 0;
for (let y = 0; y < bh; y += 1) {
for (let x = 0; x < bw; x += 1) {
const idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
const r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (let y2 = 0; y2 < bh; y2 += 1) {
for (let x2 = 0; x2 < bw; x2 += 1) {
const idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
function lensCompose(bigBuf: Uint8ClampedArray, smallBuf: Uint8ClampedArray, w: number, h: number, px: number, py: number, radius: number, outBuf: Uint8ClampedArray) {
const r2 = radius * radius;
for (let y = 0; y < h; y += 1) {
for (let x = 0; x < w; x += 1) {
const idx = (y * w + x) * 4;
const dx = x - px, dy = y - py;
const src = (dx * dx + dy * dy) < r2 ? bigBuf : smallBuf;
outBuf[idx] = src[idx]; outBuf[idx + 1] = src[idx + 1]; outBuf[idx + 2] = src[idx + 2]; outBuf[idx + 3] = 255;
}
}
}
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const BASE_BLOCK_PX = 4;
const LENS_BLOCK_PX = 20;
const PIXEL_LENS_RADIUS_PX = 40;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415051);
const baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const baseBuf = baseImg.data;
const bigBuf = new Uint8ClampedArray(baseBuf.length);
const smallBuf = new Uint8ClampedArray(baseBuf.length);
blockAverage(baseBuf, CALC_W, CALC_H, LENS_BLOCK_PX, bigBuf);
blockAverage(baseBuf, CALC_W, CALC_H, BASE_BLOCK_PX, smallBuf);
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
const outImg = outCtx.createImageData(CALC_W, CALC_H);
const outBuf = outImg.data;
let pointerActive = false;
let pointerX = CALC_W * 0.5, pointerY = CALC_H * 0.5;
function toCalc(clientX: number, clientY: number): [number, number] {
const rect = canvas!.getBoundingClientRect();
const relX = (clientX - rect.left) / Math.max(1, rect.width);
const relY = (clientY - rect.top) / Math.max(1, rect.height);
return [relX * CALC_W, relY * CALC_H];
}
function onEnter() { pointerActive = true; }
function onLeave() { pointerActive = false; }
function onMove(ev: PointerEvent) {
const p = toCalc(ev.clientX, ev.clientY);
pointerX = p[0]; pointerY = p[1];
if (reduce) draw(lastElapsed);
}
canvas.addEventListener("pointerenter", onEnter);
canvas.addEventListener("pointerleave", onLeave);
canvas.addEventListener("pointermove", onMove);
function renderFrame(t: number) {
let px: number, py: number;
if (pointerActive) {
px = pointerX; py = pointerY;
} else {
const a = t * Math.PI * 4 / LOOP_MS;
px = CALC_W * 0.5 + Math.cos(a) * CALC_W * 0.28;
py = CALC_H * 0.5 + Math.sin(a) * CALC_H * 0.3;
}
lensCompose(bigBuf, smallBuf, CALC_W, CALC_H, px, py, PIXEL_LENS_RADIUS_PX, outBuf);
}
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
canvas!.removeEventListener("pointerenter", onEnter);
canvas!.removeEventListener("pointerleave", onLeave);
canvas!.removeEventListener("pointermove", onMove);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.vd__caption}>포인터 반경 안쪽만 커지는 도트 렌즈</p>
</div>
</main>
);
}05 · ポインター半径ドットレンズ — 計測 累積面積 41.126% · 動いたフレーム 23/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
function lensCompose(bigBuf, smallBuf, w, h, px, py, radius, outBuf) {
var r2 = radius * radius;
for (var y = 0; y < h; y += 1) {
for (var x = 0; x < w; x += 1) {
var idx = (y * w + x) * 4;
var dx = x - px, dy = y - py;
var src = (dx * dx + dy * dy) < r2 ? bigBuf : smallBuf;
outBuf[idx] = src[idx]; outBuf[idx + 1] = src[idx + 1]; outBuf[idx + 2] = src[idx + 2]; outBuf[idx + 3] = 255;
}
}
}
測定値: ループ内でフレームの41.1%が書き換わり、強度は平均47.2、動いたフレームは23/23です。
06段階リストひとつで速さが変わる段階的ドットリビール遷移
06番はブロックサイズを大きい値から小さい値へタイマーで数段階に縮め、同じブロック平均の計算を段ごとに走らせることで、鮮明な写真が徐々に現れるように見せます。ギャラリー画像の読み込み演出に向く手法です。値を決めているのはREVEAL_STEPSで、リビール開始からの経過時間を測る毎フレームのループの中、一度stepIndexAt()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<p class="vd__caption">ブロックサイズが段階的に縮むドットリビール</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-yellow);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
cursor: pointer;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd__canvas { cursor: default; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ffd23f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
cursor: pointer;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd__canvas {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
function blockAverage(buf, w, h, blockPx, outBuf) {
for (var by = 0; by < h; by += blockPx) {
var bh = Math.min(blockPx, h - by);
for (var bx = 0; bx < w; bx += blockPx) {
var bw = Math.min(blockPx, w - bx);
var rs = 0, gs = 0, bs = 0, count = 0;
for (var y = 0; y < bh; y += 1) {
for (var x = 0; x < bw; x += 1) {
var idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
var r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (var y2 = 0; y2 < bh; y2 += 1) {
for (var x2 = 0; x2 < bw; x2 += 1) {
var idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
var REVEAL_STEPS = [28, 20, 14, 9, 5, 2];
var STEP_DURATION_MS = 160;
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415061);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var baseBuf = baseImg.data;
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
var outImg = outCtx.createImageData(CALC_W, CALC_H);
var outBuf = outImg.data;
var replayStart = 0;
function stepIndexAt(sinceStart) {
var cycleMs = REVEAL_STEPS.length * STEP_DURATION_MS;
var pos = sinceStart % cycleMs;
return Math.min(REVEAL_STEPS.length - 1, Math.floor(pos / STEP_DURATION_MS));
}
function renderFrame(t) {
var sinceStart = t - replayStart;
if (sinceStart < 0) sinceStart += LOOP_MS;
var idx = stepIndexAt(sinceStart);
blockAverage(baseBuf, CALC_W, CALC_H, REVEAL_STEPS[idx], outBuf);
}
canvas.addEventListener('pointerdown', function () {
replayStart = lastElapsed;
if (reduce) draw(lastElapsed);
});
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelRevealTransitionStep.module.scss";
export default function PixelRevealTransitionStep() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
const accent = ["#ff5c8a", "#2fe6c6", "#ffb703", "#8a5cd6", "#3ddc97", "#ff7a5e"];
for (let m = 0; m < 8; m += 1) {
const ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
function blockAverage(buf: Uint8ClampedArray, w: number, h: number, blockPx: number, outBuf: Uint8ClampedArray) {
for (let by = 0; by < h; by += blockPx) {
const bh = Math.min(blockPx, h - by);
for (let bx = 0; bx < w; bx += blockPx) {
const bw = Math.min(blockPx, w - bx);
let rs = 0, gs = 0, bs = 0, count = 0;
for (let y = 0; y < bh; y += 1) {
for (let x = 0; x < bw; x += 1) {
const idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
const r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (let y2 = 0; y2 < bh; y2 += 1) {
for (let x2 = 0; x2 < bw; x2 += 1) {
const idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
const REVEAL_STEPS = [28, 20, 14, 9, 5, 2];
const STEP_DURATION_MS = 160;
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415061);
const baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const baseBuf = baseImg.data;
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
const outImg = outCtx.createImageData(CALC_W, CALC_H);
const outBuf = outImg.data;
let replayStart = 0;
function stepIndexAt(sinceStart: number) {
const cycleMs = REVEAL_STEPS.length * STEP_DURATION_MS;
const pos = sinceStart % cycleMs;
return Math.min(REVEAL_STEPS.length - 1, Math.floor(pos / STEP_DURATION_MS));
}
function renderFrame(t: number) {
let sinceStart = t - replayStart;
if (sinceStart < 0) sinceStart += LOOP_MS;
const idx = stepIndexAt(sinceStart);
blockAverage(baseBuf, CALC_W, CALC_H, REVEAL_STEPS[idx], outBuf);
}
function onDown() {
replayStart = lastElapsed;
if (reduce) draw(lastElapsed);
}
canvas.addEventListener("pointerdown", onDown);
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
canvas!.removeEventListener("pointerdown", onDown);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.vd__caption}>블록 크기가 단계로 줄어드는 도트 리빌</p>
</div>
</main>
);
}06 · 段階的ドットリビール遷移 — 計測 累積面積 56.008% · 動いたフレーム 15/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
function stepIndexAt(sinceStart) {
var cycleMs = REVEAL_STEPS.length * STEP_DURATION_MS;
var pos = sinceStart % cycleMs;
return Math.min(REVEAL_STEPS.length - 1, Math.floor(pos / STEP_DURATION_MS));
}
function renderFrame(t) {
var sinceStart = t - replayStart;
if (sinceStart < 0) sinceStart += LOOP_MS;
var idx = stepIndexAt(sinceStart);
blockAverage(baseBuf, CALC_W, CALC_H, REVEAL_STEPS[idx], outBuf);
}
測定値: ループ内でフレームの56.0%が書き換わり、強度は平均45.6、動いたフレームは15/23です。
07切り替えタイマーひとつで変わるレトロコンソール・パレット固定
07番は色を自前定義の8色パレットと16色パレットだけに絞り込み、タイマーでどちらのパレットを使うかを切り替えて、昔のゲーム画面のように見せます。レトロイベントページのヒーロー画像に向いています。値を決めているのはRETRO_BLOCK_PXで、二つのパレットの切り替えも担う毎フレームのループの中、ブロックごとに一度lockRetroPalette()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<p class="vd__caption">固定パレット2組を行き来するレトロロック</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-orange);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd { cursor: default; }
}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);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
var RETRO_PALETTE_8 = ['#0f0720', '#2f1b5c', '#7a2bd8', '#ff4d6d', '#ff9a3c', '#ffe066', '#7fe0a0', '#eaf6ff'];
var RETRO_PALETTE_16 = ['#0a0614', '#1c1030', '#2f1b5c', '#4a2b8c', '#7a2bd8', '#b13bd6', '#ff4d6d', '#ff7a5e',
'#ff9a3c', '#ffc23c', '#ffe066', '#c8e86a', '#7fe0a0', '#3ddc97', '#5fd0ff', '#eaf6ff'];
function hexToRgb(hex) {
var n = parseInt(hex.slice(1), 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
var RGB_8 = RETRO_PALETTE_8.map(hexToRgb);
var RGB_16 = RETRO_PALETTE_16.map(hexToRgb);
function nearestFromSet(r, g, b, set) {
var best = 0, bd = Infinity;
for (var i = 0; i < set.length; i += 1) {
var p = set[i];
var dr = r - p[0], dg = g - p[1], db = b - p[2];
var d = dr * dr + dg * dg + db * db;
if (d < bd) { bd = d; best = i; }
}
return set[best];
}
var RETRO_BLOCK_PX = 6;
function lockRetroPalette(buf, w, h, blockPx, set, outBuf) {
for (var by = 0; by < h; by += blockPx) {
var bh = Math.min(blockPx, h - by);
for (var bx = 0; bx < w; bx += blockPx) {
var bw = Math.min(blockPx, w - bx);
var rs = 0, gs = 0, bs = 0, count = 0;
for (var y = 0; y < bh; y += 1) {
for (var x = 0; x < bw; x += 1) {
var idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
var c = nearestFromSet(rs / count, gs / count, bs / count, set);
for (var y2 = 0; y2 < bh; y2 += 1) {
for (var x2 = 0; x2 < bw; x2 += 1) {
var idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = c[0]; outBuf[idx2 + 1] = c[1]; outBuf[idx2 + 2] = c[2]; outBuf[idx2 + 3] = 255;
}
}
}
}
}
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415071);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var baseBuf = baseImg.data;
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
var outImg = outCtx.createImageData(CALC_W, CALC_H);
var outBuf = outImg.data;
function renderFrame(t) {
var phase = Math.floor((t % LOOP_MS) / 160) % 2;
var set = phase === 0 ? RGB_8 : RGB_16;
lockRetroPalette(baseBuf, CALC_W, CALC_H, RETRO_BLOCK_PX, set, outBuf);
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelRetroPaletteLock.module.scss";
export default function PixelRetroPaletteLock() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
const accent = ["#ff5c8a", "#2fe6c6", "#ffb703", "#8a5cd6", "#3ddc97", "#ff7a5e"];
for (let m = 0; m < 8; m += 1) {
const ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
function hexToRgb(hex: string): [number, number, number] {
const v = parseInt(hex.slice(1), 16);
return [(v >> 16) & 255, (v >> 8) & 255, v & 255];
}
const RETRO_PALETTE_8 = ["#1a1c2c", "#5d275d", "#b13e53", "#ef7d57", "#ffcd75", "#a7f070", "#38b764", "#257179"];
const RETRO_PALETTE_16 = [
"#1a1c2c", "#5d275d", "#b13e53", "#ef7d57", "#ffcd75", "#a7f070", "#38b764", "#257179",
"#29366f", "#3b5dc9", "#41a6f6", "#73eff7", "#f4f4f4", "#94b0c2", "#566c86", "#333c57"
];
const RGB_8 = RETRO_PALETTE_8.map(hexToRgb);
const RGB_16 = RETRO_PALETTE_16.map(hexToRgb);
function nearestFromSet(r: number, g: number, b: number, set: [number, number, number][]) {
let best = 0, bestDist = Infinity;
for (let i = 0; i < set.length; i += 1) {
const c = set[i];
const dr = r - c[0], dg = g - c[1], db = b - c[2];
const dist = dr * dr + dg * dg + db * db;
if (dist < bestDist) { bestDist = dist; best = i; }
}
return set[best];
}
const RETRO_BLOCK_PX = 6;
function lockRetroPalette(buf: Uint8ClampedArray, w: number, h: number, blockPx: number, set: [number, number, number][], outBuf: Uint8ClampedArray) {
for (let by = 0; by < h; by += blockPx) {
const bh = Math.min(blockPx, h - by);
for (let bx = 0; bx < w; bx += blockPx) {
const bw = Math.min(blockPx, w - bx);
let rs = 0, gs = 0, bs = 0, count = 0;
for (let y = 0; y < bh; y += 1) {
for (let x = 0; x < bw; x += 1) {
const idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
const avgR = rs / count, avgG = gs / count, avgB = bs / count;
const picked = nearestFromSet(avgR, avgG, avgB, set);
for (let y2 = 0; y2 < bh; y2 += 1) {
for (let x2 = 0; x2 < bw; x2 += 1) {
const idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = picked[0]; outBuf[idx2 + 1] = picked[1]; outBuf[idx2 + 2] = picked[2]; outBuf[idx2 + 3] = 255;
}
}
}
}
}
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415071);
const baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const baseBuf = baseImg.data;
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
const outImg = outCtx.createImageData(CALC_W, CALC_H);
const outBuf = outImg.data;
function renderFrame(t: number) {
const phase = Math.floor((t % LOOP_MS) / 160) % 2;
const set = phase === 0 ? RGB_8 : RGB_16;
lockRetroPalette(baseBuf, CALC_W, CALC_H, RETRO_BLOCK_PX, set, outBuf);
}
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.vd__caption}>고정 팔레트 두 벌을 오가는 레트로 락</p>
</div>
</main>
);
}07 · レトロコンソール・パレット固定 — 計測 累積面積 33.972% · 動いたフレーム 16/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
var outBuf = outImg.data;
function renderFrame(t) {
var phase = Math.floor((t % LOOP_MS) / 160) % 2;
var set = phase === 0 ? RGB_8 : RGB_16;
lockRetroPalette(baseBuf, CALC_W, CALC_H, RETRO_BLOCK_PX, set, outBuf);
}
測定値: ループ内でフレームの34.0%が書き換わり、強度は平均91.1、動いたフレームは16/23です。
08文字列ひとつで濃淡が変わる明度→アスキー文字マッピング
08番は各ブロックの明るさの値を、濃さの違う一文字が並ぶ固定の文字列から選び、その文字をブロックの色で描いて写真を文字絵に変えます。紹介ページのテキストアート背景に向く手法です。値を決めているのはBRIGHTNESS_RAMPで、塗り色を決めてからfillText()を呼ぶ毎フレームのループの中、ブロックごとに一度brightnessToChar()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<p class="vd__caption">明るさ値を文字へ変えるアスキーマップ</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-paper);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(23, 20, 26, .78);
color: $subject-cream;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd { cursor: default; }
}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);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(23, 20, 26, 0.78);
color: #fff7e6;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
var BRIGHTNESS_RAMP = ' .:-=+*#%@';
var ASCII_CELL_PX = 6;
function brightnessToChar(baseBuf, w, h, cellPx, ramp, outCtx) {
outCtx.fillStyle = '#0b0b10';
outCtx.fillRect(0, 0, w, h);
outCtx.font = Math.max(5, cellPx) + 'px monospace';
outCtx.textBaseline = 'top';
for (var by = 0; by < h; by += cellPx) {
var bh = Math.min(cellPx, h - by);
for (var bx = 0; bx < w; bx += cellPx) {
var bw = Math.min(cellPx, w - bx);
var rs = 0, gs = 0, bs = 0, count = 0;
for (var y = 0; y < bh; y += 1) {
for (var x = 0; x < bw; x += 1) {
var idx = ((by + y) * w + (bx + x)) * 4;
rs += baseBuf[idx]; gs += baseBuf[idx + 1]; bs += baseBuf[idx + 2]; count += 1;
}
}
var r = rs / count, g = gs / count, b = bs / count;
var lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
var ci = Math.min(ramp.length - 1, Math.floor(lum * ramp.length));
var ch = ramp.charAt(ci);
outCtx.fillStyle = 'rgb(' + Math.round(r) + ',' + Math.round(g) + ',' + Math.round(b) + ')';
outCtx.fillText(ch, bx, by);
}
}
}
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415081);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var baseBuf = baseImg.data;
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
function renderFrame(t) {
var wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 3;
var cellPx = Math.max(3, Math.min(11, Math.round(ASCII_CELL_PX + wave)));
brightnessToChar(baseBuf, CALC_W, CALC_H, cellPx, BRIGHTNESS_RAMP, outCtx);
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelAsciiBrightnessMap.module.scss";
export default function PixelAsciiBrightnessMap() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
if (!root || !canvas) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
const accent = ["#ff5c8a", "#2fe6c6", "#ffb703", "#8a5cd6", "#3ddc97", "#ff7a5e"];
for (let m = 0; m < 8; m += 1) {
const ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
const BRIGHTNESS_RAMP = " .:-=+*#%@";
const ASCII_CELL_PX = 6;
function brightnessToChar(baseBuf: Uint8ClampedArray, w: number, h: number, cellPx: number, ramp: string, outCtx: CanvasRenderingContext2D) {
outCtx.fillStyle = "#0b0c10";
outCtx.fillRect(0, 0, w, h);
outCtx.font = cellPx + "px monospace";
outCtx.textBaseline = "top";
for (let by = 0; by < h; by += cellPx) {
const bh = Math.min(cellPx, h - by);
for (let bx = 0; bx < w; bx += cellPx) {
const bw = Math.min(cellPx, w - bx);
let rs = 0, gs = 0, bs = 0, count = 0;
for (let y = 0; y < bh; y += 1) {
for (let x = 0; x < bw; x += 1) {
const idx = ((by + y) * w + (bx + x)) * 4;
rs += baseBuf[idx]; gs += baseBuf[idx + 1]; bs += baseBuf[idx + 2]; count += 1;
}
}
const avgR = rs / count, avgG = gs / count, avgB = bs / count;
const brightness = (avgR * 0.299 + avgG * 0.587 + avgB * 0.114) / 255;
const rampIdx = Math.min(ramp.length - 1, Math.floor(brightness * ramp.length));
const ch = ramp[rampIdx];
outCtx.fillStyle = "rgb(" + Math.round(avgR) + "," + Math.round(avgG) + "," + Math.round(avgB) + ")";
outCtx.fillText(ch, bx, by);
}
}
}
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415081);
const baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const baseBuf = baseImg.data;
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
function renderFrame(t: number) {
const wobble = Math.sin(t * Math.PI * 4 / LOOP_MS) * 2;
const cellPx = Math.max(3, Math.round(ASCII_CELL_PX + wobble));
brightnessToChar(baseBuf, CALC_W, CALC_H, cellPx, BRIGHTNESS_RAMP, outCtx);
}
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.vd__caption}>밝기 값을 문자로 바꾸는 아스키 맵</p>
</div>
</main>
);
}08 · 明度→アスキー文字マッピング — 計測 累積面積 59.368% · 動いたフレーム 19/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
function brightnessToChar(baseBuf, w, h, cellPx, ramp, outCtx) {
outCtx.fillStyle = '#0b0b10';
outCtx.fillRect(0, 0, w, h);
outCtx.font = Math.max(5, cellPx) + 'px monospace';
outCtx.textBaseline = 'top';
for (var by = 0; by < h; by += cellPx) {
var bh = Math.min(cellPx, h - by);
for (var bx = 0; bx < w; bx += cellPx) {
var bw = Math.min(cellPx, w - bx);
var rs = 0, gs = 0, bs = 0, count = 0;
for (var y = 0; y < bh; y += 1) {
for (var x = 0; x < bw; x += 1) {
var idx = ((by + y) * w + (bx + x)) * 4;
rs += baseBuf[idx]; gs += baseBuf[idx + 1]; bs += baseBuf[idx + 2]; count += 1;
}
}
var r = rs / count, g = gs / count, b = bs / count;
var lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
var ci = Math.min(ramp.length - 1, Math.floor(lum * ramp.length));
var ch = ramp.charAt(ci);
outCtx.fillStyle = 'rgb(' + Math.round(r) + ',' + Math.round(g) + ',' + Math.round(b) + ')';
outCtx.fillText(ch, bx, by);
}
}
}
測定値: ループ内でフレームの59.4%が書き換わり、強度は平均87.1、動いたフレームは19/23です。
09スライダー値ひとつで割合が変わるビフォーアフター・ヒーロー比較シーン
09番はパーセンテージの値から横方向のしきい値を決め、その左側にドット絵変換結果を、右側に元写真を同じキャンバスへ描き、スライダーの位置ひとつで見える割合を変えます。ランディングページのヒーロー比較スライダーに向いています。値を決めているのはCOMPARE_SLIDER_PERCENTで、各ピクセルのx位置としきい値を比較する毎フレームのループの中、ピクセルごとに一度splitCompare()へ渡されます。
<main class="stage">
<div class="vd is-demo" id="vdRoot">
<canvas class="vd__canvas" id="vdCanvas" width="480" height="300"></canvas>
<div class="vd__divider" id="vdDivider" role="slider" tabindex="0" aria-label="compare position" aria-valuemin="0" aria-valuemax="100" aria-valuenow="20">
<span class="vd__handle"></span>
</div>
<input class="vd__range" id="vdRange" type="range" min="0" max="100" value="20" step="1" aria-label="compare position range">
<p class="vd__caption">スライダーで分ける元画像とドット変換の比較</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-ink);
.vd {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
box-shadow: $shadow-raised;
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__divider {
position: absolute;
top: 0;
bottom: 0;
width: 2px;
background: $subject-cream;
cursor: ew-resize;
touch-action: none;
}
.vd__handle {
position: absolute;
top: 50%;
left: 50%;
width: $sp-6;
height: $sp-6;
transform: translate(-50%, -50%);
border-radius: $r-pill;
background: $subject-cream;
box-shadow: $shadow-raised;
}
.vd__range {
position: absolute;
left: $sp-3;
right: $sp-3;
top: $sp-3;
margin: 0;
opacity: 0;
cursor: pointer;
}
.vd__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 72%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.vd { cursor: default; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.vd {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__canvas {
display: block;
width: 100%;
height: 100%;
}
.vd__divider {
position: absolute;
top: 0;
bottom: 0;
width: 2px;
background: #fff7e6;
cursor: ew-resize;
touch-action: none;
}
.vd__handle {
position: absolute;
top: 50%;
left: 50%;
width: 24px;
height: 24px;
transform: translate(-50%, -50%);
border-radius: 999px;
background: #fff7e6;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.vd__range {
position: absolute;
left: 12px;
right: 12px;
top: 12px;
margin: 0;
opacity: 0;
cursor: pointer;
}
.vd__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 72%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.vd__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.vd {
cursor: default;
}
}(function () {
var root = document.getElementById('vdRoot');
var canvas = document.getElementById('vdCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
var divider = document.getElementById('vdDivider');
var range = document.getElementById('vdRange');
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#8fc7ff');
sky.addColorStop(1, '#fff2d6');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,247,230,.9)');
glow.addColorStop(1, 'rgba(255,247,230,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#ffe27a';
pctx.fill();
var hillColors = ['#6ec8ff', '#2b9d74', '#17141a'];
for (var i = 0; i < 3; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 24);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
var accent = ['#ff5c8a', '#2fe6c6', '#ffb703', '#8a5cd6', '#3ddc97', '#ff7a5e'];
for (var m = 0; m < 8; m += 1) {
var ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (var k = 0; k < 60; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.22) + ')';
pctx.fill();
}
}
function blockAverage(buf, w, h, blockPx, outBuf) {
for (var by = 0; by < h; by += blockPx) {
var bh = Math.min(blockPx, h - by);
for (var bx = 0; bx < w; bx += blockPx) {
var bw = Math.min(blockPx, w - bx);
var rs = 0, gs = 0, bs = 0, count = 0;
for (var y = 0; y < bh; y += 1) {
for (var x = 0; x < bw; x += 1) {
var idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
var r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (var y2 = 0; y2 < bh; y2 += 1) {
for (var x2 = 0; x2 < bw; x2 += 1) {
var idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
function splitCompare(beforeBuf, afterBuf, w, h, percent, outBuf) {
var threshold = Math.round((percent / 100) * w);
for (var y = 0; y < h; y += 1) {
for (var x = 0; x < w; x += 1) {
var idx = (y * w + x) * 4;
var src = x < threshold ? afterBuf : beforeBuf;
outBuf[idx] = src[idx]; outBuf[idx + 1] = src[idx + 1]; outBuf[idx + 2] = src[idx + 2]; outBuf[idx + 3] = 255;
}
}
}
var HERO_BLOCK_PX = 10;
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var COMPARE_SLIDER_PERCENT = 20;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawScene(octx, CALC_W, CALC_H, 415091);
var baseImg = octx.getImageData(0, 0, CALC_W, CALC_H);
var beforeBuf = baseImg.data;
var afterBuf = new Uint8ClampedArray(beforeBuf.length);
blockAverage(beforeBuf, CALC_W, CALC_H, HERO_BLOCK_PX, afterBuf);
var out = document.createElement('canvas');
out.width = CALC_W; out.height = CALC_H;
var outCtx = out.getContext('2d');
var outImg = outCtx.createImageData(CALC_W, CALC_H);
var outBuf = outImg.data;
var userPercent = COMPARE_SLIDER_PERCENT;
var dragging = false;
function setPercent(p, syncRender) {
userPercent = Math.max(0, Math.min(100, p));
divider.style.left = userPercent + '%';
divider.setAttribute('aria-valuenow', String(Math.round(userPercent)));
if (range) range.value = String(Math.round(userPercent));
if (syncRender && reduce) draw(lastElapsed);
}
function percentFromClientX(clientX) {
var rect = wrap.getBoundingClientRect();
var rel = (clientX - rect.left) / Math.max(1, rect.width);
return rel * 100;
}
divider.addEventListener('pointerdown', function (ev) {
dragging = true;
divider.setPointerCapture(ev.pointerId);
});
divider.addEventListener('pointermove', function (ev) {
if (!dragging) return;
setPercent(percentFromClientX(ev.clientX), true);
});
divider.addEventListener('pointerup', function () { dragging = false; });
divider.addEventListener('keydown', function (ev) {
if (ev.key === 'ArrowLeft') { setPercent(userPercent - 4, true); ev.preventDefault(); }
else if (ev.key === 'ArrowRight') { setPercent(userPercent + 4, true); ev.preventDefault(); }
});
if (range) {
range.addEventListener('input', function () { setPercent(Number(range.value), true); });
}
setPercent(userPercent, false);
function renderFrame(t) {
var wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 30;
var percent = Math.max(0, Math.min(100, userPercent + wave));
splitCompare(beforeBuf, afterBuf, CALC_W, CALC_H, percent, outBuf);
}
var cw = 480, ch = 300;
function fit() {
var r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas.width = cw; canvas.height = ch;
draw(lastElapsed);
}
var lastElapsed = 0;
function draw(elapsed) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx.imageSmoothingEnabled = false;
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var playing = !reduce;
var elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove('is-demo'); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./PixelBeforeAfterHeroScene.module.scss";
export default function PixelBeforeAfterHeroScene() {
const rootRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const dividerRef = useRef<HTMLDivElement>(null);
const rangeRef = useRef<HTMLInputElement>(null);
useEffect(() => {
const root = rootRef.current;
const canvas = canvasRef.current;
const divider = dividerRef.current;
const range = rangeRef.current;
if (!root || !canvas || !divider || !range) return;
const ctx = canvas.getContext("2d");
if (!ctx) return;
const wrap = canvas.parentElement as HTMLElement;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
function drawScene(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#8fc7ff");
sky.addColorStop(1, "#fff2d6");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.72, sunY = h * 0.28, sunR = h * 0.2;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,247,230,.9)");
glow.addColorStop(1, "rgba(255,247,230,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#ffe27a";
pctx.fill();
const hillColors = ["#6ec8ff", "#2b9d74", "#17141a"];
for (let i = 0; i < 3; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 24);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.06 + i * 2.4 + seed * 0.0003) * h * 0.05 + Math.sin(x * 0.17 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
const accent = ["#ff5c8a", "#2fe6c6", "#ffb703", "#8a5cd6", "#3ddc97", "#ff7a5e"];
for (let m = 0; m < 8; m += 1) {
const ax = rand() * w, ay = h * (0.56 + rand() * 0.38), ar = h * (0.025 + rand() * 0.05);
pctx.beginPath();
pctx.arc(ax, ay, ar, 0, Math.PI * 2);
pctx.fillStyle = accent[m % accent.length];
pctx.fill();
}
for (let k = 0; k < 60; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.3 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.22) + ")";
pctx.fill();
}
}
function blockAverage(buf: Uint8ClampedArray, w: number, h: number, blockPx: number, outBuf: Uint8ClampedArray) {
for (let by = 0; by < h; by += blockPx) {
const bh = Math.min(blockPx, h - by);
for (let bx = 0; bx < w; bx += blockPx) {
const bw = Math.min(blockPx, w - bx);
let rs = 0, gs = 0, bs = 0, count = 0;
for (let y = 0; y < bh; y += 1) {
for (let x = 0; x < bw; x += 1) {
const idx = ((by + y) * w + (bx + x)) * 4;
rs += buf[idx]; gs += buf[idx + 1]; bs += buf[idx + 2]; count += 1;
}
}
const r = Math.round(rs / count), g = Math.round(gs / count), b = Math.round(bs / count);
for (let y2 = 0; y2 < bh; y2 += 1) {
for (let x2 = 0; x2 < bw; x2 += 1) {
const idx2 = ((by + y2) * w + (bx + x2)) * 4;
outBuf[idx2] = r; outBuf[idx2 + 1] = g; outBuf[idx2 + 2] = b; outBuf[idx2 + 3] = 255;
}
}
}
}
}
function splitCompare(beforeBuf: Uint8ClampedArray, afterBuf: Uint8ClampedArray, w: number, h: number, percent: number, outBuf: Uint8ClampedArray) {
const splitX = Math.round(w * (percent / 100));
for (let y = 0; y < h; y += 1) {
for (let x = 0; x < w; x += 1) {
const idx = (y * w + x) * 4;
const src = x < splitX ? afterBuf : beforeBuf;
outBuf[idx] = src[idx]; outBuf[idx + 1] = src[idx + 1]; outBuf[idx + 2] = src[idx + 2]; outBuf[idx + 3] = 255;
}
}
}
const CALC_W = 160, CALC_H = 100;
const LOOP_MS = 2000;
const HERO_BLOCK_PX = 10;
const COMPARE_SLIDER_PERCENT = 20;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawScene(octx, CALC_W, CALC_H, 415091);
const beforeImg = octx.getImageData(0, 0, CALC_W, CALC_H);
const beforeBuf = beforeImg.data;
const afterBuf = new Uint8ClampedArray(beforeBuf.length);
blockAverage(beforeBuf, CALC_W, CALC_H, HERO_BLOCK_PX, afterBuf);
const out = document.createElement("canvas");
out.width = CALC_W; out.height = CALC_H;
const outCtx = out.getContext("2d") as CanvasRenderingContext2D;
const outImg = outCtx.createImageData(CALC_W, CALC_H);
const outBuf = outImg.data;
let userPercent = COMPARE_SLIDER_PERCENT;
let dragging = false;
function setPercent(p: number, syncRender: boolean) {
userPercent = Math.max(0, Math.min(100, p));
divider!.style.left = userPercent + "%";
divider!.setAttribute("aria-valuenow", String(Math.round(userPercent)));
range!.value = String(Math.round(userPercent));
if (syncRender && reduce) draw(lastElapsed);
}
function pctFromClientX(clientX: number) {
const rect = wrap.getBoundingClientRect();
const rel = (clientX - rect.left) / Math.max(1, rect.width);
return rel * 100;
}
function onDividerDown(ev: PointerEvent) {
dragging = true;
divider!.setPointerCapture(ev.pointerId);
setPercent(pctFromClientX(ev.clientX), true);
}
function onDividerMove(ev: PointerEvent) {
if (!dragging) return;
setPercent(pctFromClientX(ev.clientX), true);
}
function onDividerUp(ev: PointerEvent) {
dragging = false;
divider!.releasePointerCapture(ev.pointerId);
}
function onDividerKey(ev: KeyboardEvent) {
if (ev.key === "ArrowLeft") { setPercent(userPercent - 4, true); ev.preventDefault(); }
else if (ev.key === "ArrowRight") { setPercent(userPercent + 4, true); ev.preventDefault(); }
}
function onRangeInput() {
setPercent(Number(range!.value), true);
}
divider.addEventListener("pointerdown", onDividerDown);
divider.addEventListener("pointermove", onDividerMove);
divider.addEventListener("pointerup", onDividerUp);
divider.addEventListener("keydown", onDividerKey);
range.addEventListener("input", onRangeInput);
function renderFrame(t: number) {
const wave = Math.sin(t * Math.PI * 4 / LOOP_MS) * 30;
const percent = userPercent + wave;
splitCompare(beforeBuf, afterBuf, CALC_W, CALC_H, percent, outBuf);
}
let cw = 480, ch = 300;
function fit() {
const r = wrap.getBoundingClientRect();
cw = Math.max(1, Math.round(r.width));
ch = Math.max(1, Math.round(r.height));
canvas!.width = cw; canvas!.height = ch;
draw(lastElapsed);
}
let lastElapsed = 0;
function draw(elapsed: number) {
lastElapsed = elapsed;
renderFrame(elapsed);
outCtx.putImageData(outImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let playing = !reduce;
let elapsed = reduce ? LOOP_MS * 0.5 : 0;
if (reduce) { root.classList.remove("is-demo"); }
setPercent(userPercent, false);
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (elapsed >= LOOP_MS) elapsed -= LOOP_MS;
}
draw(elapsed);
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
divider!.removeEventListener("pointerdown", onDividerDown);
divider!.removeEventListener("pointermove", onDividerMove);
divider!.removeEventListener("pointerup", onDividerUp);
divider!.removeEventListener("keydown", onDividerKey);
range!.removeEventListener("input", onRangeInput);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.vd} is-demo`} ref={rootRef}>
<canvas className={styles.vd__canvas} ref={canvasRef} width={480} height={300} />
<div
className={styles.vd__divider}
ref={dividerRef}
role="slider"
tabIndex={0}
aria-label="compare position"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={20}
>
<div className={styles.vd__handle} />
</div>
<input
className={styles.vd__range}
ref={rangeRef}
type="range"
min={0}
max={100}
defaultValue={20}
aria-label="compare position range"
/>
<p className={styles.vd__caption}>슬라이더로 가르는 원본과 도트 변환 비교</p>
</div>
</main>
);
}09 · ビフォーアフター・ヒーロー比較シーン — 計測 累積面積 10.295% · 動いたフレーム 17/23GODRICH オリジナル · SCSS と React は zip に同梱 · パスワードを見る
function splitCompare(beforeBuf, afterBuf, w, h, percent, outBuf) {
var threshold = Math.round((percent / 100) * w);
for (var y = 0; y < h; y += 1) {
for (var x = 0; x < w; x += 1) {
var idx = (y * w + x) * 4;
var src = x < threshold ? afterBuf : beforeBuf;
outBuf[idx] = src[idx]; outBuf[idx + 1] = src[idx + 1]; outBuf[idx + 2] = src[idx + 2]; outBuf[idx + 3] = 255;
}
}
}
測定値: ループ内でフレームの10.3%が書き換わり、強度は平均54.3、動いたフレームは17/23です。
アクセシビリティと reduced-motion で止まる場所
prefers-reduced-motion: reduceが有効なとき、01番から08番はis-demoクラスが外れてアニメーションのフレームループが止まりますが、どの項目も先頭のフレームではなく、それぞれのループの中間地点—LOOP_MSの半分が経過した状態—で止まります。そのため動きに敏感な読者にも、各パターンの典型的な見た目が空白の初期状態の代わりに残ります。09番だけは仕組みが違い、しきい値の値がフレームごとではなくポインター・キーボード・レンジ入力の操作でしか変わりません。区切り線はすでにArrowLeftとArrowRightのキー入力に反応して4ポイントずつ位置を動かし、デモ上部の<input type="range">も同じsetPercent()を呼ぶので、ポインターに一度も触れない読者でもキーボードだけでスライダーを動かせます。getImageData()とrequestAnimationFrame()はこの9つすべてが土台にしているAPIで、prefers-reduced-motionメディアクエリとあわせてMDNに解説があります。
9つのパターンを面積と強度で測る
| 項目 | 面積% | 強度 | 動いたフレーム |
|---|---|---|---|
| 01 pixelart-block-downsample | 39.5 | 42.5 | 15/23 |
| 02 pixelart-palette-quantize | 59.0 | 163.0 | 17/23 |
| 03 pixelart-dither-diffuse | 75.3 | 88.6 | 18/23 |
| 04 pixelart-posterize-band | 70.7 | 63.8 | 18/23 |
| 05 pixelart-pointer-lens-radius | 41.1 | 47.2 | 23/23 |
| 06 pixelart-reveal-transition-step | 56.0 | 45.6 | 15/23 |
| 07 pixelart-retro-palette-lock | 34.0 | 91.1 | 16/23 |
| 08 pixelart-ascii-brightness-map | 59.4 | 87.1 | 19/23 |
| 09 pixelart-before-after-hero-scene | 10.3 | 54.3 | 17/23 |
この画像 ドット絵 変換は画像 ドット絵 変換 サイトと何が違うのか?
画像 ドット絵 変換 サイトの多くはブロックサイズ・パレット・ディザリングをボタンひとつといくつかのプリセットの裏に隠しています。このページに並んでいるのは、そうしたサイトが裏側で走らせている9つの計算そのもの—ブロック平均・パレット量子化・誤差拡散ディザリング・明度バンドほか—を、最後まで読める短い関数ひとつずつとして見せたものです。02番・04番・07番は新しい層を足すのではなく既存の色をパレットへ固定したり帯にまとめたりするので、ネイティブのCSSfilter()関数よりも画像の上にかけるフィルター処理に近く、01番・03番・05番・06番・08番・09番はピクセルの並びそのものを作り替えるので、CSSのフィルターだけでは届きません。同じ「毎フレーム値をひとつ読む」という考え方でまとめた編としては、css 画像 フィルター 9パターン — 値ひとつで色が変わるが色だけを変える半分をより詳しく扱っていて、パーティクル アニメーション canvas 9種 — ライブラリなしの作り方は静止した変換ではなく動く形が欲しいときの近い編で、画像 編集 javascript のトリミングUI 9選はこの9つが走る前に写真を切り抜く場所です。
画像 ドット絵 変換が壊れる場所 — 大きな写真と低スペック端末
上の9つはどれもCALC_W × CALC_Hのピクセルをアニメーションフレームごとに少なくとも一度getImageData()/putImageData()で読み書きします。デモの160×100のキャンバスなら1回あたり16,000回の読み取りで済みますが、同じピクセルごとのループを4000pxの写真アップロードにかけるとその200倍を超え、画像 ドット絵 変換 aiを謳うオンラインツールが本番解像度でつまずくのと同じ壁にぶつかります。05番と09番はこの毎フレームのコストにもう一段のコストを重ねていて、05番はポインターの位置が変わるたびに表示するバッファを計算し直し、09番はキー操作やレンジ入力のたびに分割のしきい値を計算し直すので、元の写真自体が大きいならこの二つを先に計測する価値があります。このピクセル単位のコストは、ダウンロードが役に立つ場所でもあります—9つすべてのindex.html/style.scssを開く895nmedwはこの段落にあるので、どのパターンを先に試すか決めてから残りを解凍してください。
画像 ドット絵 変換が実際に使われる場所
この9つの多くは、訪問者がずっと眺め続けるものではなく、静的な素材に一度だけ焼き込む変換として使われます—画像 ドット絵 変換 アプリとして検索する人が実際に欲しいのもこの使い方です。01番のブロック平均はプロフィール写真をアイコンに向くブロックサイズでドット絵アイコンに変え、02番のパレット量子化はヒーロー画像を8ビットゲーム風バナーと同じ色数に固定し、04番の明度バンドはイラストカードの陰影を滑らかな階調ではなく段階的な帯にし、07番のレトロパレット固定はレトロイベントページのヒーロー画像を昔のゲーム画面の色数に絞り、08番の明度→文字マッピングは紹介ページのテキストアート背景に写真を変えます。
残りの4つはページが読み込まれた後も値が変わり続けるので、インタラクティブなままです。03番の誤差拡散ディザリングは、素材がすでにグレースケールで白黒のドットとして見せたいときに向いています。05番のポインター半径レンズはカーソル周りの円の内側だけブロックサイズを大きくするので、商品写真の上に重ねる虫眼鏡インタラクションに合います。06番の段階的リビールはギャラリー画像の読み込み演出でブロックサイズを縮め、09番のビフォーアフターシーンはランディングページのヒーローで元写真とドット絵変換結果が見える割合をスライダーの位置ひとつに結びつけます。
3つの値を振り切ってドット絵の限界を見る
| バリエーション | 値の変更 | 感触 |
|---|---|---|
| ブロックサイズの値を調整する | 01 PIXEL_BLOCK_SIZE_PX 4 → 24 |
ブロックサイズを4から24に上げると一つ一つのドットが大きくなり、元の形がより崩れて見えます。 |
| レンズ半径の値を調整する | 05 PIXEL_LENS_RADIUS_PX 40 → 160 |
レンズ半径を40から160に広げるとポインター周りでドットが大きく見える範囲が広がります。 |
| 比較スライダーの値を調整する | 09 COMPARE_SLIDER_PERCENT 20 → 80 |
比較スライダー値を20から80に動かすと画面でドット変換結果が見える割合が増えます。 |
この画像 ドット絵 変換に関するよくある質問
この9つはアップロードした写真や外部の画像を読み込みますか?
いいえ。drawScene()はキャンバスの図形とグラデーションだけでシーンを一から描き、seedRand()で種を固定しているので同じシード値なら常に同じ配置になります。9つの計算はすべてそのキャンバスバッファの上だけで動き、アップロードされた画像もライブラリの読み込みもゼロです。
フルサイズの写真でいちばん負荷が高いのはどれですか?
03番の誤差拡散ディザリングは強度の測定値が88.6ともっとも高く、フレームの75.3%も書き換えます。すべてのピクセルのすべてのチャンネルが隣からの誤差を読み取り、さらに次へ誤差を書き込むためで、02番・04番・08番より先に本番解像度で試す価値があります。
これはピクセルアートのパーティクル表現と何が違いますか?
この9つはどれも一枚の写真のようなシーンから始まり、すでにあるピクセルの色を作り替えたり間引いたりするだけです。パーティクル表現は火花や粒子のような小さな図形を個別に増やしたり消したりして動かすもので、上の9つのブロック・パレット・ディザリングの計算とは別の考え方です。









