HTML5 Game Physics Velocity

HTML5 Game Physics Position And Velocity

This tutorial is the first in a series about HTML5 game physics. The HTML5 canvas element will act as a stage for all game characters. JavaScript will be used for game logic and updating canvas items.

The first tutorial will draw a simple rectangle that will act as a game character. The character will move horizontally across the canvas. The code is based on an article by Hunter Loftis for Skookum Digital Works titled “Physics for Lazy Game Developers”.

This series is intermediate and assumes basic knowledge of physics and 2D graphics. The character position is determined by static velocity multiplied by displacement that is calculated every frame loop. 2D graphics require separate x and y velocities.

This tutorial uses the HTML5 canvas tag and JavaScript.

    Tools are required:

  • Text editor.
  • Folder for web server.
  • Browser to view output.

HTML5 Game Physics Position & Velocity File

   Copyright 2013 edward <>
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301, USA.

<!DOCTYPE html>
<html xml:lang="en" lang="en">

	<title> HTML5 Game Physics Position And Velocity</title>
	<meta charset="utf-8" />
	<link rel="stylesheet" type="text/css" href="style.css">
	<style type="text/css">
		canvas#myCanvas {
			width: 80%;
			height: 80%;
			background: green;

	<p>Canvas Character Position & Velocity</p>
	<canvas id="myCanvas"></canvas>
		<script type="text/JavaScript">
		// Position, Size & Velocity
		var rect = {
			x: 0,
			y: 0,
			w: 20,
			h: 20,
			vx: 1,
			vy: 0
		// Find <canvas> element
		// Call getContext() method and pass 2d string
		// Update Every 20 milliseconds
		setInterval(function(){ draw();},20);	
		function draw() {			
			// Clear display;
			ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
			// Apply velocity to position (vx -> x)
			rect.x += rect.vx;
			rect.y += rect.vy;
			// Draw onto the canvas;
			// Fill style red <CSS color, a gradient, or a pattern>
			// Rectangle fillRect(x,y,width,height)


The canvas element is styled with CSS in order to determine observe the character position. The canvas object is found by its unique identifier in JavaScript. The character parameters for x and y position, width and height, and x and y velocity are stored in a JSON object called “rect”.

2D drawing for the canvas is enabled using the “getContext” method. The “setInterval” method updates the canvas every 20 seconds. The draw function clears the canvas, updates the character position via the velocity x value, and redraws the character.

How to Use:

    Open Browser

  • Observe the canvas.
  • Observe the character moving across the canvas.

Demonstration: HTML5 Game Physics Velocity Tutorial

Image Missing HTML5 Game Physics Position & Velocity


HTML5 game development requires the use of the canvas element. JavaScript is used to create canvas characters and program the game logic.

A unique identifier can be used in order to target the specific canvas element. Character movement can be based on game physics created using JavaScript.


  1. Style the canvas using CSS.
  2. Hold all character parameters in a JSON object.
  3. Use a unique identifiers to target the canvas element.