HTML5 game development tutorials

How to make an HTML5 game

Learn how to make an HTML5 game with JavaScript, step by step and for free. Find out how a browser game works, how to plan your game and what you need to get started. Then follow this tutorial series and build your own game.

Part of the HTML5 game tutorial series

This is where the series starts. Here, you'll learn how an HTML5 game works and how to plan one. The parts after this one take you from an empty canvas to a finished game, one step at a time.

Show all partsHide all parts

This page is the introduction to the Spicy Yoghurt tutorial series about HTML5 game development. You'll find out what an HTML5 game really is, which building blocks it's made of and what makes a game fun. You'll see how to plan a game, and how this tutorial series helps you build one step by step. Further down, you'll find a link to the tutorial for each step, so you can follow the whole series or go straight to the tutorial you need.

What is an HTML5 game?

HTML5 logo

An HTML5 game is a game that runs in a web browser. It's built with standard web technologies: HTML, CSS and JavaScript. There's nothing to install and no plugin is needed. You open a page and play, on a computer, a tablet or a phone.

The name is a bit misleading, though. HTML is a markup language. It describes the structure and content of a web page, but it can't run any game logic. And HTML isn't even called HTML5 anymore. Version 5 became an official standard in 2014, and today HTML is a living standard without a version number. So when people talk about an HTML5 game, or just an HTML game, they mean a game made with the whole modern web platform.

Here's what each piece of the web platform does in a game:

  • HTML - Puts a <canvas> element on the page. That's what your game is drawn on.
  • JavaScript - The programming language you write your game logic in. It moves your objects and draws them on the canvas.
  • CSS - Styles the page around your game, like the background and the position of the canvas. For the game itself, CSS doesn't do much: everything inside the canvas is drawn with JavaScript.
  • Browser APIs - Tools built into the browser that you control with JavaScript, such as the Canvas API for 2D graphics, the Web Audio API for sound and WebGL for 3D.

Most of the time, you'll be writing JavaScript. HTML creates the canvas, and JavaScript draws your game on it.

How does an HTML5 game work?

If you've never programmed a game before, you'll need to think in a new way. On a normal web page, code only runs when something happens. You click a button, the code behind it runs once, and then the page waits for the next click. A game is different. It runs its code over and over, many times per second, even when the player isn't doing anything. Every time, it draws a new image on the screen. Each of those images is called a frame.

Most HTML5 games are made of the same building blocks:

  • Canvas - An HTML element you can draw on with JavaScript. It's the screen of your game.
  • Game loop - The heart of every game. It runs many times per second, and each time, it updates the game and draws a new frame.
  • Game objects - Everything in your game that has a place on the screen, like the player, the enemies and the things you pick up. Each object keeps track of where it is and how fast it moves.
  • Input - The keyboard, the mouse or a touchscreen. The browser tells your game when the player presses a key, clicks or touches the screen, and the game loop reacts to it in the next frame.
  • Movement - Objects move a little bit every frame, and all those small steps add up to smooth motion.
  • Collision detection and physics - The game checks which objects touch each other and decides what happens next. A ball bounces off a wall, or a player loses a life. Physics makes objects move and bounce like real ones, with gravity and weight.
  • Graphics - Everything the player sees, like shapes, text and images. To animate a character, you show a series of drawings one after another. These drawings are often combined into one image, called a sprite sheet.
  • Game states - The different modes your game can be in, like the start screen, the game itself, a pause menu and a game over screen. Each state draws its own screen and reacts to the player in its own way.
  • Sound - Sound effects and music, played with the Web Audio API or a simple <audio> element.

The game loop ties all of this together. Every frame, it reads the input, updates the objects, checks for collisions, clears the canvas and draws everything again:

Not every game needs all of these building blocks. Take Conway's Game of Life, for example. It's not a game you play but one you watch, as cells on a grid live or die by a few simple rules. It has no collisions or input, just a canvas, a grid of cells and a loop. That makes it a nice project to practice with.

Game design: what makes a game fun?

A game can work perfectly and still be boring. So before you plan your own, it helps to know what makes a game fun to play. Here are a few basic principles of game design:

  • A clear goal - The player should know right away what to do, like reaching the exit or collecting coins. If they have to guess, they'll stop playing.
  • A challenge - Something has to get in the way, like enemies, a timer or a tricky jump. Without it, there's nothing to win.
  • Choices - The best moments come from small decisions. Do you grab the coin next to the enemy, or play it safe? That's called risk and reward.
  • Feedback - Every time the player does something, the game should react, with a sound, a flash or points that pop up. That reaction tells the player what happened and makes the game feel alive.
  • Fair difficulty - Start easy and make the game harder as the player gets better. When players lose, they should see why, so they'll want to try again.
  • Simple controls - Easy to learn, hard to master. A player should be able to start playing within a few seconds, and still be getting better an hour later.

Most of the fun comes from how these principles fit together. A goal without a challenge is boring, and a challenge without feedback is frustrating. Short rounds help too. If a round takes a minute and ends with a score to beat, players will want one more try.

Plan your game before you write any code

It's tempting to open your editor and start coding right away. But first, decide what your game is about and write it down. That's your game design, and it doesn't have to be a long document. For a first game, a few notes will do: the goal, what gets in the player's way and when the game is over. With a plan like that, you know what to build first and what to leave out.

Keep your first game small. One goal and one thing to watch out for is plenty. A small game that you finish teaches you more than a big one that you never get done. So don't expect to build the next Call of Duty in this series.

Here's the plan for the bee game in this tutorial series:

  • Idea - A bee collects pollen and dodges other bees. Try to describe your own game in one sentence, like this. If you can't, it's probably too big for a first game.
  • Goal - Collect as much pollen as you can.
  • Enemies - Other bees fly around and bounce off the edges of the screen and off each other. Stay out of their way. Pollen right next to a bee is risky to grab, but sometimes worth it.
  • Lives - You have three. Every bee that hits you costs you one, and when you run out, the game is over.
  • Score - Every grain of pollen is worth points. The game remembers your best score, so there's always a record to beat.
  • Controls - The arrow keys or WASD on a computer, and your finger on a phone. The mouse works too.

Here's the same plan as a board of sticky notes. Each note is one part of the game, like the player, the pollen and the enemies, and the arrows show how the parts work together. Drag the notes around to sort out the plan, for example by putting the parts that belong together side by side:

Some notes still have a question mark: the sound, the start screen and what happens when the game is over. That's fine. You don't need every answer before you start, and the rest of the series fills in the blanks.

Learn HTML5 game development step by step

A game has a lot of building blocks, but you can't test movement or collisions until there's something on the screen. So this tutorial series doesn't go through the building blocks in the order the game loop uses them. It starts with what you can see right away, and adds one building block at a time. Every part builds on the one before, so you always have something that works. Here's the order:

Along the way, you'll build your own version of the bee game. Each part has live demos and code you can copy, and you'll learn how to avoid common mistakes.

What do you need to make an HTML5 game?

Not much, and all of it is freely available:

  • A browser - Chrome, Firefox, Safari or Edge. You'll test your game in it, and it has developer tools built in.
  • A text editor - Any editor will do. Visual Studio Code is free and popular, and it colors your code so it's easier to read.
  • Some JavaScript - You should know what variables, functions, if statements and loops are. If you're completely new to JavaScript, the JavaScript Guide on MDN is a good place to start. You'll pick up the rest as you go.
  • Graphics - Simple shapes are enough to start with, and you can swap them for images later. The bees in this series started as vector drawings, made of lines and curves instead of pixels. Want to draw your own game graphics? Check out the tutorial on drawing vector potion bottles in Inkscape, a free drawing program.
  • A backup - Before you make a big change, save a copy of your game folder. If something breaks, you can go back to the copy, or compare the two folders to see what changed, for example with our Folder Compare tool. For bigger projects, most developers use version control with Git, which keeps a history of all your changes.

You don't need a server, a game engine or a build tool. An HTML5 game can be a single HTML file that you open in your browser.

Sooner or later, your game won't show up or will suddenly stop working. When that happens, the console in your browser tells you why, and on which line of your code. Press F12 (or Cmd+Option+I on a Mac) to open your browser's developer tools, and click the Console tab. In Safari, first turn on Show features for web developers under Settings > Advanced. Checking the console first will save you a lot of time.

Plain JavaScript or a game engine?

This series uses plain JavaScript, without a library or a game engine. That's on purpose, so you can see how a game works under the hood. Plenty of developers keep it that way and build their games without an engine, with full control over every line. And if you do switch to an engine later, you'll know what it does for you. For a small game, like Pong, Snake or a puzzle game, you don't need anything more anyway.

For a bigger game, an engine can save you a lot of work. It comes with a game loop, physics, sound and image loading, ready to use. It also handles most of the differences between browsers and devices. Here are some popular engines and libraries:

  • Phaser - A free framework for 2D games, with scenes, physics, sound, input and tile maps. One of the most widely used HTML5 game frameworks, with lots of examples to learn from.
  • PixiJS - Draws 2D graphics very fast. It's not a complete engine, though. The game logic and physics are up to you.
  • Three.js and Babylon.js - For 3D games in the browser.
  • Godot and Unity - Engines with an editor, for games on every platform. They can export your game to the web, but you don't write it in JavaScript.

So where should you start? With this series, even if you plan to use an engine later. An engine makes a lot more sense once you know what a game loop and collision detection are. If you choose an engine after that, what you've learned here still applies.

Make your HTML5 game work on mobile

One of the biggest advantages of HTML5 is that your game runs on mobile devices. You can show it to everyone who owns a phone. So that's about... everyone.

Run your HTML5 game on all devices

But a game that runs on a phone doesn't automatically play well on one. A phone has a small screen, no keyboard and no mouse, and it's slower than a computer. Here's what to look out for:

  • Controls - On a phone, the player can only tap and swipe. So make sure your game can be played with just a finger, without arrow keys. You'll learn how in the part about touch controls, which covers everything from tapping an object to building a virtual joystick.
  • Scrolling and zooming - When the player swipes or taps on your game, the browser may scroll or zoom the page instead. To stop that, give your canvas touch-action: none in CSS.
  • Screen size - Phones come in all shapes and sizes, and players can turn them sideways. Scale your canvas to fit the screen, and keep it sharp on a high-resolution display. You'll learn how to do both when you set up an HTML5 canvas.
  • Sound - Browsers don't play any sound until the player has tapped or clicked the page. So start your music after the first tap, for example when the player presses a start button. The part about game audio shows you how.
  • Performance - A phone has less power than a computer, and it slows itself down when it gets hot. Keep your images small, don't draw more than you need, and test on a real phone, not just in the mobile view of your browser's developer tools.

This tutorial series covers each of these points. Want to see an example? Our own game Pixi Pop started as an Android app, and now it runs in the browser on a computer, a tablet and a phone. On a computer you click, on a touchscreen you tap, and the same code handles both. You can play it right in your browser.

What happened to Flash?

Adobe Flash logo

Phones are also the reason browser games are built with HTML5 today. Here's a brief history lesson to explain why. Before HTML5, almost every browser game was made with Adobe Flash. Flash was great for animations and games, and there wasn't really any other option at the time. To play a Flash game, you had to install Flash Player, a browser plugin. It took care of the differences between browsers, so as a developer you didn't have to.

Its big weakness was mobile. In 2010, Apple made it clear that it would never support Flash on the iPhone and iPad. Steve Jobs explained why in his letter Thoughts on Flash, pointing to poor performance, battery drain and security problems, among other things. In the years after that, browsers like Chrome and Firefox slowly turned away from Flash, and Adobe stopped supporting Flash Player at the end of 2020. As of January 2021, Flash Player won't run any Flash content at all.

HTML5 took its place. At first, it wasn't easy to make a good game with it. Every browser did things a little differently, and even playing a sound could be a struggle. Luckily, things have improved a lot. Today, every browser supports the canvas, the Web Audio API and WebGL, on computers and phones.

And the old Flash games? A lot of them can still be played. Ruffle is a Flash Player emulator that runs in the browser, without a plugin. The Internet Archive uses it to keep thousands of old Flash games playable.

Common questions about HTML5 games

Is HTML5 good for game development?

Yes, for 2D games and smaller 3D games. HTML5 games run on every device with a browser, and players can start playing with a single click. Big 3D games with heavy graphics still run better as native games that players download and install.

Do I need to know JavaScript to make an HTML5 game?

Usually, yes. Most HTML5 games are written in JavaScript. You don't have to be an expert, though. If you know variables, functions and loops, you can follow this tutorial series. With an engine like Godot or Unity, you write your game in another language, and the engine turns it into a web game for you.

Can you make 3D games with HTML5?

Yes. Browsers have WebGL, and its successor WebGPU, for 3D graphics. Most developers use a library like Three.js or Babylon.js, because WebGL on its own takes a lot of code. This tutorial series is about 2D games on the canvas.

Do HTML5 games work offline?

They can. Once all the files are on the device, the game doesn't need an internet connection. To get them there, turn your game into a progressive web app with a service worker, a small script that saves your game's files in the browser. Then the game keeps working offline. You'll find the code for the service worker in the tutorial on how to publish your HTML5 game. A game with online high scores or multiplayer still needs a connection for those parts.

What's a good first game to make?

Something small, with one goal and simple controls. If you've never made a game before, follow this tutorial series and build the bee game step by step. After that, a remake of a classic like Pong or Breakout is a good next project, because you already know how it should play. That way, you can focus on the code instead of the game design.

How do you save a player's progress?

With HTML5 local storage. The browser remembers small pieces of data for your game, like a high score or the level the player reached, even after the player closes the page. You don't need a server or an account for that. In the JavaScript game states tutorial, you'll use it to save the high score of the bee game.

How long does it take to make an HTML5 game?

That depends on the game. If you know a bit of JavaScript, set aside an evening or two for each part of this series. That adds up to a few weeks. A simple game like Pong or Snake is a nice weekend project after that. A game with levels, menus and sound, like Pixi Pop, takes a lot longer.

Can you make money with an HTML5 game?

Yes, but don't expect to get rich right away. You can show ads on your own site, publish your game on a game portal that shares its ad revenue with you, or sell it in the app stores, on Steam or as a download on itch.io. Most developers make a few small games first, to learn and to find out what players like. In the last part of this series, you'll learn how to publish your HTML5 game.

What's the difference between HTML5 and JavaScript?

HTML describes what's on a page, like a canvas. JavaScript is the programming language that makes things happen, like moving a player when a key is pressed. An HTML5 game needs both, but most of the code you write is JavaScript.

What's next?

Now you know what an HTML5 game is, which parts it's made of and what the plan for the bee game looks like. Time to start building!

Do you have an idea for a game of your own? Sum it up in one sentence, like the idea for the bee game, and post it in the comments. It might even give us an idea for a new tutorial. And if you have any questions, feel free to ask them there too.