/* * Orbital Flight Simulator - canvas renderer. * Pure drawing: reads game state, never mutates physics. */ (function (global) { 'use strict'; var P = global.OrbitalPhysics; function Renderer(canvas) { this.canvas = canvas; this.ctx = canvas.getContext('2d'); this.dpr = 1; this.w = 800; this.h = 600; this.stars = []; for (var i = 0; i < 350; i++) { this.stars.push({ x: Math.random() * 2048, y: Math.random() * 2048, r: Math.random() * 1.3 + 0.4, b: Math.random() * 0.6 + 0.25, p: 0.03 + Math.random() * 0.12 // parallax factor }); } this.resize(); var self = this; global.addEventListener('resize', function () { self.resize(); }); } Renderer.prototype.resize = function () { var dpr = global.devicePixelRatio || 1; var w = this.canvas.clientWidth || global.innerWidth || 800; var h = this.canvas.clientHeight || global.innerHeight || 600; this.dpr = dpr; this.w = w; this.h = h; this.canvas.width = Math.max(1, Math.round(w * dpr)); this.canvas.height = Math.max(1, Math.round(h * dpr)); }; Renderer.prototype.toScreen = function (x, y, cam) { return { x: (x - cam.x) * cam.zoom + this.w / 2, y: (y - cam.y) * cam.zoom + this.h / 2 }; }; Renderer.prototype.render = function (game) { var ctx = this.ctx; ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0); ctx.fillStyle = '#04060d'; ctx.fillRect(0, 0, this.w, this.h); var cam = game.camera; this.drawStars(cam); this.drawPlanet(game); this.drawTrajectory(game); this.drawStation(game); this.drawExhaust(game); this.drawShip(game); this.drawMarkers(game); }; Renderer.prototype.drawStars = function (cam) { var ctx = this.ctx; var T = 2048; for (var i = 0; i < this.stars.length; i++) { var s = this.stars[i]; var ox = ((s.x - cam.x * s.p) % T + T) % T; var oy = ((s.y - cam.y * s.p) % T + T) % T; for (var dx = -T; dx <= this.w; dx += T) { for (var dy = -T; dy <= this.h; dy += T) { var x = ox + dx; var y = oy + dy; if (x < -2 || y < -2 || x > this.w + 2 || y > this.h + 2) continue; ctx.globalAlpha = s.b; ctx.fillStyle = '#cdd8ff'; ctx.fillRect(x, y, s.r, s.r); } } } ctx.globalAlpha = 1; }; Renderer.prototype.drawPlanet = function (game) { var ctx = this.ctx; var cam = game.camera; var world = game.world; var c = this.toScreen(world.planet.x, world.planet.y, cam); var R = world.planetRadius * cam.zoom; if (c.x + R < -150 || c.y + R < -150 || c.x - R > this.w + 150 || c.y - R > this.h + 150) return; // atmosphere glow var glow = ctx.createRadialGradient(c.x, c.y, Math.max(0, R * 0.92), c.x, c.y, R * 1.18); glow.addColorStop(0, 'rgba(90,160,255,0)'); glow.addColorStop(0.5, 'rgba(90,160,255,0.28)'); glow.addColorStop(1, 'rgba(90,160,255,0)'); ctx.fillStyle = glow; ctx.beginPath(); ctx.arc(c.x, c.y, R * 1.18, 0, P.TAU); ctx.fill(); // body var g = ctx.createRadialGradient(c.x - R * 0.35, c.y - R * 0.35, R * 0.1, c.x, c.y, R); g.addColorStop(0, '#7ec8e3'); g.addColorStop(0.4, '#3a7ca5'); g.addColorStop(0.75, '#1d4e6b'); g.addColorStop(1, '#0c2438'); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(c.x, c.y, R, 0, P.TAU); ctx.fill(); // craters when zoomed in if (R > 120) { ctx.save(); ctx.beginPath(); ctx.arc(c.x, c.y, R, 0, P.TAU); ctx.clip(); for (var i = 0; i < 14; i++) { var a = (i * 137.5 * Math.PI) / 180; var rr = (0.15 + 0.75 * ((i * 0.618) % 1)) * R; var cx = c.x + Math.cos(a) * rr; var cy = c.y + Math.sin(a) * rr; var cr = (0.04 + 0.09 * ((i * 0.37) % 1)) * R; ctx.fillStyle = 'rgba(10,30,45,0.35)'; ctx.beginPath(); ctx.arc(cx, cy, cr, 0, P.TAU); ctx.fill(); } ctx.restore(); } }; Renderer.prototype.drawTrajectory = function (game) { if (!game.prediction || game.prediction.points.length < 2) return; var ctx = this.ctx; var cam = game.camera; var info = P.orbitInfo(game.world, game.ship.pos, game.ship.vel); var res = game.prediction.result; ctx.save(); ctx.setLineDash([6, 6]); ctx.lineWidth = 1.5; if (res.type === 'crash') ctx.strokeStyle = 'rgba(255,90,70,0.9)'; else if (info.bound) ctx.strokeStyle = 'rgba(90,255,160,0.8)'; else ctx.strokeStyle = 'rgba(255,170,60,0.8)'; ctx.beginPath(); var started = false; for (var i = 0; i < game.prediction.points.length; i++) { var p = game.prediction.points[i]; var s = this.toScreen(p.x, p.y, cam); if (s.x < -50 || s.x > this.w + 50 || s.y < -50 || s.y > this.h + 50) { started = false; continue; } if (!started) { ctx.moveTo(s.x, s.y); started = true; } else ctx.lineTo(s.x, s.y); } ctx.stroke(); ctx.setLineDash([]); if (res.type === 'crash' || res.type === 'land') { var last = game.prediction.points[game.prediction.points.length - 1]; var ls = this.toScreen(last.x, last.y, cam); ctx.strokeStyle = res.type === 'crash' ? '#ff5a46' : '#7dff9a'; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(ls.x - 7, ls.y - 7); ctx.lineTo(ls.x + 7, ls.y + 7); ctx.moveTo(ls.x + 7, ls.y - 7); ctx.lineTo(ls.x - 7, ls.y + 7); ctx.stroke(); } ctx.restore(); }; Renderer.prototype.drawStation = function (game) { var st = game.station; if (!st) return; var ctx = this.ctx; var cam = game.camera; var s = this.toScreen(st.pos.x, st.pos.y, cam); if (s.x < -150 || s.x > this.w + 150 || s.y < -150 || s.y > this.h + 150) return; var R = Math.max(6, st.radius * cam.zoom); ctx.save(); ctx.translate(s.x, s.y); // docking ring ctx.setLineDash([4, 4]); ctx.strokeStyle = 'rgba(120,220,255,0.5)'; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(0, 0, game.world.dockDistance * cam.zoom, 0, P.TAU); ctx.stroke(); ctx.setLineDash([]); ctx.rotate(st.angle); var pw = R * 2.2; var ph = R * 0.9; ctx.fillStyle = '#2b6cb0'; ctx.strokeStyle = '#9ecbff'; ctx.lineWidth = 1; ctx.fillRect(-pw - R * 0.4, -ph / 2, pw, ph); ctx.strokeRect(-pw - R * 0.4, -ph / 2, pw, ph); ctx.fillRect(R * 0.4, -ph / 2, pw, ph); ctx.strokeRect(R * 0.4, -ph / 2, pw, ph); ctx.strokeStyle = '#c8d6e5'; ctx.lineWidth = Math.max(1.5, R * 0.25); ctx.beginPath(); ctx.moveTo(-R * 2.6, 0); ctx.lineTo(R * 2.6, 0); ctx.stroke(); ctx.fillStyle = '#dfe9f5'; ctx.beginPath(); ctx.arc(0, 0, R, 0, P.TAU); ctx.fill(); ctx.strokeStyle = '#8fa8c4'; ctx.lineWidth = 1; ctx.stroke(); ctx.restore(); ctx.fillStyle = 'rgba(150,220,255,0.9)'; ctx.font = '11px monospace'; ctx.textAlign = 'center'; ctx.fillText('STATION', s.x, s.y - R * 2.4 - 6); }; Renderer.prototype.drawExhaust = function (game) { var ctx = this.ctx; var cam = game.camera; for (var i = 0; i < game.exhaust.length; i++) { var p = game.exhaust[i]; var s = this.toScreen(p.x, p.y, cam); if (s.x < -20 || s.x > this.w + 20 || s.y < -20 || s.y > this.h + 20) continue; var t = p.life / p.maxLife; ctx.globalAlpha = t * 0.8; ctx.fillStyle = t > 0.5 ? '#ffd166' : '#ff7b4f'; ctx.beginPath(); ctx.arc(s.x, s.y, p.size * (1.6 - t * 0.6), 0, P.TAU); ctx.fill(); } ctx.globalAlpha = 1; }; Renderer.prototype.drawShip = function (game) { var ship = game.ship; if (!ship) return; var ctx = this.ctx; var cam = game.camera; var s = this.toScreen(ship.pos.x, ship.pos.y, cam); ctx.save(); ctx.translate(s.x, s.y); ctx.rotate(ship.angle); var L = 16; var Wd = 8; if (ship.state === 'crashed') ctx.globalAlpha = 0.5; ctx.fillStyle = ship.state === 'crashed' ? '#666' : '#e8eef7'; ctx.strokeStyle = '#9fb4cc'; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(L, 0); ctx.lineTo(-L * 0.7, -Wd); ctx.lineTo(-L * 0.4, 0); ctx.lineTo(-L * 0.7, Wd); ctx.closePath(); ctx.fill(); ctx.stroke(); ctx.fillStyle = '#59d3ff'; ctx.beginPath(); ctx.arc(L * 0.25, 0, 2.5, 0, P.TAU); ctx.fill(); ctx.restore(); }; Renderer.prototype.drawMarkers = function (game) { var ship = game.ship; if (!ship || ship.state !== 'flying') return; var cam = game.camera; var ctx = this.ctx; var s = this.toScreen(ship.pos.x, ship.pos.y, cam); // prograde (velocity) arrow var v = ship.vel; var sp = Math.sqrt(v.x * v.x + v.y * v.y); if (sp > 1) { var ux = v.x / sp; var uy = v.y / sp; var len = Math.min(90, 24 + sp * 0.12); var ex = s.x + ux * len; var ey = s.y + uy * len; ctx.strokeStyle = 'rgba(90,220,255,0.9)'; ctx.lineWidth = 1.5; ctx.beginPath(); ctx.moveTo(s.x + ux * 18, s.y + uy * 18); ctx.lineTo(ex, ey); ctx.stroke(); var hx = -uy; var hy = ux; ctx.fillStyle = 'rgba(90,220,255,0.9)'; ctx.beginPath(); ctx.moveTo(ex + ux * 7, ey + uy * 7); ctx.lineTo(ex - ux * 2 + hx * 4, ey - uy * 2 + hy * 4); ctx.lineTo(ex - ux * 2 - hx * 4, ey - uy * 2 - hy * 4); ctx.closePath(); ctx.fill(); } // off-screen station marker var st = game.station; if (st) { var ss = this.toScreen(st.pos.x, st.pos.y, cam); var d = Math.sqrt( (st.pos.x - ship.pos.x) * (st.pos.x - ship.pos.x) + (st.pos.y - ship.pos.y) * (st.pos.y - ship.pos.y) ); if (ss.x < 0 || ss.x > this.w || ss.y < 0 || ss.y > this.h) { var cx = this.w / 2; var cy = this.h / 2; var dx = ss.x - cx; var dy = ss.y - cy; var scale = Math.min( (this.w / 2 - 24) / Math.abs(dx || 1e-9), (this.h / 2 - 24) / Math.abs(dy || 1e-9) ); var mx = cx + dx * scale; var my = cy + dy * scale; ctx.save(); ctx.translate(mx, my); ctx.rotate(Math.atan2(dy, dx)); ctx.fillStyle = 'rgba(120,220,255,0.95)'; ctx.beginPath(); ctx.moveTo(10, 0); ctx.lineTo(-6, -6); ctx.lineTo(-6, 6); ctx.closePath(); ctx.fill(); ctx.restore(); ctx.font = '10px monospace'; ctx.textAlign = 'center'; ctx.fillText(Math.round(d) + ' m', mx, my + 18); } } }; global.Renderer = Renderer; })(typeof window !== 'undefined' ? window : globalThis);