02ひとりでに揺れる背景波紋
操作がなくても重なり合う正弦波によって画像の表面が絶えずゆらめく。
使いどころヒーロー背景、ローディング画面
ambientHeightRIPPLE_AMBIENT_WAVE_COUNTREFRACT_K

<main class="stage">
<div class="rv is-demo" id="rvRoot">
<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-yellow);
.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;
}
.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(255, 247, 230, .85);
color: $subject-ink;
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: 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);
}
.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);
}
.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(255, 247, 230, 0.85);
color: #17141a;
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: default;
}
}(function () {
// 기법: 포인터 없이도 사인파 RIPPLE_AMBIENT_WAVE_COUNT 겹을 분석식으로 합쳐 높이 필드를 매 프레임 계산한다.
// 높이 필드의 x·y 기울기를 굴절 변위로 써서 절차적 "사진" 픽셀을 매 프레임 다시 그린다(속성 트윈이 아니다).
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, '#ffe6a8');
sky.addColorStop(1, '#fff8ea');
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
var sunX = w * 0.24, sunY = h * 0.28, sunR = h * 0.17;
var glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, 'rgba(255,255,255,.85)');
glow.addColorStop(1, 'rgba(255,255,255,0)');
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = '#fff3c9';
pctx.fill();
var hillColors = ['#f3b25a', '#c9713a', '#6a3420'];
for (var i = 0; i < hillColors.length; i += 1) {
var baseY = h * (0.5 + i * 0.14);
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.05 + i * 1.7 + seed * 0.0002) * h * 0.04 + Math.sin(x * 0.14 + i) * h * 0.02;
pctx.lineTo(x, baseY + n);
}
pctx.lineTo(w, h);
pctx.closePath();
pctx.fillStyle = hillColors[i];
pctx.fill();
}
for (var k = 0; k < 55; k += 1) {
var px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.2 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = 'rgba(255,255,255,' + (0.1 + rand() * 0.2) + ')';
pctx.fill();
}
}
var CALC_W = 160, CALC_H = 100;
var RIPPLE_AMBIENT_WAVE_COUNT = 2;
var REFRACT_K = 1.15;
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
drawPhotoBase(octx, CALC_W, CALC_H, 419021);
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 ambientHeight(x, y, t) {
var h = 0;
for (var i = 0; i < RIPPLE_AMBIENT_WAVE_COUNT; i += 1) {
var ang = (i / RIPPLE_AMBIENT_WAVE_COUNT) * Math.PI + 0.5;
var freq = 0.05 + i * 0.021;
var speed = 0.0017 + i * 0.0009;
var amp = 6.5 / (i + 1);
var dir = (i % 2 === 0) ? 1 : -1;
h += amp * Math.sin(x * Math.cos(ang) * freq + y * Math.sin(ang) * freq + t * speed * dir);
}
return h;
}
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]];
}
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 h0 = ambientHeight(x, y, t);
var hx = ambientHeight(x + 1, y, t) - ambientHeight(x - 1, y, t);
var hy = ambientHeight(x, y + 1, t) - ambientHeight(x, y - 1, t);
var dx = hx * REFRACT_K, dy = hy * REFRACT_K;
var rgb = sampleBase(x + dx, y + dy);
var shine = Math.max(-26, Math.min(46, h0 * 4));
outBuf[idx] = Math.max(0, Math.min(255, rgb[0] + shine));
outBuf[idx + 1] = Math.max(0, Math.min(255, rgb[1] + shine));
outBuf[idx + 2] = Math.max(0, Math.min(255, rgb[2] + shine));
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;
var elapsed = reduce ? 900 : 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; }
draw(elapsed);
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
draw(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./RippleCanvasAutoLoopScene.module.scss";
export default function RippleCanvasAutoLoopScene() {
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, "#ffe6a8");
sky.addColorStop(1, "#fff8ea");
pctx.fillStyle = sky;
pctx.fillRect(0, 0, w, h);
const sunX = w * 0.24, sunY = h * 0.28, sunR = h * 0.17;
const glow = pctx.createRadialGradient(sunX, sunY, 0, sunX, sunY, sunR * 2.4);
glow.addColorStop(0, "rgba(255,255,255,.85)");
glow.addColorStop(1, "rgba(255,255,255,0)");
pctx.fillStyle = glow;
pctx.fillRect(0, 0, w, h);
pctx.beginPath();
pctx.arc(sunX, sunY, sunR, 0, Math.PI * 2);
pctx.fillStyle = "#fff3c9";
pctx.fill();
const hillColors = ["#f3b25a", "#c9713a", "#6a3420"];
for (let i = 0; i < hillColors.length; i += 1) {
const baseY = h * (0.5 + i * 0.14);
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.05 + i * 1.7 + seed * 0.0002) * h * 0.04 + Math.sin(x * 0.14 + 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 < 55; k += 1) {
const px = rand() * w, py = rand() * h * 0.5, r = rand() * 1.2 + 0.3;
pctx.beginPath();
pctx.arc(px, py, r, 0, Math.PI * 2);
pctx.fillStyle = "rgba(255,255,255," + (0.1 + rand() * 0.2) + ")";
pctx.fill();
}
}
const CALC_W = 160, CALC_H = 100;
const RIPPLE_AMBIENT_WAVE_COUNT = 2;
const REFRACT_K = 1.15;
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, 419021);
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 ambientHeight(x: number, y: number, t: number) {
let h = 0;
for (let i = 0; i < RIPPLE_AMBIENT_WAVE_COUNT; i += 1) {
const ang = (i / RIPPLE_AMBIENT_WAVE_COUNT) * Math.PI + 0.5;
const freq = 0.05 + i * 0.021;
const speed = 0.0017 + i * 0.0009;
const amp = 6.5 / (i + 1);
const dir = (i % 2 === 0) ? 1 : -1;
h += amp * Math.sin(x * Math.cos(ang) * freq + y * Math.sin(ang) * freq + t * speed * dir);
}
return h;
}
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]];
}
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 h0 = ambientHeight(x, y, t);
const hx = ambientHeight(x + 1, y, t) - ambientHeight(x - 1, y, t);
const hy = ambientHeight(x, y + 1, t) - ambientHeight(x, y - 1, t);
const dx = hx * REFRACT_K, dy = hy * REFRACT_K;
const rgb = sampleBase(x + dx, y + dy);
const shine = Math.max(-26, Math.min(46, h0 * 4));
outBuf[idx] = Math.max(0, Math.min(255, rgb[0] + shine));
outBuf[idx + 1] = Math.max(0, Math.min(255, rgb[1] + shine));
outBuf[idx + 2] = Math.max(0, Math.min(255, rgb[2] + shine));
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;
let elapsed = reduce ? 900 : 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; }
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.rv} is-demo`} ref={rootRef}>
<canvas className={styles.rv__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.rv__caption}>사용자 조작 없이도 사인파가 겹쳐 이미지 표면이 잔잔하게 계속 일렁인다</p>
</div>
</main>
);
}02 ひとりでに揺れる背景波紋 — 計測 累積面積 66.05% · 動いたフレーム 23/23GODRICH オリジナル · SCSS と React は zip に同梱 · css 波紋 アニメーション 9パターン — キャンバスで計算するピクセル波
このデモが属する記事
css 波紋 アニメーション 9パターン — キャンバスで計算するピクセル波css 波紋 アニメーションを9パターン集めました。9番のヒーローシーンはすでに完成した波紋だ。その前の5番は反射する水面が揺れる段階で、いちばん最初の1番はポインターの一回のタップが一つの輪を描くところから始まった。 この記事のデモをすべて見る


