Spaces:
Running
Running
Download js/aurora2d.js from AK51/MOP_tutorial: direct link, hf CLI and curl.
- Browser
- Download file 12.8 kB
-
https://huggingface.co/spaces/AK51/MOP_tutorial/resolve/main/js/aurora2d.js
- Command line
-
hf download hf://spaces/AK51/MOP_tutorial/js/aurora2d.js
-
curl -L -o aurora2d.js https://huggingface.co/spaces/AK51/MOP_tutorial/resolve/main/js/aurora2d.js
12.8 kB
| // Animated polar views of the aurora on each planet (2D canvas). | |
| // View: looking down on the north spin pole, Sun towards the top of the image. | |
| const TAU = Math.PI * 2; | |
| const D = Math.PI / 180; | |
| // periodic pseudo-noise in azimuth, 0..1 | |
| const pn = (a, t, s) => 0.5 + 0.22 * Math.sin(3 * a + 0.7 * t + s) + 0.16 * Math.sin(7 * a - 1.1 * t + 2.1 * s) + 0.12 * Math.sin(13 * a + 1.9 * t + 3.7 * s); | |
| const wrap = a => Math.atan2(Math.sin(a), Math.cos(a)); | |
| const rgba = (c, a) => `rgba(${c[0]},${c[1]},${c[2]},${Math.max(0, Math.min(1, a)).toFixed(3)})`; | |
| function sph(colat, az) { return [Math.sin(colat) * Math.cos(az), Math.sin(colat) * Math.sin(az), Math.cos(colat)]; } | |
| function basis(P) { | |
| const ref = Math.abs(P[2]) < 0.9 ? [0, 0, 1] : [1, 0, 0]; | |
| const cr = (u, v) => [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]]; | |
| const nz = v => { const l = Math.hypot(...v); return v.map(x => x / l); }; | |
| const t1 = nz(cr(P, ref)); const t2 = cr(P, t1); | |
| return [t1, t2]; | |
| } | |
| function ovalPoint(P, t1, t2, rho, b) { | |
| const c = Math.cos(rho), s = Math.sin(rho), cb = Math.cos(b), sb = Math.sin(b); | |
| return [c * P[0] + s * (cb * t1[0] + sb * t2[0]), c * P[1] + s * (cb * t1[1] + sb * t2[1]), c * P[2] + s * (cb * t1[2] + sb * t2[2])]; | |
| } | |
| const STYLES = { | |
| earth: { | |
| disc: [[40, 80, 140], [6, 14, 34]], | |
| draw(g, t) { | |
| const cyc = (t % 11) / 11; | |
| const sub = cyc < 0.12 ? cyc / 0.12 : Math.exp(-(cyc - 0.12) * 6); | |
| g.oval({ | |
| pole: sph(4 * D, -Math.PI / 2), | |
| rho: b => (19.5 + 2.5 * (pn(b, t * 0.5, 1) - 0.5) - 4.5 * sub * Math.exp(-(wrap(b + Math.PI / 2) ** 2) / 0.35)) * D, | |
| I: b => 0.35 + 0.65 * pn(b, t * 1.3, 2) + 1.4 * sub * Math.exp(-(wrap(b + Math.PI / 2) ** 2) / 0.25), | |
| color: [90, 255, 150], glow: [40, 255, 120], ray: 3.5 * D, width: 7 | |
| }); | |
| g.oval({ | |
| pole: sph(4 * D, -Math.PI / 2), | |
| rho: b => (17.5 + 2 * (pn(b, t * 0.4, 5) - 0.5)) * D, | |
| I: b => 0.25 + 0.4 * pn(b, t, 6) + 0.6 * sub * Math.exp(-(wrap(b + Math.PI / 2) ** 2) / 0.3), | |
| color: [255, 70, 110], glow: [255, 40, 90], ray: 2 * D, width: 14, glowOnly: true | |
| }); | |
| } | |
| }, | |
| jupiter: { | |
| disc: [[120, 95, 70], [20, 14, 10]], | |
| draw(g, t) { | |
| const pole = sph(7 * D, -2.4); | |
| g.oval({ | |
| pole, | |
| rho: b => (15 + 2.6 * Math.cos(b - 2.3) + 1.2 * (pn(b, t * 0.3, 1) - 0.5)) * D, | |
| I: b => 0.55 + 0.45 * pn(b, t * 1.1, 3) + 0.7 * Math.exp(-(wrap(b - 0.1) ** 2) / 0.25) * (0.6 + 0.4 * Math.sin(t * 0.6)), | |
| color: [205, 160, 255], glow: [150, 90, 255], ray: 2.2 * D, width: 9 | |
| }); | |
| // polar flares inside the main oval | |
| for (let i = 0; i < 6; i++) { | |
| const ph = i * 1.7; | |
| const f = Math.pow(Math.max(0, Math.sin(t * (1.5 + i * 0.37) + ph)), 6); | |
| g.blob(pole, (3 + (i * 1.3) % 7) * D, ph + t * 0.05, 18 + i * 3, [230, 170, 255], f * 0.9); | |
| } | |
| // moon footprints (Io has a long tail) | |
| const io = 0.4 + t * 0.33, eu = 2.3 + t * 0.21, ga = 4.4 + t * 0.14; | |
| g.foot(pole, 23 * D, io, [255, 235, 255], 1.0, 0.9); | |
| g.foot(pole, 20.5 * D, eu, [235, 210, 255], 0.75, 0.25); | |
| g.foot(pole, 18.8 * D, ga, [225, 200, 255], 0.75, 0.2); | |
| } | |
| }, | |
| saturn: { | |
| disc: [[130, 115, 80], [18, 15, 10]], | |
| draw(g, t) { | |
| const pole = sph(0.5 * D, 0); | |
| const gust = 0.5 + 0.5 * Math.sin(t * 0.25); | |
| g.oval({ | |
| pole, | |
| rho: b => (15 + 1.6 * Math.sin(b + t * 0.2) - 2.2 * gust * Math.exp(-(wrap(b + 0.4) ** 2) / 0.6)) * D, | |
| I: b => 0.25 + 0.75 * Math.pow(0.5 + 0.5 * Math.sin(3 * (b - t * 0.45)), 3) + 0.9 * gust * Math.exp(-(wrap(b - 0.0) ** 2) / 0.4), | |
| color: [140, 170, 255], glow: [110, 110, 255], ray: 2.8 * D, width: 9 | |
| }); | |
| g.oval({ | |
| pole, | |
| rho: () => 12.5 * D, | |
| I: b => 0.25 * pn(b, t * 0.6, 4), | |
| color: [230, 130, 255], glow: [210, 90, 255], ray: 1.5 * D, width: 10, glowOnly: true | |
| }); | |
| } | |
| }, | |
| uranus: { | |
| disc: [[90, 160, 175], [10, 22, 26]], | |
| draw(g, t) { | |
| const ap = t * 0.28; | |
| [[sph(59 * D, ap), 0], [sph(121 * D, ap + Math.PI), 3]].forEach(([P, s]) => { | |
| g.oval({ | |
| pole: P, | |
| rho: b => (15 + 2 * (pn(b, t * 0.4, s) - 0.5)) * D, | |
| I: b => Math.max(0, pn(b * 0.7, t * 0.8, s + 1) - 0.45) * 2.6, | |
| color: [150, 255, 235], glow: [80, 230, 220], ray: 2.6 * D, width: 9 | |
| }); | |
| }); | |
| } | |
| }, | |
| neptune: { | |
| disc: [[40, 70, 160], [6, 10, 30]], | |
| draw(g, t) { | |
| const ap = 1.2 + t * 0.3; | |
| const P = sph(47 * D, ap); | |
| g.oval({ | |
| pole: P, | |
| rho: b => (16 + 2.5 * (pn(b, t * 0.4, 2) - 0.5)) * D, | |
| I: b => Math.max(0, pn(b * 0.8, t * 0.7, 3) - 0.5) * 2.0, | |
| color: [130, 220, 255], glow: [90, 160, 255], ray: 2.2 * D, width: 9 | |
| }); | |
| } | |
| } | |
| }; | |
| class AuroraCanvas { | |
| constructor(canvas, planet) { | |
| this.c = canvas; this.ctx = canvas.getContext('2d'); this.style = STYLES[planet]; | |
| this.running = false; this.t0 = performance.now() / 1000 + Math.random() * 20; | |
| this.stars = Array.from({ length: 70 }, () => [Math.random(), Math.random(), Math.random() * 0.8 + 0.2]); | |
| this.planet = planet; | |
| this.img = new Image(); | |
| // Request with CORS so getImageData() works when textures load from another origin (e.g. a CDN). | |
| this.img.crossOrigin = 'anonymous'; | |
| this.img.onload = () => { this.polar = null; }; | |
| this.img.onerror = () => { this.imgFailed = true; }; | |
| this.img.src = `textures/${planet}.jpg`; | |
| new ResizeObserver(() => this.resize()).observe(canvas); | |
| new IntersectionObserver(es => { | |
| this.running = es[0].isIntersecting; | |
| if (this.running && !this.raf) this.raf = requestAnimationFrame(() => this.loop()); | |
| }).observe(canvas); | |
| this.resize(); | |
| } | |
| resize() { | |
| const dpr = Math.min(window.devicePixelRatio || 1, 2); | |
| const w = this.c.clientWidth || 300; | |
| this.c.width = Math.round(w * dpr); this.c.height = Math.round(w * dpr); | |
| this.W = this.c.width; this.R = this.W * 0.42; this.cx = this.W / 2; this.cy = this.W / 2 + this.W * 0.02; | |
| this.s = this.W / 400; | |
| this.polar = null; | |
| } | |
| // Orthographic view of the north pole, sampled from the equirectangular texture (built once per size). | |
| buildPolar() { | |
| if (this.polarFailed || this.imgFailed) return null; | |
| if (!this.img.complete || !this.img.naturalWidth) return null; | |
| const iw = this.img.naturalWidth, ih = this.img.naturalHeight; | |
| const src = document.createElement('canvas'); src.width = iw; src.height = ih; | |
| const sg = src.getContext('2d'); sg.drawImage(this.img, 0, 0); | |
| let sd; | |
| try { | |
| sd = sg.getImageData(0, 0, iw, ih).data; | |
| } catch (e) { | |
| // Cross-origin texture without CORS taints the canvas; fall back to the flat disc. | |
| this.polarFailed = true; | |
| return null; | |
| } | |
| const size = Math.round(this.R * 2), out = document.createElement('canvas'); out.width = out.height = size; | |
| const og = out.getContext('2d'), od = og.createImageData(size, size); | |
| for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) { | |
| const nx = (x + 0.5) / size * 2 - 1, ny = 1 - (y + 0.5) / size * 2, rr = nx * nx + ny * ny; | |
| if (rr > 1) continue; | |
| const colat = Math.asin(Math.sqrt(rr)); | |
| const lon = Math.atan2(ny, nx); | |
| const u = ((lon / TAU) % 1 + 1) % 1, v = colat / Math.PI; | |
| const si = ((Math.min(ih - 1, Math.floor(v * ih)) * iw) + Math.floor(u * iw)) * 4, di = (y * size + x) * 4; | |
| od.data[di] = sd[si]; od.data[di + 1] = sd[si + 1]; od.data[di + 2] = sd[si + 2]; od.data[di + 3] = 255; | |
| } | |
| og.putImageData(od, 0, 0); | |
| return out; | |
| } | |
| P(v) { return [this.cx + this.R * v[0], this.cy - this.R * v[1]]; } | |
| oval({ pole, rho, I, color, glow, ray, width, glowOnly }) { | |
| const g = this.ctx, [t1, t2] = basis(pole), N = 220; | |
| let prev = null; | |
| for (let k = 0; k <= N; k++) { | |
| const b = (k / N) * TAU; | |
| const r = rho(b), inten = Math.max(0, I(b)); | |
| const p = ovalPoint(pole, t1, t2, r, b); | |
| if (p[2] <= 0.02) { prev = null; continue; } | |
| const limb = 0.6 + 0.6 * (1 - p[2]); // brighter near the limb (longer path) | |
| const sp = this.P(p); | |
| if (prev) { | |
| g.strokeStyle = rgba(glow, 0.13 * inten * limb); | |
| g.lineWidth = width * this.s * 1.8; | |
| g.beginPath(); g.moveTo(prev[0], prev[1]); g.lineTo(sp[0], sp[1]); g.stroke(); | |
| g.lineWidth = width * this.s * 0.6; | |
| g.strokeStyle = rgba(glow, 0.22 * inten * limb); | |
| g.stroke(); | |
| } | |
| if (!glowOnly && k % 1 === 0) { | |
| const q = ovalPoint(pole, t1, t2, r + ray * (0.4 + inten), b); | |
| if (q[2] > 0) { | |
| const sq = this.P(q); | |
| const grd = g.createLinearGradient(sp[0], sp[1], sq[0], sq[1]); | |
| grd.addColorStop(0, rgba(color, Math.min(1, 0.85 * inten * limb))); | |
| grd.addColorStop(1, rgba(color, 0)); | |
| g.strokeStyle = grd; g.lineWidth = 1.6 * this.s; | |
| g.beginPath(); g.moveTo(sp[0], sp[1]); g.lineTo(sq[0], sq[1]); g.stroke(); | |
| } | |
| } | |
| prev = sp; | |
| } | |
| } | |
| blob(pole, rho, b, size, color, a) { | |
| if (a < 0.01) return; | |
| const [t1, t2] = basis(pole); | |
| const p = ovalPoint(pole, t1, t2, rho, b); if (p[2] <= 0) return; | |
| const [x, y] = this.P(p), r = size * this.s, g = this.ctx; | |
| const grd = g.createRadialGradient(x, y, 0, x, y, r); | |
| grd.addColorStop(0, rgba(color, a)); grd.addColorStop(1, rgba(color, 0)); | |
| g.fillStyle = grd; g.beginPath(); g.arc(x, y, r, 0, TAU); g.fill(); | |
| } | |
| foot(pole, rho, b, color, a, tail) { | |
| const [t1, t2] = basis(pole); | |
| const n = Math.round(60 * tail) + 1; | |
| for (let i = n; i >= 0; i--) { | |
| const bb = b + (i / 60) * 1.2; | |
| const p = ovalPoint(pole, t1, t2, rho, bb); if (p[2] <= 0) continue; | |
| const [x, y] = this.P(p), g = this.ctx; | |
| const f = i === 0 ? 1 : 0.35 * Math.exp(-i / 25); | |
| const r = (i === 0 ? 16 : 7) * this.s; | |
| const grd = g.createRadialGradient(x, y, 0, x, y, r); | |
| grd.addColorStop(0, rgba(color, a * f)); grd.addColorStop(1, rgba(color, 0)); | |
| g.fillStyle = grd; g.beginPath(); g.arc(x, y, r, 0, TAU); g.fill(); | |
| } | |
| } | |
| background() { | |
| const g = this.ctx, W = this.W, R = this.R; | |
| g.globalCompositeOperation = 'source-over'; | |
| g.fillStyle = '#02030a'; g.fillRect(0, 0, W, W); | |
| for (const [x, y, b] of this.stars) { g.fillStyle = `rgba(255,255,255,${b * 0.6})`; g.fillRect(x * W, y * W, 1.2 * this.s, 1.2 * this.s); } | |
| if (!this.polar) this.polar = this.buildPolar(); | |
| if (this.polar) { | |
| g.save(); g.translate(this.cx, this.cy); g.rotate(-this.spin || 0); | |
| g.drawImage(this.polar, -R, -R, 2 * R, 2 * R); g.restore(); | |
| // day side (towards the Sun, top) to night side (bottom) | |
| const sh = g.createLinearGradient(0, this.cy - R, 0, this.cy + R); | |
| sh.addColorStop(0, 'rgba(0,0,8,0.15)'); sh.addColorStop(0.45, 'rgba(0,0,8,0.55)'); sh.addColorStop(0.62, 'rgba(0,0,8,0.86)'); sh.addColorStop(1, 'rgba(0,0,8,0.9)'); | |
| g.fillStyle = sh; g.beginPath(); g.arc(this.cx, this.cy, R, 0, TAU); g.fill(); | |
| } else { | |
| const [cDay, cNight] = this.style.disc; | |
| const lin = g.createLinearGradient(0, this.cy - R, 0, this.cy + R); | |
| lin.addColorStop(0, rgba(cDay, 1)); lin.addColorStop(0.55, rgba(cNight, 1)); lin.addColorStop(1, rgba(cNight, 1)); | |
| g.fillStyle = lin; g.beginPath(); g.arc(this.cx, this.cy, R, 0, TAU); g.fill(); | |
| } | |
| // limb darkening | |
| const rad = g.createRadialGradient(this.cx, this.cy, R * 0.55, this.cx, this.cy, R); | |
| rad.addColorStop(0, 'rgba(0,0,0,0)'); rad.addColorStop(1, 'rgba(0,0,0,0.55)'); | |
| g.fillStyle = rad; g.beginPath(); g.arc(this.cx, this.cy, R, 0, TAU); g.fill(); | |
| // latitude grid | |
| g.strokeStyle = 'rgba(255,255,255,0.08)'; g.lineWidth = 1 * this.s; | |
| [30, 60].forEach(c => { g.beginPath(); g.arc(this.cx, this.cy, R * Math.sin(c * D), 0, TAU); g.stroke(); }); | |
| for (let i = 0; i < 4; i++) { const a = i * Math.PI / 4; g.beginPath(); g.moveTo(this.cx - R * Math.cos(a), this.cy - R * Math.sin(a)); g.lineTo(this.cx + R * Math.cos(a), this.cy + R * Math.sin(a)); g.stroke(); } | |
| // Sun marker | |
| g.fillStyle = 'rgba(255,220,140,0.95)'; g.font = `${18 * this.s}px system-ui, sans-serif`; g.textAlign = 'center'; g.textBaseline = 'middle'; | |
| g.fillText('☀', this.cx, this.cy - R - 14 * this.s); | |
| } | |
| loop() { | |
| this.raf = 0; | |
| if (!this.running) return; | |
| const t = performance.now() / 1000 - this.t0; | |
| this.spin = t * 0.04; // slow planet rotation under the aurora | |
| this.background(); | |
| this.ctx.globalCompositeOperation = 'lighter'; | |
| this.style.draw(this, t); | |
| this.raf = requestAnimationFrame(() => this.loop()); | |
| } | |
| } | |
| export function initAuroraGallery(root = document) { | |
| root.querySelectorAll('canvas.aurora2d').forEach(c => new AuroraCanvas(c, c.dataset.planet)); | |
| } | |