04ポインターを追う屈折レンズ
ポインター周辺の円形領域だけ、光が屈折したようにピクセルが歪んで見える。
使いどころ商品詳細画像、ポートフォリオプレビュー
LENS_RADIUSLENS_REFRACT_KvirtualLens

<main class="stage">
<div class="rv is-demo" id="rvRoot" tabindex="0">
<canvas class="rv__canvas" id="rvCanvas" width="480" height="300"></canvas>
<p class="rv__caption">ポインターを追いかける円形レンズの中で、画像が水中のように屈折して見える。</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-paper);
.rv {
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;
cursor: crosshair;
outline: none;
}
.rv__canvas {
display: block;
width: 100%;
height: 100%;
}
.rv__caption {
position: absolute;
left: $sp-3;
bottom: $sp-3;
max-width: 78%;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(23, 20, 26, .68);
color: $stage-paper;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.rv__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.rv { cursor: pointer; }
}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);
}
.rv {
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);
cursor: crosshair;
outline: none;
}
.rv__canvas {
display: block;
width: 100%;
height: 100%;
}
.rv__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 78%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(23, 20, 26, 0.68);
color: #ffffff;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.rv__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.rv {
cursor: pointer;
}
}(function () {
// 기법: 렌즈 중심에서의 거리 기반 반경 변위 필드(스넬 굴절 근사)로 원 안쪽만 픽셀을 재배치한다.
// 렌즈 표면에는 sin 파동을 얹어 정적 돋보기가 아니라 물 렌즈처럼 표면이 흔들리게 한다.
var root = document.getElementById('rvRoot');
var canvas = document.getElementById('rvCanvas');
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 drawPhotoBase(pctx, w, h, seed) {
var rand = seedRand(seed);
var sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, '#d8e9ff');
sky.addColorStop(1, '#f6f2ff');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.3, sunY = h * 0.22, sunR = h * 0.14;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.6);
glow.addColorStop(0, 'rgba(255,255,255,.9)');
glow.addColorStop(1, 'rgba(255,255,255,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
var gridColors = ['#a9c9ff', '#6f97e0', '#3d5fa8'];
for (var i = 0; i < gridColors.length; i += 1) {
var baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
var step = Math.max(2, w / 22);
for (var x = 0; x <= w; x += step) {
var n = Math.sin(x * 0.055 + i * 1.9 + seed * 0.00025) * h * 0.035;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = gridColors[i];
pctx.fill();
}
for (var k = 0; k < 46; k += 1) {
var px = rand() * w, py = rand() * h, r = rand() * 1.1 + 0.4;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.12 + rand() * 0.2) + ')';
pctx.fill();
}
}
var CALC_W = 160, CALC_H = 100;
var LENS_RADIUS = 26;
var LENS_REFRACT_K = 1.7;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawPhotoBase(octx, CALC_W, CALC_H, 419041);
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 sampleBase(sx, sy) {
sx = sx < 0 ? 0 : (sx > CALC_W - 1 ? CALC_W - 1 : sx);
sy = sy < 0 ? 0 : (sy > CALC_H - 1 ? CALC_H - 1 : sy);
var i = (Math.round(sy) * CALC_W + Math.round(sx)) * 4;
return [baseBuf[i], baseBuf[i + 1], baseBuf[i + 2]];
}
var lens = { x: CALC_W * 0.5, y: CALC_H * 0.5 };
function renderFrame(t) {
var idx = 0;
for (var y = 0; y < CALC_H; y += 1) {
for (var x = 0; x < CALC_W; x += 1) {
var dxp = x - lens.x, dyp = y - lens.y;
var dist = Math.sqrt(dxp * dxp + dyp * dyp);
var rgb;
if (dist < LENS_RADIUS) {
var norm = dist / LENS_RADIUS;
var bulge = (1 - norm) * (1 - norm);
var ripple = Math.sin(dist * 0.9 - t * 0.012) * 0.6 * (1 - norm);
var factor = LENS_REFRACT_K * bulge + ripple;
var ux = dist > 0.0001 ? dxp / dist : 0;
var uy = dist > 0.0001 ? dyp / dist : 0;
var sx = x - ux * factor * 4;
var sy = y - uy * factor * 4;
rgb = sampleBase(sx, sy);
var shine = Math.max(-20, Math.min(50, (1 - norm) * 30 - ripple * 14));
rgb = [
Math.max(0, Math.min(255, rgb[0] + shine)),
Math.max(0, Math.min(255, rgb[1] + shine)),
Math.max(0, Math.min(255, rgb[2] + shine))
];
} else {
rgb = sampleBase(x, y);
}
outBuf[idx] = rgb[0];
outBuf[idx + 1] = rgb[1];
outBuf[idx + 2] = rgb[2];
outBuf[idx + 3] = 255;
idx += 4;
}
}
}
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(t) {
lastElapsed = t;
renderFrame(t);
outCtx.putImageData(outImg, 0, 0);
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;
if (reduce) { root.classList.remove('is-demo'); }
var pointerActive = false;
function toGrid(e) {
var r = wrap.getBoundingClientRect();
var t0 = (e.touches && e.touches[0]) ? e.touches[0] : e;
return {
x: (t0.clientX - r.left) / r.width * CALC_W,
y: (t0.clientY - r.top) / r.height * CALC_H
};
}
wrap.addEventListener('pointerenter', function () { pointerActive = true; });
wrap.addEventListener('pointermove', function (e) {
pointerActive = true;
lens = toGrid(e);
if (reduce) draw(lastElapsed);
});
wrap.addEventListener('pointerleave', function () { pointerActive = false; });
wrap.addEventListener('keydown', function (e) {
var moved = true;
if (e.key === 'ArrowLeft') lens.x = Math.max(0, lens.x - 6);
else if (e.key === 'ArrowRight') lens.x = Math.min(CALC_W, lens.x + 6);
else if (e.key === 'ArrowUp') lens.y = Math.max(0, lens.y - 6);
else if (e.key === 'ArrowDown') lens.y = Math.min(CALC_H, lens.y + 6);
else moved = false;
if (moved) {
e.preventDefault();
pointerActive = true;
if (reduce) draw(lastElapsed);
}
});
wrap.tabIndex = 0;
function virtualLens(t) {
return {
x: CALC_W * (0.5 + 0.32 * Math.sin(t * 0.0011)),
y: CALC_H * (0.5 + 0.3 * Math.cos(t * 0.0017))
};
}
var lastTs = null;
var elapsed = 0;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (!pointerActive) lens = virtualLens(elapsed);
draw(elapsed);
}
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./RippleCanvasRefractionLens.module.scss";
export default function RippleCanvasRefractionLens() {
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") as CanvasRenderingContext2D;
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 drawPhotoBase(pctx: CanvasRenderingContext2D, w: number, h: number, seed: number) {
const rand = seedRand(seed);
const sky = pctx.createLinearGradient(0, 0, 0, h);
sky.addColorStop(0, "#d8e9ff");
sky.addColorStop(1, "#f6f2ff");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.3, sunY = h * 0.22, sunR = h * 0.14;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.6);
glow.addColorStop(0, "rgba(255,255,255,.9)");
glow.addColorStop(1, "rgba(255,255,255,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
const gridColors = ["#a9c9ff", "#6f97e0", "#3d5fa8"];
for (let i = 0; i < gridColors.length; i += 1) {
const baseY = h * (0.48 + i * 0.16);
pctx.beginPath();
pctx.moveTo(0, h);
const step = Math.max(2, w / 22);
for (let x = 0; x <= w; x += step) {
const n = Math.sin(x * 0.055 + i * 1.9 + seed * 0.00025) * h * 0.035;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = gridColors[i];
pctx.fill();
}
for (let k = 0; k < 46; k += 1) {
const px = rand() * w, py = rand() * h, r = rand() * 1.1 + 0.4;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.12 + rand() * 0.2) + ")";
pctx.fill();
}
}
const CALC_W = 160, CALC_H = 100;
const LENS_RADIUS = 26;
const LENS_REFRACT_K = 1.7;
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
drawPhotoBase(octx, CALC_W, CALC_H, 419041);
const baseBuf = octx.getImageData(0, 0, CALC_W, CALC_H).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 sampleBase(sx: number, sy: number): [number, number, number] {
sx = sx < 0 ? 0 : (sx > CALC_W - 1 ? CALC_W - 1 : sx);
sy = sy < 0 ? 0 : (sy > CALC_H - 1 ? CALC_H - 1 : sy);
const i = (Math.round(sy) * CALC_W + Math.round(sx)) * 4;
return [baseBuf[i], baseBuf[i + 1], baseBuf[i + 2]];
}
let lens = { x: CALC_W * 0.5, y: CALC_H * 0.5 };
function renderFrame(t: number) {
let idx = 0;
for (let y = 0; y < CALC_H; y += 1) {
for (let x = 0; x < CALC_W; x += 1) {
const dxp = x - lens.x, dyp = y - lens.y;
const dist = Math.sqrt(dxp * dxp + dyp * dyp);
let rgb: [number, number, number];
if (dist < LENS_RADIUS) {
const norm = dist / LENS_RADIUS;
const bulge = (1 - norm) * (1 - norm);
const ripple = Math.sin(dist * 0.9 - t * 0.012) * 0.6 * (1 - norm);
const factor = LENS_REFRACT_K * bulge + ripple;
const ux = dist > 0.0001 ? dxp / dist : 0;
const uy = dist > 0.0001 ? dyp / dist : 0;
const sx = x - ux * factor * 4;
const sy = y - uy * factor * 4;
const base = sampleBase(sx, sy);
const shine = Math.max(-20, Math.min(50, (1 - norm) * 30 - ripple * 14));
rgb = [
Math.max(0, Math.min(255, base[0] + shine)),
Math.max(0, Math.min(255, base[1] + shine)),
Math.max(0, Math.min(255, base[2] + shine))
];
} else {
rgb = sampleBase(x, y);
}
outBuf[idx] = rgb[0];
outBuf[idx + 1] = rgb[1];
outBuf[idx + 2] = rgb[2];
outBuf[idx + 3] = 255;
idx += 4;
}
}
}
let cw = 480, ch = 300;
let lastElapsed = 0;
function draw(t: number) {
lastElapsed = t;
renderFrame(t);
outCtx.putImageData(outImg, 0, 0);
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(out, 0, 0, CALC_W, CALC_H, 0, 0, cw, ch);
}
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);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const playing = !reduce;
if (reduce) { root.classList.remove("is-demo"); }
let pointerActive = false;
function toGrid(e: PointerEvent) {
const r = wrap.getBoundingClientRect();
return {
x: (e.clientX - r.left) / r.width * CALC_W,
y: (e.clientY - r.top) / r.height * CALC_H
};
}
function onPointerEnter() { pointerActive = true; }
function onPointerMove(e: PointerEvent) {
pointerActive = true;
lens = toGrid(e);
if (reduce) draw(lastElapsed);
}
function onPointerLeave() { pointerActive = false; }
function onKeyDown(e: KeyboardEvent) {
let moved = true;
if (e.key === "ArrowLeft") lens.x = Math.max(0, lens.x - 6);
else if (e.key === "ArrowRight") lens.x = Math.min(CALC_W, lens.x + 6);
else if (e.key === "ArrowUp") lens.y = Math.max(0, lens.y - 6);
else if (e.key === "ArrowDown") lens.y = Math.min(CALC_H, lens.y + 6);
else moved = false;
if (moved) {
e.preventDefault();
pointerActive = true;
if (reduce) draw(lastElapsed);
}
}
wrap.addEventListener("pointerenter", onPointerEnter);
wrap.addEventListener("pointermove", onPointerMove);
wrap.addEventListener("pointerleave", onPointerLeave);
wrap.addEventListener("keydown", onKeyDown);
wrap.tabIndex = 0;
function virtualLens(t: number) {
return {
x: CALC_W * (0.5 + 0.32 * Math.sin(t * 0.0011)),
y: CALC_H * (0.5 + 0.3 * Math.cos(t * 0.0017))
};
}
let lastTs: number | null = null;
let elapsed = 0;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = ts - lastTs;
lastTs = ts;
if (playing) {
elapsed += dt;
if (!pointerActive) lens = virtualLens(elapsed);
draw(elapsed);
}
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
draw(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
wrap.removeEventListener("pointerenter", onPointerEnter);
wrap.removeEventListener("pointermove", onPointerMove);
wrap.removeEventListener("pointerleave", onPointerLeave);
wrap.removeEventListener("keydown", onKeyDown);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.rv} is-demo`} ref={rootRef} tabIndex={0}>
<canvas className={styles.rv__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.rv__caption}>포인터를 따라다니는 원형 렌즈 안에서 이미지가 물속처럼 굴절되어 보인다</p>
</div>
</main>
);
}04 ポインターを追う屈折レンズ — 計測 累積面積 21.081% · 動いたフレーム 23/23GODRICH オリジナル · SCSS と React は zip に同梱 · css 波紋 アニメーション 9パターン — キャンバスで計算するピクセル波
このデモが属する記事
css 波紋 アニメーション 9パターン — キャンバスで計算するピクセル波css 波紋 アニメーションを9パターン集めました。9番のヒーローシーンはすでに完成した波紋だ。その前の5番は反射する水面が揺れる段階で、いちばん最初の1番はポインターの一回のタップが一つの輪を描くところから始まった。 この記事のデモをすべて見る


