04Smoke That Fades and Dissolves the Farther It Drifts From Its Source
Opacity drops in step with distance from the source, and past a set distance the particle disappears completely.
Best forCleanup trail after loading completes
SMOKE_DISSOLVE_MSDISSOLVE_DISTANCEDRIFT_SPEED_PX_S

<main class="stage">
<div class="smk is-demo" id="smkRoot">
<canvas class="smk__canvas" id="smkCanvas" width="480" height="300"></canvas>
<p class="smk__caption">Opacity drops in step with distance from the source, and past a set distance the particle disappears completely.</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-paper);
.smk {
box-sizing: border-box;
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;
background: $stage-paper;
border: 1px solid rgba(23, 20, 26, .08);
}
.smk__canvas {
display: block;
width: 100%;
height: 100%;
}
.smk__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, .78);
color: $stage-paper;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.smk__caption { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.smk { 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);
}
.smk {
box-sizing: border-box;
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);
background: #ffffff;
border: 1px solid rgba(23, 20, 26, 0.08);
}
.smk__canvas {
display: block;
width: 100%;
height: 100%;
}
.smk__caption {
position: absolute;
left: 12px;
bottom: 12px;
max-width: 78%;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(23, 20, 26, 0.78);
color: #ffffff;
font-weight: 500;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
}
@media (max-width: 340px) {
.smk__caption {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.smk {
cursor: default;
}
}(function () {
// 기법: 발원지(원점) 좌표를 저장해두고, 매 프레임 입자와 원점 사이의 유클리드 거리를 계산한다.
// 거리를 SMOKE_DISSOLVE_MS(=이동 속도 x 시간으로 환산한 소멸 거리)로 나눈 비율만큼 알파를
// 선형으로 깎는다 — 나이가 아니라 "거리"가 직접 불투명도를 정한다. 거리가 소멸 거리를
// 넘어서면 배열에서 제거한다.
var root = document.getElementById('smkRoot');
var canvas = document.getElementById('smkCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
var CALC_W = 160, CALC_H = 100;
var SMOKE_DISSOLVE_MS = 900;
var DRIFT_SPEED_PX_S = 30;
var DISSOLVE_DISTANCE = (SMOKE_DISSOLVE_MS / 1000) * DRIFT_SPEED_PX_S;
var SPAWN_PER_S = 30;
var SOURCE_X = CALC_W * 0.5, SOURCE_Y = CALC_H * 0.62;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
var NOISE_DIM = 40;
var noiseRand = seedRand(423041);
var noiseTable = new Float32Array(NOISE_DIM * NOISE_DIM);
for (var ni = 0; ni < noiseTable.length; ni += 1) noiseTable[ni] = noiseRand();
function noiseAt(xi, yi) {
var wx = ((xi % NOISE_DIM) + NOISE_DIM) % NOISE_DIM;
var wy = ((yi % NOISE_DIM) + NOISE_DIM) % NOISE_DIM;
return noiseTable[wy * NOISE_DIM + wx];
}
function smoothstep(t) { return t * t * (3 - 2 * t); }
function valueNoise2D(x, y) {
var x0 = Math.floor(x), y0 = Math.floor(y);
var tx = smoothstep(x - x0), ty = smoothstep(y - y0);
var v00 = noiseAt(x0, y0), v10 = noiseAt(x0 + 1, y0);
var v01 = noiseAt(x0, y0 + 1), v11 = noiseAt(x0 + 1, y0 + 1);
var a = v00 + (v10 - v00) * tx;
var b = v01 + (v11 - v01) * tx;
return a + (b - a) * ty;
}
var pRand = seedRand(423042);
var particles = [];
var spawnAcc = 0;
function spawn() {
var ang = -Math.PI / 2 + (pRand() - 0.5) * Math.PI * 0.9;
particles.push({
x: SOURCE_X, y: SOURCE_Y,
vx: Math.cos(ang) * DRIFT_SPEED_PX_S,
vy: Math.sin(ang) * DRIFT_SPEED_PX_S,
wSeed: pRand() * 1000
});
}
function stepSim(dtMs) {
var dtS = dtMs / 1000;
spawnAcc += dtMs * SPAWN_PER_S / 1000;
while (spawnAcc >= 1) { spawn(); spawnAcc -= 1; }
var alive = [];
for (var i = 0; i < particles.length; i += 1) {
var p = particles[i];
p.x += p.vx * dtS;
p.y += p.vy * dtS;
var dist = Math.hypot(p.x - SOURCE_X, p.y - SOURCE_Y);
if (dist < DISSOLVE_DISTANCE) alive.push(p);
}
particles = alive;
}
function drawSmoke(nowMs) {
var sx = cw / CALC_W, sy = ch / CALC_H;
ctx.clearRect(0, 0, cw, ch);
for (var i = 0; i < particles.length; i += 1) {
var p = particles[i];
var dist = Math.hypot(p.x - SOURCE_X, p.y - SOURCE_Y);
var f = Math.max(0, 1 - dist / DISSOLVE_DISTANCE);
var turb = (valueNoise2D(p.wSeed * 0.04, nowMs * 0.0016) - 0.5) * 6;
var r = 6 + (1 - f) * 24;
var alpha = f * 0.6;
if (alpha <= 0.01) continue;
var dx = (p.x + turb) * sx, dy = p.y * sy, dr = r * ((sx + sy) / 2);
var g = ctx.createRadialGradient(dx, dy, 0, dx, dy, dr);
g.addColorStop(0, 'rgba(35,32,40,' + alpha + ')');
g.addColorStop(1, 'rgba(35,32,40,0)');
ctx.fillStyle = g;
ctx.beginPath();
ctx.arc(dx, dy, dr, 0, Math.PI * 2);
ctx.fill();
}
}
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;
drawSmoke(elapsed);
}
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
var elapsed = 0;
if (reduce) root.classList.remove('is-demo');
var warmSteps = 60;
for (var w = 0; w < warmSteps; w += 1) { elapsed += 20; stepSim(20); }
var lastTs = null;
function tick(ts) {
if (lastTs === null) lastTs = ts;
var dt = Math.min(ts - lastTs, 48);
lastTs = ts;
if (!reduce) {
elapsed += dt;
stepSim(dt);
drawSmoke(elapsed);
}
requestAnimationFrame(tick);
}
fit();
window.addEventListener('resize', fit);
drawSmoke(elapsed);
requestAnimationFrame(tick);
})();import { useEffect, useRef } from "react";
import styles from "./SmokeEffectDistanceFadeDissolve.module.scss";
type Particle = { x: number; y: number; ang: number; born: number };
export default function SmokeEffectDistanceFadeDissolve() {
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;
const CALC_W = 160, CALC_H = 100;
const SMOKE_DISSOLVE_MS = 900;
const DRIFT_SPEED_PX_S = 30;
const DISSOLVE_DISTANCE = (SMOKE_DISSOLVE_MS / 1000) * DRIFT_SPEED_PX_S;
const SOURCE_X = CALC_W * 0.5, SOURCE_Y = CALC_H * 0.55;
const SPAWN_PER_S = 30;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
const pRand = seedRand(423041);
let particles: Particle[] = [];
let spawnAcc = 0;
function spawn(bornMs: number) {
particles.push({ x: SOURCE_X, y: SOURCE_Y, ang: pRand() * Math.PI * 2, born: bornMs });
}
function stepSim(dtMs: number, nowMs: number) {
const dtS = dtMs / 1000;
spawnAcc += dtMs * SPAWN_PER_S / 1000;
while (spawnAcc >= 1) { spawn(nowMs); spawnAcc -= 1; }
const alive: Particle[] = [];
for (let i = 0; i < particles.length; i += 1) {
const p = particles[i];
p.x += Math.cos(p.ang) * DRIFT_SPEED_PX_S * dtS;
p.y += Math.sin(p.ang) * DRIFT_SPEED_PX_S * dtS - DRIFT_SPEED_PX_S * 0.2 * dtS;
const dist = Math.hypot(p.x - SOURCE_X, p.y - SOURCE_Y);
if (dist < DISSOLVE_DISTANCE && nowMs - p.born < 4000) alive.push(p);
}
particles = alive;
}
let cw = 480, ch = 300;
function drawSmoke(nowMs: number) {
const sx = cw / CALC_W, sy = ch / CALC_H;
ctx!.clearRect(0, 0, cw, ch);
for (let i = 0; i < particles.length; i += 1) {
const p = particles[i];
const dist = Math.hypot(p.x - SOURCE_X, p.y - SOURCE_Y);
const f = Math.max(0, 1 - dist / DISSOLVE_DISTANCE);
const r = 6 + (1 - f) * 24;
const alpha = f * 0.6;
if (alpha <= 0.01) continue;
const dx = p.x * sx, dy = p.y * sy, dr = r * ((sx + sy) / 2);
const g = ctx!.createRadialGradient(dx, dy, 0, dx, dy, dr);
g.addColorStop(0, "rgba(35,32,40," + alpha + ")");
g.addColorStop(1, "rgba(35,32,40,0)");
ctx!.fillStyle = g;
ctx!.beginPath();
ctx!.arc(dx, dy, dr, 0, Math.PI * 2);
ctx!.fill();
}
void nowMs;
}
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;
drawSmoke(elapsed);
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let elapsed = 0;
if (reduce) root.classList.remove("is-demo");
const warmSteps = reduce ? 140 : 100;
for (let w = 0; w < warmSteps; w += 1) { elapsed += 20; stepSim(20, elapsed); }
let lastTs: number | null = null;
let raf = 0;
function tick(ts: number) {
if (lastTs === null) lastTs = ts;
const dt = Math.min(ts - lastTs, 48);
lastTs = ts;
if (!reduce) {
elapsed += dt;
stepSim(dt, elapsed);
drawSmoke(elapsed);
}
raf = requestAnimationFrame(tick);
}
fit();
window.addEventListener("resize", fit);
drawSmoke(elapsed);
raf = requestAnimationFrame(tick);
return () => {
cancelAnimationFrame(raf);
window.removeEventListener("resize", fit);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.smk} is-demo`} ref={rootRef}>
<canvas className={styles.smk__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.smk__caption}>발원지에서 멀어진 거리만큼 입자의 불투명도가 줄어들다가 일정 거리를 넘으면 완전히 사라진다</p>
</div>
</main>
);
}04 Smoke That Fades and Dissolves the Farther It Drifts From Its Source — Measured area 15.542% · moving frames 23/23Original by GODRICH · SCSS and React ship in the zip · CSS Smoke Effect: 9 Canvas Noise Patterns
The post this demo lives in
CSS Smoke Effect: 9 Canvas Noise PatternsA css smoke effect set: This smoke has no physics engine. Item 06 still swirls away from the pointer, item 07 still puff See all demos in this post
If you like this effect - similar demos
Galleries01 Magnifier lens that follows the pointer9 Image Zoom Viewer Interactions — No Photo Files
Galleries07 Drag slider that decides the rest by release speedCSS Image Slider — 9 Responsive Sliders, No GSAP
Galleries02 Glitch Split From a Channel-Offset Distance ValueCSS Image Filters: 9 Patterns, One Value