09ビフォーアフター・ヒーロー比較シーン
スライダーの位置の値に応じて元写真とドット絵変換結果を一画面で分けるヒーロー場面を作る。
使いどころランディングページのヒーロー比較スライダー
COMPARE_SLIDER_PERCENTsplitCompareaddEventListener
<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 に同梱 · 画像 ドット絵 変換 9パターン — キャンバスでドット絵を作る


