Ribbon Ripples
60 ribbons of 120 sample points each undulate along the x axis with phase offsets; a wavelength slider stretches the period between 24 and 120 scene units.
Business context
Ribbon installations of this kind appear in data sculpture, launch key visuals and gallery walls. The users are creative coding and spatial design teams turning audio or sensor signals into motion that can be watched for long periods. Data is a procedurally generated set of control points and phases, with 60 lanes of 120 points each as a common configuration; production pieces may take audio analysis or footfall counts and map amplitude and frequency onto wave height and wavelength live.
The waveform could be computed entirely in a vertex shader, but to change control points per frame while keeping parameters readable, 7,140 segment instances are built once and only z values are changed before attribute recomputation; period and wavelength are converted into scene units first so the slider range matches the visual scale. three.js requires managing geometry and materials, while LineLayer consumes instances directly at the cost of re-uploading positions each frame.
Misreading comes from two places: the wavelength slider changes period in scene units, not meters or seconds, so camera distance must be matched before comparing screenshots; and source and target each hold a position attribute, so the smooth-looking waveform is a discrete polyline that shows corners when viewed closely. Changing color or width does not alter the waveform, so motion on screen does not prove a parameter took effect.
How it works
Each ribbon spans x from -240 to 240 with 120 sample points spaced 4.03 apart; 60 lanes stack in y at (i-29.5)×7 over a span of 413. Height is two sine components: the primary wave sin(x·k + t + 0.22i)·16 carries the overall undulation, and a second harmonic at 2.3× frequency, reversed phase and 4.5 amplitude breaks the regularity. k comes from the slider, with period = λ×40, so λ from 0.6 to 3.0 corresponds to 24–120 scene units, or 4–20 full waves along the 480-unit length.
Segment objects are created once at initialization, and the s and t of the 7,140 instances reference adjacent control point arrays directly; each frame only z of the control points changes, and updateTriggers recomputes getSourcePosition and getTargetPosition. The geometry is modified in place, but attributes must be re-uploaded: 7,140×2×3 floats, about 171 KB per frame, or 10 MB/s at 60 fps. Changing only color, for instance to monochrome, does not touch the waveform but positions still recompute because the motion continues; the way to truly avoid uploads is static geometry with displacement in a vertex shader, at the cost of a custom layer. 60×120 runs at full frame rate on a discrete GPU, while 300 points per line means 18,000 segments and 2.5× the uploads, where frame time is consumed by buffer writes before the twenty thousand sin calls in JavaScript.
| Setting | Effect |
|---|---|
| 60 × 120 points | 119 segments per ribbon, 7,140 LineLayer instances in total |
| period = λ × 40 | Slider 0.6–3.0 maps to a 24–120 scene-unit wave period |
| getWidth: 1.6 + pixels | Line width fixed at 1.6 px, not thickening with distance |
| three updateTriggers accessors | Recompute source, target and color attributes when the frame number changes |
Note. A segment instance holds separate position attributes for source and target, so moving control points every frame means two uploads per frame; segment count is the bandwidth ceiling.
Source
<!--
示例:丝带波纹
要点:60 条波形线共 7140 个线段实例,逐帧改写控制点后由 LineLayer 重算源点与靶点属性
依赖:deck.gl 9.0 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>丝带波纹</title>
<style>
html,body{height:100%;margin:0;background:#0d1117}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#e6e8ec}
.bar{position:relative;z-index:5;flex:0 0 auto;padding:8px 12px;background:#161b22;border-bottom:1px solid #252c36;
display:flex;gap:14px;align-items:center;font-size:12px;color:#8b93a1;flex-wrap:wrap}
.bar b{color:#58a6ff;font-weight:600}
.bar label{display:flex;align-items:center;gap:5px;cursor:pointer}
.bar input[type=checkbox]{accent-color:#4f9ef8}
.bar input[type=range]{width:150px;accent-color:#4f9ef8}
.bar button{font:inherit;font-size:12px;padding:3px 10px;border-radius:4px;cursor:pointer;
border:1px solid #2f3947;background:#1d242e;color:#c9d3e0}
.bar button:hover{background:#26303d}
.bar button.on{background:#2c4a7a;border-color:#4f8ad6;color:#dfe9ff}
.bar .sep{width:1px;height:18px;background:#2a313c;margin:0 4px}
#cv{flex:1 1 auto;min-height:0;width:100%;display:block}
</style>
</head>
<body>
<div class="bar">
<span>60 条 × 120 点 · 7,140 段</span>
<span class="sep"></span>
<button id="mode" class="on">按高度</button>
<label>波长 <b id="wl">1.6</b> <input id="lam" type="range" min="0.6" max="3" step="0.1" value="1.6"></label>
</div>
<canvas id="cv"></canvas>
<script src="https://cdn.jsdelivr.net/npm/deck.gl@9.0.35/dist.min.js"></script>
<script>
(function () {
'use strict';
if (typeof deck === 'undefined') {
document.body.innerHTML =
'<div style="padding:24px;color:#ff8c74">deck.gl 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var LINES = 60, PTS = 120, SPAN = 480, LANE = 7, TAU = Math.PI * 2, STEP = 1 / 60;
var i, j;
var paths = [];
var segs = [];
for (i = 0; i < LINES; i++) {
var lane = (i - 29.5) * LANE;
var pts = [];
for (j = 0; j < PTS; j++) {
pts.push([-SPAN / 2 + j * (SPAN / (PTS - 1)), lane, 0]);
}
paths.push(pts);
for (j = 0; j < PTS - 1; j++) {
segs.push({ s: pts[j], t: pts[j + 1] });
}
}
var STOPS = [
[56, 118, 226], [64, 206, 226], [86, 214, 168],
[246, 186, 78], [240, 104, 72]
];
var LUT = [];
for (i = 0; i < 32; i++) {
var u = i / 31 * (STOPS.length - 1);
var k = Math.min(STOPS.length - 2, Math.floor(u));
var f = u - k;
LUT.push([
Math.round(STOPS[k][0] + (STOPS[k + 1][0] - STOPS[k][0]) * f),
Math.round(STOPS[k][1] + (STOPS[k + 1][1] - STOPS[k][1]) * f),
Math.round(STOPS[k][2] + (STOPS[k + 1][2] - STOPS[k][2]) * f),
232
]);
}
var MONO = [96, 168, 244, 225];
var mode = 'height';
var lambda = 1.6;
var t = 0, frameNo = 0;
function update() {
var kx = TAU / (lambda * 40);
for (var a = 0; a < LINES; a++) {
var pts = paths[a];
for (var b = 0; b < PTS; b++) {
var p = pts[b];
var x = p[0];
p[2] = Math.sin(x * kx + t + a * 0.22) * 16 +
Math.sin(x * kx * 2.3 - t * 1.4 + a * 0.5) * 4.5;
}
}
}
function getColor(d) {
if (mode === 'mono') return MONO;
var h = (d.s[2] + 21) / 42;
h = h < 0 ? 0 : (h > 1 ? 1 : h);
return LUT[(h * 31) | 0];
}
update();
function buildLayers() {
return [
new deck.LineLayer({
id: 'ribbon-lines',
data: segs,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getSourcePosition: function (d) { return d.s; },
getTargetPosition: function (d) { return d.t; },
getColor: getColor,
getWidth: 1.6,
widthUnits: 'pixels',
widthMinPixels: 1,
opacity: 0.9,
pickable: false,
updateTriggers: {
getSourcePosition: frameNo,
getTargetPosition: frameNo,
getColor: frameNo
}
})
];
}
var deckgl = new deck.Deck({
canvas: 'cv',
width: '100%',
height: '100%',
views: new deck.OrbitView({ target: [0, 0, 0], fovy: 30 }),
initialViewState: { target: [0, 0, 0], rotationX: 31, rotationOrbit: -38, zoom: 0.5 },
controller: true,
layers: buildLayers()
});
var modeEl = document.getElementById('mode');
modeEl.addEventListener('click', function () {
mode = mode === 'height' ? 'mono' : 'height';
modeEl.className = mode === 'height' ? 'on' : '';
modeEl.textContent = mode === 'height' ? '按高度' : '单色';
frameNo++;
deckgl.setProps({ layers: buildLayers() });
});
var lamEl = document.getElementById('lam');
lamEl.addEventListener('input', function () {
lambda = +lamEl.value;
document.getElementById('wl').textContent = lambda.toFixed(1);
update();
frameNo++;
deckgl.setProps({ layers: buildLayers() });
});
function loop() {
t += STEP;
update();
frameNo++;
deckgl.setProps({ layers: buildLayers() });
requestAnimationFrame(loop);
}
window.__deck = deckgl;
requestAnimationFrame(loop);
})();
</script>
</body>
</html>