Urgent.News

What's breaking now, across thousands of outlets.

Tech

How Browser Games Work: HTML5, Canvas and WebGL Explained

Browser games have changed dramatically over the years. Older web games often depended on plugins such as Flash, but modern browser games can now run directly inside Chrome, Firefox, Safari, Edge and other modern browsers using open web technologies. Today, developers can build surprisingly sophisticated games using technologies such as: HTML5 JavaScript Canvas WebGL Web Audio API WebAssembly…

Browser games have evolved significantly over recent years. Earlier web games relied on plugins like Flash, but modern browser games can now run directly in browsers such as Chrome, Firefox, Safari, Edge, and others using open web technologies. Developers can create complex games using technologies like HTML5, JavaScript Canvas, WebGL, Web Audio API, WebAssembly, and WebSockets. Understanding how these technologies work together explains why browser gaming is now faster, more accessible, and more capable.

A browser game runs directly inside a web browser, loading its code, graphics, audio, and other assets from a web server. The process involves the player opening a game page, the browser downloading game files, HTML and JavaScript initializing the game, graphics being rendered using Canvas or WebGL, and input (keyboard, mouse, or touch) being detected.

HTML5 provides the foundational structure for modern interactive web applications, enabling browser games to run without external plugins. The canvas element creates a drawing area where JavaScript can render graphics, animations, characters, and environments onto it. For example, a rectangle can be drawn using this code: const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); ctx.fillRect(100, 100, 50, 50);

The canvas provides a programmable area for rendering graphics frame by frame. A game loop, which repeatedly updates the game, might look like this: function gameLoop() { update(); render(); requestAnimationFrame(gameLoop); } gameLoop(); In the update function, game elements are calculated such as player movement, enemy movement, collision detection, scores, timers, and game state. The render function draws the updated scene on the canvas.

WebGL makes it possible to create advanced 2D and 3D graphics in browser games. Modern game engines often use WebGL when exporting games for browsers. JavaScript is the brain of a browser game, controlling player input, enemy AI, physics, scores, menus, animations, sound, networking, and game progression. For example, basic keyboard movement might look like this: window.addEventListener('keydown', event = { if (event.key === 'ArrowRight') { player.x += 5; } });

Modern browsers also offer the Web Audio API for managing background music, sound effects, positional audio, audio mixing, volume controls, and dynamic sound, which is crucial for providing feedback by combining visual and audio cues.

Multiplayer games require communication between the player's browser and a server. WebSockets create a persistent connection between the browser and the game server, allowing information to move in both directions continuously. For example, when a player moves, the browser sends their coordinates to the server, which updates the game state and shares the new position with other players.

WebAssembly, often shortened to WASM, allows code written in languages like C, C++, or Rust to run efficiently in web browsers. This technology can be useful for computationally demanding tasks such as physics engines, game engines, simulations, advanced graphics, and complex calculations. WebAssembly does not replace JavaScript but can work alongside it.

Game engines like Unity, Godot, Phaser, Three.js, Babylon.js, PlayCanvas, and PixiJS can export games into packages containing HTML, JavaScript, assets, and WebAssembly files, which are then uploaded to a server and loaded when someone starts the game.

When a player clicks 'Play' on a browser game, the browser loads the HTML page, and JavaScript initializes the game. The game engine proceeds to set up the game environment and begin the game loop, continuously updating and rendering the game based on player input and game logic.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

The Provenance Layer — Or, You Can't Audit What You Can't Rebuild

Yesterday was the solvency layer (#63): the vendor holding your switch can run out of money. Today's front page answers the next question down — how do you know what the thing on your machine actually…

  • Provenance Layer addresses authenticity of software dependencies
  • Most dependencies cannot be rebuilt from public inputs
  • Implement provenance by pinning dependencies by digest and using lockfiles

Property Import Rollbacks with a Node.js Feature Flag Kill Switch

TL;DR: Put the kill decision in a small server-side gate, cache the last valid flag snapshot, and make the fallback disable the new import path.

  • Implement kill switch using feature flag in Node.js backend property import system
  • Cache last valid flag snapshot and have fallback mechanism disabling new import path
  • Monitor progress of scheduled import with run ID, watermark, counts, completion time

Bad Habits in Web Development We Should Avoid

Web development mixes creativity, technology and user experience. It's also easy to pick up habits that quietly erode a site's quality, accessibility and maintainability.

  • Neglecting mobile design from the outset degrades site quality and accessibility
  • Inconsistent design elements confuse users and give unprofessional impression
  • Hardcoding URLs makes website maintenance and scaling difficult

More from Monday 19 October →