05Metaball Color Blend
Two differently colored blobs blend their colors right where they touch as they drift closer.
Best forPortfolio backgrounds, brand intros
PLASMA_BLOB_RADIUS_PXnearestBlobIndexupdateBlobs

<main class="stage">
<div class="pv is-demo" id="pvRoot">
<canvas class="pv__canvas" id="pvCanvas" width="480" height="300"></canvas>
<p class="pv__caption">A background where blobs blend color right where they come within radius of touching</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-yellow);
.pv {
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;
touch-action: none;
}
.pv__canvas {
display: block;
width: 100%;
height: 100%;
}
.pv__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;
}
.pv__step {
position: absolute;
right: $sp-3;
top: $sp-3;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
font-weight: 700;
font-size: 11px;
font-variant-numeric: tabular-nums;
pointer-events: none;
}
@media (max-width: 340px) {
.pv__caption, .pv__step { font-size: 10px; padding: $sp-1 $sp-2; }
}
@media (prefers-reduced-motion: reduce) {
.pv { 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);
}
.pv {
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);
touch-action: none;
}
.pv__canvas {
display: block;
width: 100%;
height: 100%;
}
.pv__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;
}
.pv__step {
position: absolute;
right: 12px;
top: 12px;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
font-weight: 700;
font-size: 11px;
font-variant-numeric: tabular-nums;
pointer-events: none;
}
@media (max-width: 340px) {
.pv__caption, .pv__step {
font-size: 10px;
padding: 4px 8px;
}
}
@media (prefers-reduced-motion: reduce) {
.pv {
cursor: default;
}
}(function () {
// 기법: 덩어리마다 메타볼 필드(반지름^2 / 거리^2 형태의 감쇠)를 계산해 제곱으로 날카롭게 만든 뒤
// 필드 값으로 색을 가중 평균한다 — 두 덩어리가 가까워질수록 경계에서 색이 섞인다.
// 실제 입력: 덩어리를 손가락·마우스로 드래그해 옮길 수 있다.
var root = document.getElementById('pvRoot');
var canvas = document.getElementById('pvCanvas');
var ctx = canvas.getContext('2d');
var wrap = canvas.parentElement;
var CALC_W = 160, CALC_H = 100;
var LOOP_MS = 2000;
var PLASMA_BLOB_RADIUS_PX = 60;
var DRIFT_AMP = 14;
var DRIFT_FREQ = Math.PI * 2 / LOOP_MS;
function seedRand(seed) {
var s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
var rand = seedRand(417051);
function hslToRgb(h, s, l) {
h = ((h % 360) + 360) % 360;
var c = (1 - Math.abs(2 * l - 1)) * s;
var x = c * (1 - Math.abs((h / 60) % 2 - 1));
var m = l - c / 2;
var r, g, b;
if (h < 60) { r = c; g = x; b = 0; }
else if (h < 120) { r = x; g = c; b = 0; }
else if (h < 180) { r = 0; g = c; b = x; }
else if (h < 240) { r = 0; g = x; b = c; }
else if (h < 300) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
return [(r + m) * 255, (g + m) * 255, (b + m) * 255];
}
var off = document.createElement('canvas');
off.width = CALC_W; off.height = CALC_H;
var octx = off.getContext('2d');
var offImg = octx.createImageData(CALC_W, CALC_H);
var offBuf = offImg.data;
var blobHues = [205, 325, 55];
var blobs = blobHues.map(function (h, i) {
return {
bx: CALC_W * (0.28 + i * 0.24), by: CALC_H * (0.35 + (i % 2) * 0.3),
x: 0, y: 0, phase: rand() * Math.PI * 2, rgb: hslToRgb(h, 0.72, 0.56)
};
});
var dragIndex = -1;
function updateBlobs(t) {
blobs.forEach(function (b, i) {
if (i === dragIndex) return;
b.x = b.bx + Math.sin(t * DRIFT_FREQ + b.phase) * DRIFT_AMP;
b.y = b.by + Math.cos(t * DRIFT_FREQ * 0.75 + b.phase * 1.2) * DRIFT_AMP;
});
}
function renderField(t) {
updateBlobs(t);
var idx = 0;
for (var y = 0; y < CALC_H; y += 1) {
for (var x = 0; x < CALC_W; x += 1) {
var sw = 0, rr = 0, gg = 0, bb = 0;
for (var k = 0; k < blobs.length; k += 1) {
var bo = blobs[k];
var dx = x - bo.x, dy = y - bo.y;
var d2 = dx * dx + dy * dy;
var w = (PLASMA_BLOB_RADIUS_PX * PLASMA_BLOB_RADIUS_PX) / (d2 + PLASMA_BLOB_RADIUS_PX * 2);
w = w * w;
sw += w; rr += bo.rgb[0] * w; gg += bo.rgb[1] * w; bb += bo.rgb[2] * w;
}
offBuf[idx] = rr / sw; offBuf[idx + 1] = gg / sw; offBuf[idx + 2] = bb / sw; offBuf[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(elapsed) {
lastElapsed = elapsed;
renderField(elapsed);
octx.putImageData(offImg, 0, 0);
ctx.clearRect(0, 0, cw, ch);
ctx.drawImage(off, 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'); }
function calcPoint(e) {
var r = wrap.getBoundingClientRect();
var t0 = (e.touches && e.touches[0]) ? e.touches[0] : e;
var relX = (t0.clientX - r.left) / r.width * cw;
var relY = (t0.clientY - r.top) / r.height * ch;
return { x: relX * (CALC_W / cw), y: relY * (CALC_H / ch) };
}
function nearestBlobIndex(p) {
var bi = -1, bd = Infinity;
blobs.forEach(function (b, i) {
var dx = b.x - p.x, dy = b.y - p.y, d = dx * dx + dy * dy;
if (d < bd) { bd = d; bi = i; }
});
return Math.sqrt(bd) < PLASMA_BLOB_RADIUS_PX * 1.4 ? bi : -1;
}
wrap.addEventListener('pointerdown', function (e) {
var p = calcPoint(e);
dragIndex = nearestBlobIndex(p);
if (dragIndex >= 0) { blobs[dragIndex].x = p.x; blobs[dragIndex].y = p.y; draw(lastElapsed); }
});
wrap.addEventListener('pointermove', function (e) {
if (dragIndex < 0) return;
var p = calcPoint(e);
blobs[dragIndex].x = p.x; blobs[dragIndex].y = p.y;
draw(lastElapsed);
});
window.addEventListener('pointerup', function () {
if (dragIndex >= 0) {
blobs[dragIndex].bx = blobs[dragIndex].x;
blobs[dragIndex].by = blobs[dragIndex].y;
}
dragIndex = -1;
});
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 "./PlasmaMetaballColorBlend.module.scss";
export default function PlasmaMetaballColorBlend() {
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 LOOP_MS = 2000;
const PLASMA_BLOB_RADIUS_PX = 60;
const DRIFT_AMP = 14;
function seedRand(seed: number) {
let s = seed >>> 0;
return function () { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
}
const rand = seedRand(417051);
function hslToRgb(h: number, s: number, l: number): [number, number, number] {
h = ((h % 360) + 360) % 360;
const c = (1 - Math.abs(2 * l - 1)) * s;
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
const m = l - c / 2;
let r = 0, g = 0, b = 0;
if (h < 60) { r = c; g = x; b = 0; }
else if (h < 120) { r = x; g = c; b = 0; }
else if (h < 180) { r = 0; g = c; b = x; }
else if (h < 240) { r = 0; g = x; b = c; }
else if (h < 300) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
return [(r + m) * 255, (g + m) * 255, (b + m) * 255];
}
type Blob = { hue: number; rgb: [number, number, number]; bx: number; by: number; x: number; y: number; phase: number };
const blobHues = [205, 325, 55];
const blobs: Blob[] = blobHues.map((h, i) => {
const bx = (CALC_W * (0.28 + i * 0.24));
const by = CALC_H * 0.5 + (i % 2 === 0 ? -1 : 1) * 8;
return { hue: h, rgb: hslToRgb(h, 0.68, 0.55), bx, by, x: bx, y: by, phase: rand() * Math.PI * 2 };
});
let dragIndex = -1;
function updateBlobs(t: number) {
const tt = t / LOOP_MS;
blobs.forEach((b, i) => {
if (i === dragIndex) return;
b.x = b.bx + Math.sin(tt * Math.PI * 2 + b.phase) * DRIFT_AMP;
b.y = b.by + Math.cos(tt * Math.PI * 2 * 1.3 + b.phase) * DRIFT_AMP * 0.7;
});
}
const off = document.createElement("canvas");
off.width = CALC_W; off.height = CALC_H;
const octx = off.getContext("2d") as CanvasRenderingContext2D;
const offImg = octx.createImageData(CALC_W, CALC_H);
const offBuf = offImg.data;
function renderField(t: number) {
updateBlobs(t);
let idx = 0;
for (let y = 0; y < CALC_H; y += 1) {
for (let x = 0; x < CALC_W; x += 1) {
let sw = 0, rr = 0, gg = 0, bb = 0;
for (let k = 0; k < blobs.length; k += 1) {
const bo = blobs[k];
const dx = x - bo.x, dy = y - bo.y;
const d2 = dx * dx + dy * dy;
let w = (PLASMA_BLOB_RADIUS_PX * PLASMA_BLOB_RADIUS_PX) / (d2 + PLASMA_BLOB_RADIUS_PX * 2);
w = w * w;
sw += w; rr += bo.rgb[0] * w; gg += bo.rgb[1] * w; bb += bo.rgb[2] * w;
}
offBuf[idx] = rr / sw; offBuf[idx + 1] = gg / sw; offBuf[idx + 2] = bb / sw; offBuf[idx + 3] = 255;
idx += 4;
}
}
}
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;
renderField(elapsed);
octx.putImageData(offImg, 0, 0);
ctx!.imageSmoothingEnabled = false;
ctx!.clearRect(0, 0, cw, ch);
ctx!.drawImage(off, 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"); }
function calcPoint(e: PointerEvent) {
const r = wrap.getBoundingClientRect();
const relX = ((e.clientX - r.left) / r.width) * cw;
const relY = ((e.clientY - r.top) / r.height) * ch;
return { x: relX * (CALC_W / cw), y: relY * (CALC_H / ch) };
}
function nearestBlobIndex(p: { x: number; y: number }) {
let bi = -1, bd = Infinity;
blobs.forEach((b, i) => {
const d = Math.hypot(b.x - p.x, b.y - p.y);
if (d < bd) { bd = d; bi = i; }
});
return bd < PLASMA_BLOB_RADIUS_PX * 1.4 ? bi : -1;
}
function onPointerDown(e: PointerEvent) {
const p = calcPoint(e);
dragIndex = nearestBlobIndex(p);
if (dragIndex >= 0) { blobs[dragIndex].x = p.x; blobs[dragIndex].y = p.y; draw(lastElapsed); }
}
function onPointerMove(e: PointerEvent) {
if (dragIndex < 0) return;
const p = calcPoint(e);
blobs[dragIndex].x = p.x; blobs[dragIndex].y = p.y;
draw(lastElapsed);
}
function onPointerUp() {
if (dragIndex < 0) return;
blobs[dragIndex].bx = blobs[dragIndex].x; blobs[dragIndex].by = blobs[dragIndex].y;
dragIndex = -1;
}
wrap.addEventListener("pointerdown", onPointerDown);
wrap.addEventListener("pointermove", onPointerMove);
window.addEventListener("pointerup", onPointerUp);
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);
wrap.removeEventListener("pointerdown", onPointerDown);
wrap.removeEventListener("pointermove", onPointerMove);
window.removeEventListener("pointerup", onPointerUp);
};
}, []);
return (
<main className={styles.stage}>
<div className={`${styles.pv} is-demo`} ref={rootRef}>
<canvas className={styles.pv__canvas} ref={canvasRef} width={480} height={300} />
<p className={styles.pv__caption}>반지름 값만큼 가까워진 덩어리가 맞닿은 자리부터 색이 섞이는 배경</p>
</div>
</main>
);
}05 Metaball Color Blend — Measured area 60.035% · moving frames 22/23Original by GODRICH · SCSS and React ship in the zip · Rainbow Gradient Background CSS: 9 Patterns
The post this demo lives in
Rainbow Gradient Background CSS: 9 PatternsA rainbow gradient background css, explained: Light, you start out with no color. Item 1 layers three sine waves onto yo See all demos in this post
If you like this effect - similar demos
Creative UI06 Metaball merge — smooth union of blobs9 Ray Marching Scenes in Canvas (No WebGL)
Creative UI05 Sheep fleece — several seeds collide and blend9 Reaction Diffusion Simulations, No Library
Creative UI01 One field layer — counting threshold-crossing pixels as a first metaball9 Metaball Effect Scenes, Marching Squares Canvas