Ripple Text
Sine-wave text distortion with animated color gradient — characters undulate like a flag in wind
Sine-wave text distortion with animated color gradient — characters undulate like a flag in wind
import { Scene, Entity } from "@vectojs/core";
class RippleText extends Entity {
text = "RIPPLE";
freq = 0.06;
amplitude = 14;
speed = 2.5;
time = 0;
fontSize = 90;
colorA = "#d97757";
colorB = "#38bdf8";
isPointInside() {
return false;
}
hasPendingAnimations() {
return true;
}
update(dt) {
this.time += dt;
}
render(r) {
const ctx = r.getContext();
const chars = [...this.text];
if (chars.length === 0) return;
ctx.save();
const fs = Math.min(this.fontSize, this.scene.width * 0.12);
ctx.font = `900 ${fs}px "Inter", system-ui, sans-serif`;
ctx.textBaseline = "middle";
const widths = chars.map((ch) => ctx.measureText(ch).width);
const totalW = widths.reduce((s, w) => s + w, 0) + (chars.length - 1) * 2;
const startX = (this.scene.width - totalW) / 2;
let cx = startX;
const cy = this.scene.height / 2;
for (let i = 0; i < chars.length; i++) {
const w = widths[i];
const xPos = cx + w / 2;
// Gradient position must be relative to the text's OWN span —
// distance walked from the word's first character (xPos - startX,
// where startX is fixed at the word's left edge), not xPos itself.
// xPos is an absolute canvas coordinate starting around half the
// canvas width, so dividing it directly by totalW (just the text's
// width) overshot past 1.0 for every character and rendered the
// whole word in colorB only.
const t = totalW > 0 ? (xPos - startX) / totalW : 0;
const yOff =
Math.sin(xPos * this.freq + this.time * this.speed * 0.001) *
this.amplitude *
0.5 +
Math.sin(xPos * this.freq * 1.7 + this.time * this.speed * 0.0013) *
this.amplitude *
0.3 +
Math.sin(xPos * this.freq * 0.4 + this.time * this.speed * 0.0007) *
this.amplitude *
0.2;
ctx.save();
ctx.translate(xPos, cy + yOff);
ctx.fillStyle = lerpColor(this.colorA, this.colorB, t);
ctx.textAlign = "center";
ctx.fillText(chars[i], 0, 0);
ctx.restore();
cx += w + 2;
}
ctx.restore();
}
}
function lerpColor(a, b, t) {
const [ar, ag, ab] = hexToRgb(a);
const [br, bg, bb] = hexToRgb(b);
return `rgb(${Math.round(ar + (br - ar) * t)},${Math.round(ag + (bg - ag) * t)},${Math.round(ab + (bb - ab) * t)})`;
}
function hexToRgb(hex) {
const n = parseInt(hex.slice(1), 16);
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
}
const app = document.getElementById("app");
const canvas = document.getElementById("canvas");
const hud = document.getElementById("hud");
const textInput = document.getElementById("input-text");
const freqInput = document.getElementById("input-freq");
const ampInput = document.getElementById("input-amp");
const speedInput = document.getElementById("input-speed");
const colorAInput = document.getElementById("input-color-a");
const colorBInput = document.getElementById("input-color-b");
const scene = new Scene(canvas, {
renderMode: "always",
maxFPS: 60,
disableWindowResize: true,
maxDPR: 2,
});
const ripple = new RippleText();
scene.add(ripple);
scene.start();
textInput.addEventListener("input", () => {
ripple.text = textInput.value.toUpperCase() || "RIPPLE";
});
freqInput.addEventListener("input", () => {
ripple.freq = Number(freqInput.value) / 100;
document.getElementById("value-freq").textContent = ripple.freq.toFixed(2);
});
ampInput.addEventListener("input", () => {
ripple.amplitude = Number(ampInput.value);
document.getElementById("value-amp").textContent = `${ripple.amplitude}px`;
});
speedInput.addEventListener("input", () => {
ripple.speed = Number(speedInput.value) / 10;
document.getElementById("value-speed").textContent = ripple.speed.toFixed(1);
});
colorAInput.addEventListener("input", () => {
ripple.colorA = colorAInput.value;
});
colorBInput.addEventListener("input", () => {
ripple.colorB = colorBInput.value;
});
const observer = new ResizeObserver(() => {
const w = app.clientWidth;
const h = app.clientHeight;
if (w > 0 && h > 0) scene.resize(w, h);
});
observer.observe(app);
function updateHud() {
hud.textContent =
`"${ripple.text}" · ${ripple.text.length} chars\n` +
`wave ${ripple.freq.toFixed(2)} · amp ${ripple.amplitude}px · speed ${ripple.speed.toFixed(1)}`;
setTimeout(updateHud, 250);
}
updateHud();
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="/no-ff-webgpu.js"></script>
<title>Ripple Text — Motif</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;900&family=JetBrains+Mono:wght@500&display=swap"
rel="stylesheet"
/>
<style>
* {
margin: 0;
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
background: #f7f4ee;
}
#app {
position: relative;
width: 100%;
height: 100%;
}
canvas {
display: block;
width: 100%;
height: 100%;
}
#panel {
position: absolute;
top: 14px;
left: 14px;
right: 14px;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
font:
500 12px "Inter",
sans-serif;
z-index: 10;
background: rgba(255, 255, 255, 0.86);
border: 1px solid rgba(42, 39, 35, 0.16);
border-radius: 10px;
padding: 10px 12px;
color: #2a2723;
}
#panel .field {
display: flex;
align-items: center;
gap: 6px;
}
#panel label {
color: #6b6459;
font-size: 11px;
white-space: nowrap;
}
#panel input[type="text"] {
width: 100px;
border: 1px solid rgba(42, 39, 35, 0.2);
border-radius: 6px;
padding: 5px 7px;
font: inherit;
background: #fff;
color: #2a2723;
}
#panel input[type="text"]:focus {
outline: 2px solid #d97757;
outline-offset: 1px;
}
#panel input[type="range"] {
width: 72px;
accent-color: #d97757;
}
#panel input[type="color"] {
width: 26px;
height: 26px;
padding: 0;
border: 1px solid rgba(42, 39, 35, 0.2);
border-radius: 6px;
cursor: pointer;
background: none;
}
#panel .value {
font:
500 11px "JetBrains Mono",
monospace;
color: #6b6459;
min-width: 28px;
}
#panel .divider {
width: 1px;
height: 20px;
background: rgba(42, 39, 35, 0.14);
}
#hud {
position: absolute;
top: 68px;
left: 14px;
font:
500 12px "JetBrains Mono",
monospace;
color: #2a2723;
background: rgba(255, 255, 255, 0.7);
border: 1px solid rgba(42, 39, 35, 0.16);
border-radius: 7px;
padding: 8px 12px;
line-height: 1.6;
z-index: 10;
white-space: pre;
}
</style>
<script type="importmap">
{
"imports": {
"@vectojs/core": "https://esm.sh/@vectojs/core@1.11.3"
}
}
</script>
</head>
<body>
<div id="app">
<canvas id="canvas"></canvas>
<div id="panel">
<div class="field">
<label for="input-text">Text</label>
<input
id="input-text"
type="text"
value="RIPPLE"
maxlength="16"
spellcheck="false"
autocomplete="off"
/>
</div>
<div class="divider"></div>
<div class="field">
<label for="input-freq">Wave</label>
<input
id="input-freq"
type="range"
min="1"
max="15"
step="1"
value="6"
/>
<span class="value" id="value-freq">0.06</span>
</div>
<div class="field">
<label for="input-amp">Amp</label>
<input
id="input-amp"
type="range"
min="1"
max="35"
step="1"
value="14"
/>
<span class="value" id="value-amp">14px</span>
</div>
<div class="field">
<label for="input-speed">Speed</label>
<input
id="input-speed"
type="range"
min="5"
max="50"
step="1"
value="25"
/>
<span class="value" id="value-speed">2.5</span>
</div>
<div class="divider"></div>
<div class="field">
<label for="input-color-a">Color</label>
<input id="input-color-a" type="color" value="#d97757" />
<input id="input-color-b" type="color" value="#38bdf8" />
</div>
</div>
<div id="hud">RIPPLE</div>
</div>
<script type="module" src="./demo.js"></script>
</body>
</html>