rocketDemo = {
const H = 400; // fixed height in px
const M = 20; // background overscan so screen shake never shows edges
const TAU = Math.PI * 2;
const rnd = Math.random;
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
const wrapA = a => { while (a > Math.PI) a -= TAU; while (a < -Math.PI) a += TAU; return a; };
const GM = 900000; // gravitational parameter (px^3/s^2)
const R = 38; // Earth radius
const R0 = 105; // starting orbit radius
const A_MAX = 75; // max engine acceleration (px/s^2)
const SL = Math.hypot(-0.8, -0.6);
const SUN = { x: -0.8 / SL, y: -0.6 / SL }; // direction toward the sun
let W = 900;
let dpr = window.devicePixelRatio || 1;
// ---------- DOM ----------
const container = document.createElement("div");
container.style.cssText = `position:relative;width:100%;height:${H}px;border-radius:10px;overflow:hidden;
background:#05070f;user-select:none;-webkit-user-select:none;`;
const canvas = document.createElement("canvas");
canvas.style.cssText = `width:100%;height:${H}px;display:block;touch-action:none;cursor:crosshair;`;
container.append(canvas);
const ctx = canvas.getContext("2d");
const resetBtn = document.createElement("button");
resetBtn.textContent = "Reset";
resetBtn.style.cssText = `position:absolute;top:10px;right:10px;z-index:2;padding:3px 10px;font-size:12px;
border:1px solid rgba(255,255,255,0.35);background:rgba(0,0,0,0.45);color:#fff;border-radius:6px;cursor:pointer;`;
container.append(resetBtn);
// ---------- Helpers ----------
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;
};
}
const HEAT = [
[0.0, [70, 38, 30]], [0.25, [230, 70, 20]], [0.5, [255, 170, 40]],
[0.75, [255, 225, 130]], [1.0, [255, 250, 220]]
];
function heat(h) {
h = clamp(h, 0, 1);
for (let i = 1; i < HEAT.length; i++) {
if (h <= HEAT[i][0]) {
const [h0, c0] = HEAT[i - 1], [h1, c1] = HEAT[i];
const t = (h - h0) / (h1 - h0);
return [c0[0] + (c1[0] - c0[0]) * t, c0[1] + (c1[1] - c0[1]) * t, c0[2] + (c1[2] - c0[2]) * t];
}
}
return HEAT[HEAT.length - 1][1];
}
// ---------- State ----------
let ex = 450, ey = H / 2; // Earth centre
const ship = { x: 0, y: 0, vx: 0, vy: 0, ang: 0, state: "flying" }; // flying | crashed | escaped
let T = 0, held = false; // throttle 0..1, mouse held
const aim = { x: 0, y: 0, has: false };
let parts = [], rings = [], trail = [], stars = [];
let trailT = 0, exhaustAcc = 0;
let flash = 0, shake = 0, earthRot = 0, scorch = null, glowT = 0;
let bannerT = 0, hint = 1, usedThrust = false, tPhase = 0;
let bgSpace = null, earthTex = null, cloudTex = null;
function init() {
ship.x = ex; ship.y = ey - R0;
ship.vx = Math.sqrt(GM / R0); ship.vy = 0;
ship.ang = 0;
ship.state = "flying";
T = 0; held = false;
parts = []; rings = []; trail = [];
flash = 0; shake = 0; scorch = null; glowT = 0; bannerT = 0;
exhaustAcc = 0;
}
// ---------- Physics ----------
function simStep(dt) {
if (ship.state !== "flying") return;
const ta = T * A_MAX;
const tx = Math.cos(ship.ang) * ta, ty = Math.sin(ship.ang) * ta;
let dx = ship.x - ex, dy = ship.y - ey;
let r2 = dx * dx + dy * dy, r = Math.sqrt(r2);
let g = -GM / (r2 * r);
const ax = g * dx + tx, ay = g * dy + ty;
ship.x += ship.vx * dt + 0.5 * ax * dt * dt;
ship.y += ship.vy * dt + 0.5 * ay * dt * dt;
dx = ship.x - ex; dy = ship.y - ey;
r2 = dx * dx + dy * dy; r = Math.sqrt(r2);
g = -GM / (r2 * r);
const ax2 = g * dx + tx, ay2 = g * dy + ty;
ship.vx += 0.5 * (ax + ax2) * dt;
ship.vy += 0.5 * (ay + ay2) * dt;
if (r <= R + 5) { crash(); return; }
// left the window?
if (ship.x < -40 || ship.x > W + 40 || ship.y < -40 || ship.y > H + 40) {
const E = 0.5 * (ship.vx * ship.vx + ship.vy * ship.vy) - GM / r;
const outward = dx * ship.vx + dy * ship.vy > 0;
if (E >= 0 && outward) {
ship.state = "escaped";
T = 0;
bannerT = -0.2;
}
}
}
function orbitStatus() {
const dx = ship.x - ex, dy = ship.y - ey;
const r = Math.hypot(dx, dy);
const v2 = ship.vx * ship.vx + ship.vy * ship.vy;
const E = 0.5 * v2 - GM / r;
const h = dx * ship.vy - dy * ship.vx;
let kind;
if (E >= 0) kind = "escape";
else {
const e = Math.sqrt(Math.max(0, 1 + 2 * E * h * h / (GM * GM)));
const rp = h * h / (GM * (1 + e));
kind = rp < R + 5 ? "crash" : "orbit";
}
return { kind, speed: Math.sqrt(v2), vesc: Math.sqrt(2 * GM / r), r };
}
// coasting path preview (no thrust)
function predictPath() {
const pts = [];
let x = ship.x, y = ship.y, vx = ship.vx, vy = ship.vy;
const dt = 0.02;
let dx = x - ex, dy = y - ey, r2 = dx * dx + dy * dy, r = Math.sqrt(r2);
let g = -GM / (r2 * r), ax = g * dx, ay = g * dy;
let prevA = Math.atan2(dy, dx), swept = 0, crashed = false;
for (let i = 0; i < 2200; i++) {
x += vx * dt + 0.5 * ax * dt * dt;
y += vy * dt + 0.5 * ay * dt * dt;
dx = x - ex; dy = y - ey; r2 = dx * dx + dy * dy; r = Math.sqrt(r2);
if (r <= R + 5) { crashed = true; pts.push({ x, y }); break; }
g = -GM / (r2 * r);
const nax = g * dx, nay = g * dy;
vx += 0.5 * (ax + nax) * dt;
vy += 0.5 * (ay + nay) * dt;
ax = nax; ay = nay;
if (i % 3 === 0) pts.push({ x, y });
const a = Math.atan2(dy, dx);
swept += Math.abs(wrapA(a - prevA));
prevA = a;
if (swept > TAU) break;
if (x < -250 || x > W + 250 || y < -250 || y > H + 250) break;
}
return { pts, crashed };
}
// ---------- Explosion ----------
function crash() {
const dx = ship.x - ex, dy = ship.y - ey;
const r = Math.hypot(dx, dy) || 1;
const nx = dx / r, ny = dy / r;
const ix = ex + nx * R, iy = ey + ny * R;
const sp = Math.hypot(ship.vx, ship.vy);
ship.state = "crashed";
ship.x = ix; ship.y = iy;
T = 0; held = false;
scorch = { a: Math.atan2(ny, nx) - earthRot };
glowT = 6;
flash = 0.9; shake = 14; bannerT = -0.6;
const k = clamp(sp / 110, 0.6, 1.7);
const base = Math.atan2(ny, nx);
for (let i = 0; i < 120 * k; i++) {
const a = base + (rnd() - 0.5) * 2.7, s = (30 + rnd() * 240) * k;
parts.push({ type: "fire", x: ix + nx * 2, y: iy + ny * 2, vx: Math.cos(a) * s, vy: Math.sin(a) * s,
t: 0.5 + rnd() * 1.0, max: 1, r: 2 + rnd() * 5 * k });
parts[parts.length - 1].max = parts[parts.length - 1].t;
}
for (let i = 0; i < 50; i++) {
const a = base + (rnd() - 0.5) * 2.9, s = 120 + rnd() * 330 * k;
const t = 0.4 + rnd() * 0.7;
parts.push({ type: "spark", x: ix, y: iy, vx: Math.cos(a) * s, vy: Math.sin(a) * s, t, max: t, r: 1 + rnd() });
}
for (let i = 0; i < 40; i++) {
const a = base + (rnd() - 0.5) * 1.8, s = 10 + rnd() * 60;
const t = 2 + rnd() * 2;
parts.push({ type: "smoke", x: ix + nx * 4, y: iy + ny * 4, vx: Math.cos(a) * s, vy: Math.sin(a) * s,
t, max: t, r: 6 + rnd() * 8, grow: 8 + rnd() * 10 });
}
for (let i = 0; i < 26; i++) {
const a = base + (rnd() - 0.5) * 2.2, s = 60 + rnd() * 200 * k;
const t = 3 + rnd() * 2;
parts.push({ type: "debris", x: ix + nx * 3, y: iy + ny * 3, vx: Math.cos(a) * s, vy: Math.sin(a) * s,
t, max: t, w: 2 + rnd() * 3.5, rot: rnd() * TAU, vr: (rnd() - 0.5) * 18, col: rnd() < 0.5 ? "#dfe3ea" : "#c63b3b" });
}
rings.push({ x: ix, y: iy, r: 6, a: 0.85, v: 190 });
rings.push({ x: ix, y: iy, r: 4, a: 0.55, v: 110 });
}
// ---------- Per-frame update ----------
function frameUpdate(dtF) {
tPhase += dtF;
earthRot += dtF * 0.12;
shake *= Math.exp(-dtF * 6);
if (shake < 0.05) shake = 0;
flash *= Math.exp(-dtF * 4);
if (glowT > 0) glowT = Math.max(0, glowT - dtF);
if (usedThrust) hint = Math.max(0, hint - dtF * 0.8);
if (ship.state !== "flying") bannerT += dtF;
// throttle: holding ramps up, releasing bleeds off, taps add a kick (see pointerdown)
if (ship.state === "flying") {
T = held ? Math.min(1, T + 0.6 * dtF) : Math.max(0, T - 1.0 * dtF);
} else {
T = 0;
}
// point toward the cursor
if (ship.state === "flying") {
const target = aim.has ? Math.atan2(aim.y - ship.y, aim.x - ship.x) : Math.atan2(ship.vy, ship.vx);
const da = wrapA(target - ship.ang);
const maxTurn = 10 * dtF;
ship.ang += clamp(da * Math.min(1, 14 * dtF), -maxTurn, maxTurn);
}
// exhaust particles
if (ship.state === "flying" && T > 0.03) {
exhaustAcc += T * 5;
const c = Math.cos(ship.ang), s = Math.sin(ship.ang);
while (exhaustAcc >= 1) {
exhaustAcc -= 1;
const sp = (70 + rnd() * 110) * (0.5 + T);
const jx = (rnd() - 0.5) * 50, jy = (rnd() - 0.5) * 50;
const t = 0.35 + rnd() * 0.45;
parts.push({
type: "exhaust",
x: ship.x - c * 13, y: ship.y - s * 13,
vx: ship.vx * 0.7 - c * sp + jx, vy: ship.vy * 0.7 - s * sp + jy,
t, max: t, r: 1.5 + rnd() * 1.8
});
}
}
// trail
trailT += dtF;
if (trailT > 0.035) {
trailT = 0;
if (ship.state === "flying") trail.push({ x: ship.x, y: ship.y });
else if (trail.length) trail.shift();
if (trail.length > 240) trail.shift();
}
// particles
for (let i = parts.length - 1; i >= 0; i--) {
const p = parts[i];
p.t -= dtF;
if (p.t <= 0) { parts.splice(i, 1); continue; }
if (p.type === "debris") {
const dx = p.x - ex, dy = p.y - ey;
const r2 = dx * dx + dy * dy, r = Math.sqrt(r2);
const g = -GM / (r2 * r);
p.vx += g * dx * dtF;
p.vy += g * dy * dtF;
p.rot += p.vr * dtF;
if (r < R) { parts.splice(i, 1); continue; }
} else if (p.type === "smoke") {
p.r += p.grow * dtF;
const d = Math.exp(-1.0 * dtF);
p.vx *= d; p.vy *= d;
} else {
const d = Math.exp((p.type === "exhaust" ? -0.8 : -1.2) * dtF);
p.vx *= d; p.vy *= d;
}
p.x += p.vx * dtF;
p.y += p.vy * dtF;
}
for (let i = rings.length - 1; i >= 0; i--) {
const r = rings[i];
r.r += r.v * dtF;
r.a *= Math.exp(-2.2 * dtF);
if (r.a < 0.02) rings.splice(i, 1);
}
// twinkle
for (const s of stars) s.ph += dtF * s.sp;
}
// ---------- Rendering ----------
function drawEarth() {
// atmosphere glow
const ag = ctx.createRadialGradient(ex, ey, R * 0.95, ex, ey, R + 34);
ag.addColorStop(0, "rgba(110,170,255,0.55)");
ag.addColorStop(1, "rgba(110,170,255,0)");
ctx.fillStyle = ag;
ctx.beginPath(); ctx.arc(ex, ey, R + 34, 0, TAU); ctx.fill();
// rotating surface (polar view), scorch mark, clouds
ctx.save();
ctx.beginPath(); ctx.arc(ex, ey, R, 0, TAU); ctx.clip();
ctx.translate(ex, ey);
ctx.save();
ctx.rotate(earthRot);
ctx.drawImage(earthTex, -R, -R, 2 * R, 2 * R);
if (scorch) {
ctx.rotate(scorch.a);
ctx.fillStyle = "rgba(25,12,6,0.8)";
ctx.beginPath(); ctx.ellipse(R - 3, 0, 7, 11, 0, 0, TAU); ctx.fill();
ctx.fillStyle = "rgba(60,30,15,0.5)";
ctx.beginPath(); ctx.ellipse(R - 6, 0, 11, 16, 0, 0, TAU); ctx.fill();
}
ctx.restore();
ctx.rotate(earthRot * 1.4);
ctx.drawImage(cloudTex, -R, -R, 2 * R, 2 * R);
ctx.restore();
// day / night shading
ctx.save();
ctx.beginPath(); ctx.arc(ex, ey, R, 0, TAU); ctx.clip();
const sg = ctx.createLinearGradient(ex + SUN.x * R, ey + SUN.y * R, ex - SUN.x * R, ey - SUN.y * R);
sg.addColorStop(0, "rgba(255,255,230,0.14)");
sg.addColorStop(0.45, "rgba(0,0,20,0)");
sg.addColorStop(1, "rgba(0,0,15,0.72)");
ctx.fillStyle = sg;
ctx.fillRect(ex - R, ey - R, 2 * R, 2 * R);
ctx.restore();
ctx.strokeStyle = "rgba(170,210,255,0.5)";
ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.arc(ex, ey, R, 0, TAU); ctx.stroke();
// glowing crater
if (scorch && glowT > 0) {
const a = scorch.a + earthRot;
const gx = ex + Math.cos(a) * R, gy = ey + Math.sin(a) * R;
const f = glowT / 6;
const gg = ctx.createRadialGradient(gx, gy, 0, gx, gy, 26);
gg.addColorStop(0, `rgba(255,170,60,${0.8 * f * (0.8 + 0.2 * Math.sin(tPhase * 14))})`);
gg.addColorStop(1, "rgba(255,80,10,0)");
ctx.globalCompositeOperation = "lighter";
ctx.fillStyle = gg;
ctx.beginPath(); ctx.arc(gx, gy, 26, 0, TAU); ctx.fill();
ctx.globalCompositeOperation = "source-over";
}
}
function drawRocket() {
ctx.save();
ctx.translate(ship.x, ship.y);
ctx.rotate(ship.ang);
ctx.scale(1.15, 1.15);
if (T > 0.03) {
const fl = (10 + T * 36) * (0.85 + rnd() * 0.3);
ctx.globalCompositeOperation = "lighter";
const gg = ctx.createRadialGradient(-12, 0, 1, -12, 0, 14 + T * 16);
gg.addColorStop(0, "rgba(255,200,100,0.7)");
gg.addColorStop(1, "rgba(255,100,20,0)");
ctx.fillStyle = gg;
ctx.beginPath(); ctx.arc(-12, 0, 14 + T * 16, 0, TAU); ctx.fill();
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "#ff8a1f";
ctx.beginPath(); ctx.moveTo(-11, -4); ctx.lineTo(-11 - fl, 0); ctx.lineTo(-11, 4); ctx.closePath(); ctx.fill();
ctx.fillStyle = "#ffd84a";
ctx.beginPath(); ctx.moveTo(-11, -2.4); ctx.lineTo(-11 - fl * 0.65, 0); ctx.lineTo(-11, 2.4); ctx.closePath(); ctx.fill();
ctx.fillStyle = "#ffffff";
ctx.beginPath(); ctx.moveTo(-11, -1.2); ctx.lineTo(-11 - fl * 0.35, 0); ctx.lineTo(-11, 1.2); ctx.closePath(); ctx.fill();
}
// fins
ctx.fillStyle = "#d63a3a";
ctx.beginPath(); ctx.moveTo(-6, -4); ctx.lineTo(-12, -9.5); ctx.lineTo(-12, -3); ctx.closePath(); ctx.fill();
ctx.beginPath(); ctx.moveTo(-6, 4); ctx.lineTo(-12, 9.5); ctx.lineTo(-12, 3); ctx.closePath(); ctx.fill();
// nozzle
ctx.fillStyle = "#555a64";
ctx.fillRect(-12, -2.5, 4, 5);
// body
const bg = ctx.createLinearGradient(0, -4, 0, 4);
bg.addColorStop(0, "#ffffff");
bg.addColorStop(1, "#b9c3d4");
ctx.fillStyle = bg;
ctx.fillRect(-9, -4, 15, 8);
// nose
ctx.fillStyle = "#d63a3a";
ctx.beginPath();
ctx.moveTo(6, -4);
ctx.quadraticCurveTo(12, -3, 15, 0);
ctx.quadraticCurveTo(12, 3, 6, 4);
ctx.closePath();
ctx.fill();
// porthole
ctx.fillStyle = "#4aa3ff";
ctx.strokeStyle = "#2a3550";
ctx.lineWidth = 0.8;
ctx.beginPath(); ctx.arc(0, 0, 2.2, 0, TAU); ctx.fill(); ctx.stroke();
ctx.restore();
}
function drawParticles() {
// opaque-ish layers first
for (const p of parts) {
const f = p.t / p.max;
if (p.type === "smoke") {
ctx.fillStyle = `rgba(70,70,76,${0.35 * f})`;
ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, TAU); ctx.fill();
} else if (p.type === "debris") {
ctx.save();
ctx.translate(p.x, p.y);
ctx.rotate(p.rot);
ctx.globalAlpha = Math.min(1, f * 2);
ctx.fillStyle = p.col;
ctx.fillRect(-p.w, -p.w * 0.5, p.w * 2, p.w);
ctx.restore();
}
}
ctx.globalAlpha = 1;
// additive layers
ctx.globalCompositeOperation = "lighter";
for (const p of parts) {
const f = p.t / p.max;
if (p.type === "fire" || p.type === "spark" || p.type === "exhaust") {
const c = heat(p.type === "exhaust" ? 0.35 + 0.6 * f : f);
const a = p.type === "exhaust" ? 0.7 * f : 0.9 * f;
ctx.fillStyle = `rgba(${c[0] | 0},${c[1] | 0},${c[2] | 0},${a})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.r * (0.5 + 0.5 * f), 0, TAU);
ctx.fill();
}
}
ctx.globalCompositeOperation = "source-over";
for (const r of rings) {
ctx.strokeStyle = `rgba(255,230,190,${r.a})`;
ctx.lineWidth = 3;
ctx.beginPath(); ctx.arc(r.x, r.y, r.r, 0, TAU); ctx.stroke();
}
}
function drawTrail() {
ctx.lineCap = "round";
ctx.lineWidth = 2;
const n = trail.length;
for (let i = 1; i < n; i++) {
ctx.strokeStyle = `rgba(170,205,255,${0.32 * i / n})`;
ctx.beginPath();
ctx.moveTo(trail[i - 1].x, trail[i - 1].y);
ctx.lineTo(trail[i].x, trail[i].y);
ctx.stroke();
}
}
function drawPrediction(status) {
const { pts, crashed } = predictPath();
const col = crashed ? [255, 105, 85] : status.kind === "escape" ? [255, 205, 100] : [125, 255, 165];
const n = pts.length;
for (let i = 0; i < n; i++) {
ctx.fillStyle = `rgba(${col[0]},${col[1]},${col[2]},${0.85 * (1 - 0.7 * i / n)})`;
ctx.beginPath(); ctx.arc(pts[i].x, pts[i].y, 1.3, 0, TAU); ctx.fill();
}
if (crashed && n) {
const p = pts[n - 1];
ctx.strokeStyle = "rgba(255,105,85,0.95)";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(p.x - 5, p.y - 5); ctx.lineTo(p.x + 5, p.y + 5);
ctx.moveTo(p.x + 5, p.y - 5); ctx.lineTo(p.x - 5, p.y + 5);
ctx.stroke();
}
}
function drawOffscreenArrow() {
if (ship.x >= 0 && ship.x <= W && ship.y >= 0 && ship.y <= H) return;
const ix = clamp(ship.x, 16, W - 16), iy = clamp(ship.y, 16, H - 16);
const a = Math.atan2(ship.y - iy, ship.x - ix);
ctx.save();
ctx.translate(ix, iy);
ctx.rotate(a);
ctx.fillStyle = "rgba(255,255,255,0.85)";
ctx.beginPath(); ctx.moveTo(9, 0); ctx.lineTo(-6, -7); ctx.lineTo(-6, 7); ctx.closePath(); ctx.fill();
ctx.restore();
}
function drawHUD(status) {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.fillStyle = "rgba(0,0,0,0.42)";
ctx.fillRect(10, 10, 214, 100);
// status
let label, col;
if (ship.state === "crashed") { label = "CRASHED"; col = "#ff6b4a"; }
else if (ship.state === "escaped") { label = "ESCAPED"; col = "#8fe3ff"; }
else if (status.kind === "orbit") { label = "STABLE ORBIT"; col = "#7dffa5"; }
else if (status.kind === "crash") { label = "CRASH COURSE"; col = "#ff6b4a"; }
else { label = "ESCAPE TRAJECTORY"; col = "#ffcd64"; }
ctx.textAlign = "left";
ctx.fillStyle = col;
ctx.font = "bold 14px Lato, system-ui, sans-serif";
ctx.fillText(label, 20, 29);
// speed vs escape speed
const flying = ship.state === "flying";
const ratio = flying ? status.speed / status.vesc : 0;
const maxR = 1.25, bx = 20, bw = 194;
const tOrbit = bx + bw * (0.7071 / maxR), tEsc = bx + bw * (1 / maxR);
ctx.fillStyle = "rgba(255,255,255,0.8)";
ctx.font = "9px Lato, system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText("orbit", tOrbit, 42);
ctx.fillText("escape", tEsc, 42);
ctx.fillStyle = "rgba(255,255,255,0.2)";
ctx.fillRect(bx, 46, bw, 9);
const fc = ratio >= 1 ? "#ffb03a" : ratio >= 0.7071 ? "#7dffa5" : "#6cb7ff";
ctx.fillStyle = fc;
ctx.fillRect(bx, 46, bw * clamp(ratio / maxR, 0, 1), 9);
ctx.fillStyle = "#fff";
ctx.fillRect(tOrbit - 0.5, 44, 1.5, 13);
ctx.fillRect(tEsc - 0.5, 44, 1.5, 13);
ctx.textAlign = "left";
ctx.fillStyle = "rgba(255,255,255,0.85)";
ctx.font = "10px Lato, system-ui, sans-serif";
ctx.fillText("SPEED", 20, 68);
ctx.textAlign = "right";
ctx.fillText(flying ? `${Math.round(ratio * 100)}% of escape` : "-", 214, 68);
// thrust
ctx.fillStyle = "rgba(255,255,255,0.2)";
ctx.fillRect(bx, 76, bw, 9);
const tg = ctx.createLinearGradient(bx, 0, bx + bw, 0);
tg.addColorStop(0, "#ffd84a"); tg.addColorStop(1, "#ff4a2e");
ctx.fillStyle = tg;
ctx.fillRect(bx, 76, bw * T, 9);
ctx.textAlign = "left";
ctx.fillStyle = "rgba(255,255,255,0.85)";
ctx.fillText("THRUST", 20, 98);
ctx.textAlign = "right";
ctx.fillText(`${Math.round(T * 100)}%`, 214, 98);
// hint
if (hint > 0) {
ctx.globalAlpha = hint * 0.9;
ctx.textAlign = "center";
ctx.font = "14px Lato, system-ui, sans-serif";
const msg = "Move the mouse to aim. Click or hold to fire the engine; tap rapidly for more thrust.";
const tw = ctx.measureText(msg).width;
ctx.fillStyle = "rgba(0,0,0,0.45)";
ctx.fillRect(W / 2 - tw / 2 - 12, H - 40, tw + 24, 26);
ctx.fillStyle = "#fff";
ctx.fillText(msg, W / 2, H - 22);
ctx.globalAlpha = 1;
}
// end banner
if (ship.state !== "flying") {
const a = clamp(bannerT * 2, 0, 1);
if (a > 0) {
const crashed = ship.state === "crashed";
ctx.globalAlpha = a;
ctx.textAlign = "center";
ctx.lineJoin = "round";
ctx.lineWidth = 6;
ctx.strokeStyle = "rgba(0,0,0,0.7)";
ctx.font = "bold 34px Lato, system-ui, sans-serif";
const title = crashed ? "CRASHED!" : "ESCAPED!";
ctx.strokeText(title, W / 2, 62);
ctx.fillStyle = crashed ? "#ff6b4a" : "#8fe3ff";
ctx.fillText(title, W / 2, 62);
ctx.font = "14px Lato, system-ui, sans-serif";
const sub = crashed ? "The rocket hit the Earth. Press Reset to try again."
: "Escape velocity reached. The rocket left the window. Press Reset to try again.";
ctx.lineWidth = 4;
ctx.strokeText(sub, W / 2, 86);
ctx.fillStyle = "#fff";
ctx.fillText(sub, W / 2, 86);
ctx.globalAlpha = 1;
}
}
}
function draw() {
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, W, H);
const sx = (rnd() - 0.5) * 2 * shake, sy = (rnd() - 0.5) * 2 * shake;
ctx.setTransform(dpr, 0, 0, dpr, sx * dpr, sy * dpr);
ctx.drawImage(bgSpace, -M, -M, W + 2 * M, H + 2 * M);
for (const s of stars) {
ctx.fillStyle = `rgba(255,255,255,${0.35 + 0.65 * (0.5 + 0.5 * Math.sin(s.ph))})`;
ctx.beginPath(); ctx.arc(s.x, s.y, s.r, 0, TAU); ctx.fill();
}
drawEarth();
const status = orbitStatus();
if (ship.state === "flying") drawPrediction(status);
drawTrail();
drawParticles();
if (ship.state === "flying") {
drawRocket();
if (aim.has) {
ctx.save();
ctx.setLineDash([3, 5]);
ctx.strokeStyle = "rgba(255,255,255,0.28)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(ship.x + Math.cos(ship.ang) * 20, ship.y + Math.sin(ship.ang) * 20);
ctx.lineTo(ship.x + Math.cos(ship.ang) * 62, ship.y + Math.sin(ship.ang) * 62);
ctx.stroke();
ctx.restore();
}
drawOffscreenArrow();
}
if (flash > 0.01) {
ctx.fillStyle = `rgba(255,235,200,${flash * 0.6})`;
ctx.fillRect(-M, -M, W + 2 * M, H + 2 * M);
}
drawHUD(status);
}
// ---------- Input ----------
const pos = e => {
const r = canvas.getBoundingClientRect();
return { x: e.clientX - r.left, y: e.clientY - r.top };
};
canvas.addEventListener("pointermove", e => {
const p = pos(e);
aim.x = p.x; aim.y = p.y; aim.has = true;
});
canvas.addEventListener("pointerdown", e => {
const p = pos(e);
aim.x = p.x; aim.y = p.y; aim.has = true;
if (ship.state === "flying") {
held = true;
T = Math.min(1, T + 0.25); // each click adds a burst of thrust
usedThrust = true;
}
canvas.setPointerCapture(e.pointerId);
e.preventDefault();
});
const onUp = e => {
held = false;
if (canvas.hasPointerCapture && canvas.hasPointerCapture(e.pointerId)) canvas.releasePointerCapture(e.pointerId);
};
canvas.addEventListener("pointerup", onUp);
canvas.addEventListener("pointercancel", onUp);
resetBtn.addEventListener("click", init);
// ---------- Textures and layers ----------
function makeEarthTextures() {
const size = Math.round(2 * R * dpr);
const mk = () => {
const c = document.createElement("canvas");
c.width = c.height = size;
const g = c.getContext("2d");
g.scale(dpr, dpr);
g.translate(R, R);
return { c, g };
};
// surface
const s = mk();
const og = s.g.createRadialGradient(-R * 0.2, -R * 0.2, R * 0.1, 0, 0, R);
og.addColorStop(0, "#2a82e0");
og.addColorStop(1, "#0a3a8c");
s.g.fillStyle = og;
s.g.beginPath(); s.g.arc(0, 0, R, 0, TAU); s.g.fill();
s.g.save();
s.g.beginPath(); s.g.arc(0, 0, R, 0, TAU); s.g.clip();
const rng = mulberry32(7);
const lands = ["#3f8f4a", "#5fa04a", "#8aa84f", "#c9b27a"];
for (let k = 0; k < 9; k++) {
const a = rng() * TAU, d = R * (0.28 + rng() * 0.55);
const cx = Math.cos(a) * d, cy = Math.sin(a) * d;
s.g.fillStyle = lands[(rng() * lands.length) | 0];
for (let j = 0; j < 7; j++) {
s.g.beginPath();
s.g.arc(cx + (rng() - 0.5) * R * 0.5, cy + (rng() - 0.5) * R * 0.5, R * (0.08 + rng() * 0.14), 0, TAU);
s.g.fill();
}
}
const ic = s.g.createRadialGradient(0, 0, 0, 0, 0, R * 0.22);
ic.addColorStop(0, "#ffffff");
ic.addColorStop(0.7, "rgba(255,255,255,0.9)");
ic.addColorStop(1, "rgba(255,255,255,0)");
s.g.fillStyle = ic;
s.g.beginPath(); s.g.arc(0, 0, R * 0.22, 0, TAU); s.g.fill();
s.g.restore();
earthTex = s.c;
// clouds
const cl = mk();
cl.g.lineCap = "round";
const r2 = mulberry32(21);
for (let i = 0; i < 16; i++) {
const rr = R * (0.25 + r2() * 0.7), a0 = r2() * TAU, len = 0.6 + r2() * 1.4;
cl.g.strokeStyle = `rgba(255,255,255,${0.2 + r2() * 0.18})`;
cl.g.lineWidth = 2 + r2() * 3;
cl.g.beginPath(); cl.g.arc(0, 0, rr, a0, a0 + len); cl.g.stroke();
}
for (let i = 0; i < 10; i++) {
const a = r2() * TAU, d = R * (0.2 + r2() * 0.7);
cl.g.fillStyle = "rgba(255,255,255,0.22)";
cl.g.beginPath(); cl.g.arc(Math.cos(a) * d, Math.sin(a) * d, R * (0.06 + r2() * 0.1), 0, TAU); cl.g.fill();
}
cloudTex = cl.c;
}
function paintSpace() {
const c = document.createElement("canvas");
c.width = Math.round((W + 2 * M) * dpr);
c.height = Math.round((H + 2 * M) * dpr);
const g = c.getContext("2d");
g.scale(dpr, dpr);
g.translate(M, M);
const sg = g.createLinearGradient(0, -M, 0, H + M);
sg.addColorStop(0, "#02030a");
sg.addColorStop(1, "#0a1030");
g.fillStyle = sg;
g.fillRect(-M, -M, W + 2 * M, H + 2 * M);
const rng = mulberry32(3);
// faint nebulae
for (const [fx, fy, col] of [[0.2, 0.7, "120,80,200"], [0.78, 0.25, "60,120,220"], [0.55, 0.85, "200,90,140"]]) {
const r = 120 + rng() * 80;
const ng = g.createRadialGradient(W * fx, H * fy, 0, W * fx, H * fy, r);
ng.addColorStop(0, `rgba(${col},0.10)`);
ng.addColorStop(1, `rgba(${col},0)`);
g.fillStyle = ng;
g.fillRect(W * fx - r, H * fy - r, r * 2, r * 2);
}
// stars
const n = Math.floor((W * H) / 1500);
for (let i = 0; i < n; i++) {
const x = -M + rng() * (W + 2 * M), y = -M + rng() * (H + 2 * M);
const b = rng();
g.fillStyle = `rgba(255,255,255,${0.2 + b * 0.6})`;
g.beginPath(); g.arc(x, y, 0.4 + b * 0.9, 0, TAU); g.fill();
}
bgSpace = c;
stars = [];
for (let i = 0; i < 24; i++) {
stars.push({ x: rng() * W, y: rng() * H, r: 0.8 + rng() * 1.1, ph: rng() * TAU, sp: 1 + rng() * 2.5 });
}
}
// ---------- 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);
ex = W / 2; ey = H / 2;
makeEarthTextures();
paintSpace();
init();
}
const ro = new ResizeObserver(resize);
ro.observe(container);
// ---------- Loop ----------
let last = performance.now(), acc = 0, raf;
const DT = 1 / 240;
function frame(now) {
const dtF = Math.min(0.1, (now - last) / 1000);
last = now;
acc += dtF;
while (acc >= DT) { simStep(DT); acc -= DT; }
frameUpdate(dtF);
if (bgSpace && earthTex) draw();
raf = requestAnimationFrame(frame);
}
raf = requestAnimationFrame(frame);
invalidation.then(() => {
cancelAnimationFrame(raf);
ro.disconnect();
});
return container;
}