Gravitational Orbit System
60,000 particles in 12 groups travel along tilted elliptical orbits around different centers; orbit rings can be toggled and angular speed adjusted.
Business context
Orbit visualizations are used mainly by astrophysicists, mission designers and molecular dynamics researchers to judge whether a many-body configuration is stable and whether orbits show resonance or close encounters. Data typically comes from an N-body integrator stepping at a fixed interval; a few hundred to a few thousand bodies per run is normal. Here each body is expanded into a group of tracer particles, and 60,000 points convey the tube-like thickness of an orbit.
Raw output is the full state at every time step, so it is first moved into the barycentric frame, resampled by orbital period, and long arcs are cut into a cached sequence of fixed angular steps. Integrating in the browser means solving forces and controlling step error at once, and any parameter change destroys reproducibility of the history. Cesium's clock and entity model are built for geographic time axes and three.js requires manual buffer management, whereas frame copying plus attribute recomputation in deck.gl is more direct.
A common misreading is to assume the tilt of an orbit ring reflects a real orbital plane; the normals here are random, so only the shape can be read, not orbital elements. A 9° step is a visible jump on large-radius orbits, especially at 30 frames per second, which is under-sampling rather than physical jitter. The rotation of the whole group is also not the revolution of the group centers; the two motions are constructed separately.
How it works
Each group takes a center on a circle of radius 150, jittered by ±15 in z; the semi-major axis is 55–80 and the semi-minor axis 40–60, and the orbital plane is spanned by an orthonormal basis u, v generated from a random normal n. Particle phases are uniformly distributed over 0–2π, with an additional ±3.5 normal offset that gives the orbit tube its thickness. Forty frames are precomputed at 9° per frame, totaling 40×60000×3 Float32 values, about 28.8 MB; switching frames copies only these 180,000 floats back into a fixed coordinate array, and the data reference never changes.
Real-time integration is possible, but each frame would solve 60,000 force evaluations and carry step-size error, and any parameter change would make the history irreproducible; precomputation pushes the cost into the loading phase so playback is only a memory copy. A 9° step corresponds to roughly 12 units of travel on a radius-80 orbit, which is a visible jump at 30 frames per second, so the slider caps playback at 2–30 fps, equivalent to 18–270 °/s. Smoother motion requires interpolating linearly between adjacent frames using the remaining time, not raising the frame count.
| Setting | Effect |
|---|---|
| STEP = 9° | Angular step per frame; 40 frames cover one revolution |
| framesPerSecond 2–30 | Playback rate, equivalent to 18–270 °/s |
| a, b | Semi-major and semi-minor axes; their means set orbit radius and color |
| copyFrame | Copies a precomputed frame into the reused array without rebuilding the data object |
Note. Fix the angular step and the playback rate together: 9° per frame at 10 fps shows no visible jump, while 30 fps requires interpolation.
Source
<!--
示例:引力系统轨道
要点:6 万粒子分 12 组沿倾斜椭圆轨道运行,40 帧位置预计算,逐帧切换帧索引
依赖: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>粒子 <b>60,000</b> · 12 组</span><span class="sep"></span>
<button id="orb" class="on">轨迹 + 点</button>
<label>角速度 <input id="rate" type="range" min="2" max="30" value="10"><b id="rv">90°/s</b></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;
}
function rng(seed) {
var a = seed >>> 0;
return function () {
a = (a + 0x6D2B79F5) >>> 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
var N = 60000, G = 12, FRAMES = 40;
var STEP = Math.PI / 20; // 每帧 9°,40 帧覆盖一圈
var TAU = Math.PI * 2;
var rand = rng(77123);
var STOPS = [
[64, 154, 255], [64, 206, 226], [86, 214, 168],
[246, 186, 78], [240, 104, 72], [176, 132, 255]
];
var i, j, k, gi;
var groups = [];
for (gi = 0; gi < G; gi++) {
var ang = gi / G * TAU;
var nx = (rand() - 0.5) * 0.6, ny = (rand() - 0.5) * 0.6;
var nz = 0.85 + (rand() - 0.5) * 0.5;
var nl = Math.sqrt(nx * nx + ny * ny + nz * nz);
nx /= nl; ny /= nl; nz /= nl;
var ux = ny, uy = -nx;
var ul = Math.sqrt(ux * ux + uy * uy);
ux /= ul; uy /= ul;
groups.push({
c: [Math.cos(ang) * 150, Math.sin(ang) * 150, (rand() - 0.5) * 30],
u: [ux, uy, 0],
v: [-nz * uy, nz * ux, nx * uy - ny * ux],
n: [nx, ny, nz], a: 55 + rand() * 25, b: 40 + rand() * 20
});
}
var pos = new Array(N);
var gof = new Uint8Array(N);
var A = new Float32Array(N), B = new Float32Array(N);
var PH = new Float32Array(N), HH = new Float32Array(N);
var COL = new Uint8Array(N);
for (i = 0; i < N; i++) {
var g = i % G;
gof[i] = g;
A[i] = groups[g].a + (rand() - 0.5) * 12;
B[i] = groups[g].b + (rand() - 0.5) * 10;
PH[i] = rand() * TAU;
HH[i] = (rand() - 0.5) * 7;
pos[i] = [0, 0, 0];
var rn = (A[i] + B[i]) / 2;
COL[i] = Math.max(0, Math.min(23, Math.round((rn - 50) / 45 * 23)));
}
var COLORS = [];
for (i = 0; i < 24; i++) {
var cc = STOPS[Math.min(STOPS.length - 1, (i / 4) | 0)];
COLORS.push([cc[0], cc[1], cc[2], 225]);
}
var frames = new Float32Array(FRAMES * N * 3);
for (j = 0; j < FRAMES; j++) {
var base = j * N * 3;
for (i = 0; i < N; i++) {
var gr = groups[gof[i]], th = PH[i] + j * STEP;
var cs = Math.cos(th), sn = Math.sin(th);
var kk = base + i * 3;
frames[kk] = gr.c[0] + A[i] * cs * gr.u[0] + B[i] * sn * gr.v[0] + gr.n[0] * HH[i];
frames[kk + 1] = gr.c[1] + A[i] * cs * gr.u[1] + B[i] * sn * gr.v[1] + gr.n[1] * HH[i];
frames[kk + 2] = gr.c[2] + A[i] * cs * gr.u[2] + B[i] * sn * gr.v[2] + gr.n[2] * HH[i];
}
}
function copyFrame(fi) {
var b = fi * N * 3, p, q;
for (var m = 0; m < N; m++) {
p = pos[m]; q = b + m * 3;
p[0] = frames[q]; p[1] = frames[q + 1]; p[2] = frames[q + 2];
}
}
copyFrame(0);
var orbits = [];
for (gi = 0; gi < G; gi++) {
var gr2 = groups[gi], path = [];
for (k = 0; k <= 90; k++) {
var ta = k / 90 * TAU, c2 = Math.cos(ta), s2 = Math.sin(ta);
path.push([
gr2.c[0] + gr2.a * c2 * gr2.u[0] + gr2.b * s2 * gr2.v[0],
gr2.c[1] + gr2.a * c2 * gr2.u[1] + gr2.b * s2 * gr2.v[1],
gr2.c[2] + gr2.a * c2 * gr2.u[2] + gr2.b * s2 * gr2.v[2]
]);
}
var col = STOPS[gi % STOPS.length];
orbits.push({ path: path, color: [col[0], col[1], col[2], 190] });
}
function getPosition(d) { return d; }
function getFillColor(d, info) { return COLORS[COL[info.index]]; }
var frameIdx = 0, showOrbits = true;
function buildLayers() {
var layers = [];
if (showOrbits) {
layers.push(new deck.PathLayer({
id: 'orbit-rings', data: orbits,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getPath: function (d) { return d.path; },
getColor: function (d) { return d.color; },
getWidth: 1, widthUnits: 'pixels', widthMinPixels: 1,
opacity: 0.6, pickable: false
}));
}
layers.push(new deck.ScatterplotLayer({
id: 'orbit-particles', data: pos,
coordinateSystem: deck.COORDINATE_SYSTEM.CARTESIAN,
getPosition: getPosition, getFillColor: getFillColor,
getRadius: 1.5, radiusUnits: 'pixels', radiusMinPixels: 1,
opacity: 0.9, pickable: false,
updateTriggers: { getPosition: frameIdx }
}));
return layers;
}
var deckgl = new deck.Deck({
canvas: 'cv', width: '100%', height: '100%',
views: new deck.OrbitView({ target: [0, 0, 0], fovy: 20 }),
initialViewState: { target: [0, 0, 0], rotationX: 30, rotationOrbit: -28, zoom: 0.2 },
controller: true,
layers: buildLayers()
});
document.getElementById('orb').addEventListener('click', function () {
showOrbits = !showOrbits;
this.className = showOrbits ? 'on' : '';
this.textContent = showOrbits ? '轨迹 + 点' : '只看点';
deckgl.setProps({ layers: buildLayers() });
});
var rate = 10;
document.getElementById('rate').addEventListener('input', function () {
rate = +this.value;
document.getElementById('rv').textContent = Math.round(rate * 9) + '°/s';
});
var acc = 0, last = performance.now();
function loop(now) {
var dt = Math.min((now - last) / 1000, 0.1);
last = now;
acc += dt * rate;
if (acc >= 1) {
var steps = Math.floor(acc);
acc -= steps;
frameIdx = (frameIdx + steps) % FRAMES;
copyFrame(frameIdx);
deckgl.setProps({ layers: buildLayers() });
}
requestAnimationFrame(loop);
}
window.__deck = deckgl;
requestAnimationFrame(loop);
})();
</script>
</body>
</html>