volcanoDemo = {
const H = 400; // fixed height in px
let W = 800; // width follows the container (100%)
let dpr = window.devicePixelRatio || 1;
const rnd = Math.random;
// ---------- DOM ----------
const container = document.createElement("div");
container.style.cssText = `position:relative;width:100%;height:${H}px;border-radius:10px;overflow:hidden;
background:#222;user-select:none;-webkit-user-select:none;`;
const canvas = document.createElement("canvas");
canvas.style.cssText = `width:100%;height:${H}px;display:block;touch-action:manipulation;cursor:default;`;
container.append(canvas);
const ctx = canvas.getContext("2d");
// ---------- Geometry ----------
let cx = W / 2, baseHalf = 300, groundY = H - 46, peakY = groundY - 210;
const craterR = 24;
let trees = [];
function heightAt(x) {
const d = Math.abs(x - cx);
if (d >= baseHalf) return groundY;
if (d <= craterR) { const u = d / craterR; return peakY + 16 * (1 - u * u); }
const t = (d - craterR) / (baseHalf - craterR);
const base = peakY + (groundY - peakY) * (1 - Math.pow(1 - t, 1.8));
const bump = (Math.sin(x * 0.11) * 1.6 + Math.sin(x * 0.27 + 1.3) * 0.9) * Math.min(1, t * 5) * (1 - t);
return base + bump;
}
function inVolcano(x, y) {
if (Math.abs(x - cx) > baseHalf || y > groundY + 10) return false;
return y >= heightAt(x) - 14;
}
function mulberry32(a) {
return () => {
a |= 0; a = a + 0x6D2B79F5 | 0;
let t = Math.imul(a ^ a >>> 15, 1 | a);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
// ---------- Colour helpers ----------
const lerp = (a, b, t) => a + (b - a) * t;
const mix = (c1, c2, t) =>
`rgb(${lerp(c1[0], c2[0], t) | 0},${lerp(c1[1], c2[1], t) | 0},${lerp(c1[2], c2[2], t) | 0})`;
const STOPS = [
[0.0, [38, 30, 30]], [0.15, [95, 38, 26]], [0.4, [232, 72, 20]],
[0.7, [255, 172, 40]], [1.0, [255, 240, 170]]
];
function lavaRGB(h) {
h = Math.max(0, Math.min(1, h));
for (let i = 1; i < STOPS.length; i++) {
if (h <= STOPS[i][0]) {
const [h0, c0] = STOPS[i - 1], [h1, c1] = STOPS[i];
const t = (h - h0) / (h1 - h0);
return [lerp(c0[0], c1[0], t), lerp(c0[1], c1[1], t), lerp(c0[2], c1[2], t)];
}
}
return STOPS[STOPS.length - 1][1];
}
const lavaCol = (h, a = 1) => {
const c = lavaRGB(h);
return `rgba(${c[0] | 0},${c[1] | 0},${c[2] | 0},${a})`;
};
// ---------- State ----------
let P = 0; // pressure 0..100 (clicks add, time drains)
let kick = 0; // short-lived shake from clicks
let sm = 0; // smoothed intensity for sky / glow
let hint = 1;
let clicked = false;
let lava = [], ash = [], flows = [], rings = [];
let bolt = null, flash = 0;
let lavaAcc = 0, ashAcc = 0;
function spawnLava(I) {
if (lava.length > 1400) return;
const spread = 0.3 + 0.55 * I;
const ang = -Math.PI / 2 + (rnd() - 0.5) * 2 * spread;
const s = 2.5 + 4.5 * I + rnd() * (1.5 + 2.5 * I);
lava.push({
x: cx + (rnd() - 0.5) * craterR * 1.1, y: peakY + 6,
vx: Math.cos(ang) * s, vy: Math.sin(ang) * s,
r: 1.6 + rnd() * rnd() * 4.5 * (0.6 + I), age: 0
});
}
function spawnAsh(I) {
if (ash.length > 750) return;
ash.push({
x: cx + (rnd() - 0.5) * craterR * 1.2, y: peakY + 2,
vx: (rnd() - 0.5) * (0.6 + 1.6 * I), vy: -(0.8 + 2.4 * I + rnd() * 1.2),
r: 5 + rnd() * 8, grow: 0.06 + rnd() * 0.08 + I * 0.05,
age: 0, life: 200 + rnd() * 160
});
}
function erupt() {
clicked = true;
P = Math.min(100, P + 15);
kick = Math.min(14, kick + 5);
const I = P / 100;
const nL = 14 + ((I * 40) | 0);
for (let i = 0; i < nL; i++) spawnLava(Math.min(1, I + 0.15));
const nA = 6 + ((I * 10) | 0);
for (let i = 0; i < nA; i++) spawnAsh(I);
rings.push({ r: 8, a: 0.55 });
}
// ---------- Simulation step (fixed 60 Hz) ----------
const GRAV = 0.18;
function step() {
P = Math.max(0, P - (6 + P * 0.12) / 60);
const I = P / 100;
sm += (I - sm) * 0.04;
kick *= 0.9;
// continuous emission
if (I > 0.04) lavaAcc += I * I * 4.5 + 0.05;
while (lavaAcc >= 1) { spawnLava(I); lavaAcc -= 1; }
ashAcc += 0.06 + I * 2.2;
while (ashAcc >= 1) { spawnAsh(I); ashAcc -= 1; }
// volcanic lightning
if (I > 0.55 && rnd() < (I - 0.5) * 0.025) {
const pts = [];
let x = cx + (rnd() - 0.5) * 160, y = 10 + rnd() * 30;
const endY = peakY - 30 - rnd() * 40;
const n = 9;
for (let i = 0; i <= n; i++) {
pts.push({ x, y });
x += (rnd() - 0.5) * 34;
y += (endY - y) / (n - i + 0.001) + (rnd() - 0.5) * 6;
}
bolt = { pts, life: 8 };
flash = 0.35;
}
if (bolt && --bolt.life <= 0) bolt = null;
flash *= 0.85;
// lava bombs
lava = lava.filter(p => {
p.age++;
p.vy += GRAV;
p.x += p.vx; p.y += p.vy;
if (p.x < -60 || p.x > W + 60 || p.age > 700 || p.y > H + 40) return false;
if (p.age > 8 && p.y >= heightAt(p.x)) {
const d = Math.abs(p.x - cx);
if (d > craterR + 4 && d < baseHalf && flows.length < 420 && rnd() < 0.35) {
flows.push({
x: p.x, dir: d < 2 ? (rnd() < 0.5 ? -1 : 1) : Math.sign(p.x - cx),
size: 2.5 + rnd() * 3, age: 0
});
}
return false;
}
return true;
});
// lava flows creeping down the slopes
flows = flows.filter(f => {
f.age++;
const heat = Math.max(0, 1 - f.age / 700);
if (heat > 0.04 && Math.abs(f.x - cx) < baseHalf + 10) {
const slope = Math.abs(heightAt(f.x + f.dir * 2) - heightAt(f.x)) / 2;
f.x += f.dir * (0.12 + slope * 0.9) * (0.3 + 0.7 * heat);
}
return f.age < 1300;
});
// ash plume
ash = ash.filter(a => {
a.age++;
a.x += a.vx + 0.35 + sm * 0.2 + Math.sin(a.age * 0.02 + a.x * 0.01) * 0.1;
a.y += a.vy;
a.vx *= 0.995;
a.vy = a.vy * 0.99 - 0.002;
a.r += a.grow;
return a.age < a.life && a.y > -80;
});
// shockwave rings
rings = rings.filter(r => { r.r += 7; r.a *= 0.93; return r.a > 0.02; });
}
// ---------- Rendering ----------
function draw() {
const shake = kick + sm * 8 * (0.4 + 0.6 * sm);
const sx = (rnd() - 0.5) * 2 * shake;
const sy = (rnd() - 0.5) * 2 * shake * 0.8;
ctx.setTransform(dpr, 0, 0, dpr, sx * dpr, sy * dpr);
ctx.lineCap = "round";
ctx.lineJoin = "round";
// sky
const sg = ctx.createLinearGradient(0, -40, 0, groundY);
sg.addColorStop(0, mix([86, 152, 214], [28, 18, 22], Math.min(1, sm * 1.3)));
sg.addColorStop(1, mix([190, 220, 240], [150, 56, 22], Math.min(1, sm * 1.2)));
ctx.fillStyle = sg;
ctx.fillRect(-60, -60, W + 120, H + 120);
// ash plume (behind the mountain)
for (const a of ash) {
const t = a.age / a.life;
const alpha = Math.min(1, a.age / 12) * (1 - t) * (0.22 + 0.16 * sm);
const w = Math.max(0, 1 - a.age / 50);
ctx.fillStyle = `rgba(${(66 + 84 * w) | 0},${(62 + 30 * w) | 0},${(64 - 20 * w) | 0},${alpha})`;
ctx.beginPath();
ctx.arc(a.x, a.y, a.r, 0, Math.PI * 2);
ctx.fill();
}
// ground
ctx.fillStyle = "#2f3a2b";
ctx.fillRect(-60, groundY, W + 120, H - groundY + 60);
ctx.fillStyle = "#3d4a36";
ctx.fillRect(-60, groundY, W + 120, 3);
// volcano body
const vg = ctx.createLinearGradient(0, peakY, 0, groundY);
vg.addColorStop(0, "#5b4a42");
vg.addColorStop(1, "#2e2523");
ctx.fillStyle = vg;
ctx.beginPath();
ctx.moveTo(cx - baseHalf, groundY + 2);
for (let x = cx - baseHalf; x <= cx + baseHalf; x += 3) ctx.lineTo(x, heightAt(x));
ctx.lineTo(cx + baseHalf, heightAt(cx + baseHalf));
ctx.lineTo(cx + baseHalf, groundY + 2);
ctx.closePath();
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,0.06)";
ctx.lineWidth = 1.5;
ctx.stroke();
// trees
ctx.fillStyle = "#25402a";
for (const tr of trees) {
const s = tr.s;
ctx.beginPath();
ctx.moveTo(tr.x - 6 * s, tr.y + 1);
ctx.lineTo(tr.x, tr.y - 18 * s);
ctx.lineTo(tr.x + 6 * s, tr.y + 1);
ctx.closePath();
ctx.moveTo(tr.x - 5 * s, tr.y - 8 * s);
ctx.lineTo(tr.x, tr.y - 26 * s);
ctx.lineTo(tr.x + 5 * s, tr.y - 8 * s);
ctx.closePath();
ctx.fill();
}
// lava lake in the crater
ctx.fillStyle = lavaCol(0.3 + 0.7 * sm);
ctx.beginPath();
ctx.ellipse(cx, peakY + 14, craterR * 0.9, 4.5, 0, 0, Math.PI * 2);
ctx.fill();
// flows
for (const f of flows) {
const heat = Math.max(0, 1 - f.age / 700);
const alpha = f.age > 1100 ? (1300 - f.age) / 200 : 1;
ctx.fillStyle = lavaCol(heat, alpha);
ctx.beginPath();
ctx.arc(f.x, heightAt(f.x) - f.size * 0.4, f.size, 0, Math.PI * 2);
ctx.fill();
}
// lava glow (additive)
ctx.globalCompositeOperation = "lighter";
for (const p of lava) {
const heat = Math.max(0.2, 1 - p.age / 260);
ctx.fillStyle = `rgba(255,110,20,${0.18 * heat})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.r * 3.2, 0, Math.PI * 2);
ctx.fill();
}
for (const f of flows) {
const heat = Math.max(0, 1 - f.age / 700);
if (heat < 0.25) continue;
ctx.fillStyle = `rgba(255,100,10,${0.22 * heat})`;
ctx.beginPath();
ctx.arc(f.x, heightAt(f.x) - f.size * 0.4, f.size * 2.8, 0, Math.PI * 2);
ctx.fill();
}
ctx.globalCompositeOperation = "source-over";
// lava bombs with streaky trails
for (const p of lava) {
const heat = Math.max(0.2, 1 - p.age / 260);
ctx.strokeStyle = lavaCol(heat);
ctx.lineWidth = p.r * 1.5;
ctx.beginPath();
ctx.moveTo(p.x - p.vx * 1.4, p.y - p.vy * 1.4);
ctx.lineTo(p.x, p.y);
ctx.stroke();
}
// glow around the vent
ctx.globalCompositeOperation = "lighter";
const gr = 120 + 260 * sm;
const gg = ctx.createRadialGradient(cx, peakY + 10, 0, cx, peakY + 10, gr);
gg.addColorStop(0, `rgba(255,140,40,${0.05 + 0.5 * sm})`);
gg.addColorStop(1, "rgba(255,80,10,0)");
ctx.fillStyle = gg;
ctx.fillRect(cx - gr, peakY + 10 - gr, gr * 2, gr * 2);
ctx.globalCompositeOperation = "source-over";
// shockwave rings
for (const r of rings) {
ctx.strokeStyle = `rgba(255,230,200,${r.a})`;
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(cx, peakY + 10, r.r, 0, Math.PI * 2);
ctx.stroke();
}
// lightning
if (bolt) {
const a = bolt.life / 8;
ctx.beginPath();
ctx.moveTo(bolt.pts[0].x, bolt.pts[0].y);
for (const p of bolt.pts) ctx.lineTo(p.x, p.y);
ctx.strokeStyle = `rgba(170,150,255,${0.3 * a})`;
ctx.lineWidth = 9;
ctx.stroke();
ctx.strokeStyle = `rgba(235,228,255,${a})`;
ctx.lineWidth = 2.5;
ctx.stroke();
}
if (flash > 0.01) {
ctx.fillStyle = `rgba(220,215,255,${flash})`;
ctx.fillRect(-60, -60, W + 120, H + 120);
}
// ---------- HUD (not shaken) ----------
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
const label = P < 3 ? "Dormant" : P < 25 ? "Rumbling" : P < 60 ? "Erupting"
: P < 85 ? "Violent eruption" : "CATASTROPHIC!";
ctx.fillStyle = "rgba(0,0,0,0.38)";
ctx.fillRect(10, 10, 170, 44);
ctx.fillStyle = "rgba(255,255,255,0.92)";
ctx.font = "bold 13px sans-serif";
ctx.textAlign = "left";
ctx.fillText(label, 20, 29);
ctx.fillStyle = "rgba(255,255,255,0.2)";
ctx.fillRect(20, 36, 150, 9);
const bg = ctx.createLinearGradient(20, 0, 170, 0);
bg.addColorStop(0, "#ffd54a");
bg.addColorStop(1, "#ff3d00");
ctx.fillStyle = bg;
ctx.fillRect(20, 36, 1.5 * P, 9);
if (clicked) hint = Math.max(0, hint - 0.02);
if (hint > 0) {
ctx.globalAlpha = hint * 0.85;
ctx.fillStyle = "#fff";
ctx.font = "15px sans-serif";
ctx.textAlign = "center";
ctx.fillText("Click the volcano! Click faster for bigger eruptions.", W / 2, H - 16);
ctx.globalAlpha = 1;
}
}
// ---------- Input ----------
const pos = e => {
const r = canvas.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
canvas.addEventListener("pointerdown", e => {
const p = pos(e);
if (inVolcano(p.x, p.y)) erupt();
});
canvas.addEventListener("pointermove", e => {
const p = pos(e);
canvas.style.cursor = inVolcano(p.x, p.y) ? "pointer" : "default";
});
// ---------- Loop ----------
let last = performance.now(), acc = 0, raf;
const STEP = 1000 / 60;
function frame(now) {
acc += Math.min(now - last, 100);
last = now;
while (acc >= STEP) { step(); acc -= STEP; }
draw();
raf = requestAnimationFrame(frame);
}
// ---------- Resize (width: 100%) ----------
function resize() {
const w = Math.floor(container.clientWidth);
if (w <= 0) return;
W = w;
dpr = window.devicePixelRatio || 1;
canvas.width = Math.round(W * dpr);
canvas.height = Math.round(H * dpr);
cx = W / 2;
baseHalf = Math.max(150, Math.min(W * 0.4, 420));
groundY = H - 46;
peakY = groundY - 210;
lava = []; flows = []; ash = []; rings = [];
const rng = mulberry32(42);
trees = [];
const n = Math.floor(W / 22);
for (let i = 0; i < n; i++) {
const x = rng() * W;
const s = 0.7 + rng() * 0.8;
if (Math.abs(x - cx) < baseHalf * 0.42) continue;
trees.push({ x, s, y: heightAt(x) });
}
}
const ro = new ResizeObserver(resize);
ro.observe(container);
raf = requestAnimationFrame(frame);
invalidation.then(() => {
cancelAnimationFrame(raf);
ro.disconnect();
});
return container;
}Earth & Space Science 🌋
Greetings, Earthling!
The more we know about our planet, the greater our ability to take care of it and learn to have a positive relationship with Mother Nature.
Please click on the volcano to cause an eruption and create new land!