← AI & Me Studios

01 Setup

The page itself: the HTML shell, the CSS that makes it full-screen, and the canvas setup that gives the game a fixed 480×640 playfield on any screen.

Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Glidepath — AI &amp; Me Studios</title>
<style>
  * { margin:0; padding:0; box-sizing:border-box; }
  html, body { height:100%; background:#050816; overflow:hidden; touch-action:none; overscroll-behavior:none; }
  body { font-family:"Segoe UI", system-ui, -apple-system, sans-serif; color:#e8ecff; }
  #wrap { position:fixed; inset:0; display:flex; align-items:center; justify-content:center; }
  canvas { display:block; touch-action:none; user-select:none; -webkit-user-select:none; -webkit-tap-highlight-color:transparent; }
</style>
</head>
<body>
<div id="wrap"><canvas id="game"></canvas></div>
<script>
"use strict";
/* Glidepath — an AI & Me Studios arcade game. Original code and art.
   Tap / click / Space to thrust. Guide the rocket down its glidepath (the descent path to the pad) and land it on the pad. Pad point zones: center PERFECT +500, mid GREAT +250, outer GOOD +100. */
const cv = document.getElementById('game');
const ctx = cv.getContext('2d');

// Logical playfield (portrait, phone-first).
const W = 480, H = 640;
let scale = 1, dpr = 1;

function resize() {
  dpr = Math.min(window.devicePixelRatio || 1, 2);
  const aw = window.innerWidth, ah = window.innerHeight;
  scale = Math.min(aw / W, ah / H);
  const w = Math.round(W * scale * dpr), h = Math.round(H * scale * dpr);
  if (w === lastRW && h === lastRH) return;
  lastRW = w; lastRH = h;
  cv.style.width = (W * scale) + 'px';
  cv.style.height = (H * scale) + 'px';
  cv.width = w;
  cv.height = h;
}
let lastRW = 0, lastRH = 0;
window.addEventListener('resize', resize);
resize();
/* Same first-load race as Galactic Zone: the iframe can load while its view
   is hidden, so the first size read comes back ~0. Re-fit periodically and
   when the tab becomes visible so the game recovers within half a second. */
setInterval(resize, 500);
document.addEventListener('visibilitychange', () => { if (!document.hidden) resize(); });
What it does

This file is the top of the game: the HTML page, the CSS that makes it full-screen, and the JavaScript that sets up the canvas and the fixed logical playfield everything else is measured in.

THE HTML

The page is a plain HTML document. The <head> sets the character set and a viewport tag that locks the page to the device width, disables zooming and double-tap zoom (maximum-scale=1, user-scalable=no). That matters because this is a tap game — you don't want the browser zooming when the player taps fast.

The <body> holds one thing: a <div id="wrap"> containing a single <canvas id="game"> element. The canvas is where every pixel of the game is drawn. There are no images, no DOM game elements — just this one canvas.

THE CSS

  • * { margin:0; padding:0; box-sizing:border-box; } — strips the browser's default spacing so the game fills the screen exactly.
  • html, body { height:100%; background:#050816; overflow:hidden; touch-action:none; overscroll-behavior:none; } — the page is exactly one screen, dark navy, no scrolling, no pull-to-refresh, no touch gestures stealing taps.
  • #wrap is fixed to the whole screen and centers its child with flexbox.
  • canvas { touch-action:none; user-select:none; -webkit-tap-highlight-color:transparent; } — taps go straight to the game, nothing gets selected, and there's no grey flash when you tap.

THE CANVAS AND THE LOGICAL PLAYFIELD

    const cv = document.getElementById('game');
    const ctx = cv.getContext('2d');

cv is the canvas element, ctx is its 2D drawing context. Every draw call in the whole game goes through ctx.

    const W = 480, H = 640;

This is the key idea: the game is designed in a FIXED logical size, 480 wide by 640 tall (portrait, phone-first). All positions, speeds, and sizes in the code are in these units. The real screen can be any size; resize() scales this logical playfield to fit it.

resize()

    function resize() {
      dpr = Math.min(window.devicePixelRatio || 1, 2);
      const aw = window.innerWidth, ah = window.innerHeight;
      scale = Math.min(aw / W, ah / H);
      ...
    }
  • dpr (device pixel ratio) is how many real screen pixels make up one CSS pixel. On a phone it's often 2 or 3. It's capped at 2 so the game doesn't burn memory on very dense screens.
  • scale is the smaller of (screen width / 480) and (screen height / 640), so the whole 480x640 playfield fits without cropping.
  • The canvas's real pixel size is set to W*scale*dpr by H*scale*dpr, while its CSS size is W*scale by H*scale. That makes the drawing sharp on retina screens.
  • It remembers the last size (lastRW, lastRH) and returns early if nothing changed, so it doesn't reset the canvas on every call.

THE FIRST-LOAD RACE

The comment explains a real bug: the game can load inside an iframe while its view is hidden, so the first size read comes back ~0. The fix is belt and suspenders — resize() runs every 500ms on a timer (setInterval(resize, 500)) and again whenever the tab becomes visible (visibilitychange). So the game recovers within half a second of being shown. The same pattern is in Galactic Zone.

WHAT THIS FILE GIVES THE REST OF THE GAME

  • cv and ctx: the drawing surface.
  • W and H: the logical playfield size everything is measured in.
  • scale and dpr: how to convert logical units to real screen pixels.
  • resize(): keeps the canvas fitted to the screen.