Skip to content
Random game

HTML5 and JavaScript games

Last updated 2026-11-19

This guide is for games written in plain JavaScript or TypeScript, with a <canvas> and your own game loop. If you use a framework, the same calls go in the same places.

The script tag

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <script src="https://games.foony.com/sdk/v1.js"></script>
  <script type="module" src="main.js"></script>
</head>

With TypeScript, download the types once into your project, so the compiler knows every call:

curl -o src/freegames-sdk.d.ts https://games.foony.com/sdk/v1.d.ts

They declare window.FreeGamesSdk. With a bundler you can install the ES module and its types as a package instead, see the SDK reference.

Loading with progress

Count your files as they arrive:

const sdk = window.FreeGamesSdk;
const FILES = ['img/sheet.webp', 'audio/music.ogg', 'levels.json'];

async function loadAll() {
  let loaded = 0;
  const assets = await Promise.all(
    FILES.map(async (path) => {
      const response = await fetch(path);
      const blob = await response.blob();
      loaded++;
      sdk.loadingProgress(loaded / FILES.length);
      return blob;
    }),
  );
  return assets;
}

async function main() {
  const settings = await sdk.init();
  sdk.loadingStart();
  const assets = await loadAll();
  setupGame(assets, settings);
  sdk.loadingFinished();
}

main();

Load only what the first screen needs before loadingFinished(). Load later levels while the player is in the menu.

Fill the frame

The game frame changes size with the window, and on phones it turns with the device. Size the canvas to the window and redraw on resize:

const canvas = document.querySelector('canvas');

function fitCanvas() {
  const ratio = Math.min(window.devicePixelRatio, 2);
  canvas.width = Math.round(innerWidth * ratio);
  canvas.height = Math.round(innerHeight * ratio);
  canvas.style.width = `${innerWidth}px`;
  canvas.style.height = `${innerHeight}px`;
}

addEventListener('resize', fitCanvas);
fitCanvas();

Input

Use pointer events, so mouse, touch and pen all work with one handler:

canvas.addEventListener('pointerdown', (event) => {
  onPress(event.offsetX, event.offsetY);
});

Add touch-action: none to the canvas’s CSS so a drag doesn’t scroll the page.

Sound

Browsers start sound only after the player’s first tap, click or key press. Start your audio then, and follow the page’s mute button:

const audio = new AudioContext();

addEventListener('pointerdown', () => audio.resume(), {once: true});
addEventListener('keydown', () => audio.resume(), {once: true});

sdk.audio.onMuteChange((isMuted) => {
  masterGain.gain.value = isMuted ? 0 : 1;
});

Pause during ads

let isPaused = false;

sdk.pause.onPause(() => {
  isPaused = true;
  audio.suspend();
});
sdk.pause.onResume(() => {
  isPaused = false;
  audio.resume();
});

function frame(time) {
  if (!isPaused) {
    update(time);
    draw();
  }
  requestAnimationFrame(frame);
}
requestAnimationFrame(frame);

Play, game over, play again

function startRun() {
  sdk.gameplayStart();
  state = newRun();
}

function onPlayerDied() {
  sdk.gameplayStop();
  sdk.gameEnd('loss');
  showGameOver();
}

async function onPlayAgainPressed() {
  await sdk.commercialBreak();
  startRun();
}

Before you upload

  • Every path is relative: img/sheet.webp, not /img/sheet.webp.
  • Fonts and libraries are inside your build, not loaded from a CDN.
  • localStorage calls are inside try, or you use saves.
  • The Inspector shows no errors and the calls in order.