- Tutorials / JavaScript
- Friday, 5th Oct, 2012
Home » Tutorials » JavaScript » Animating Sprites In HTML5 Canvas
When you want to build your own web games using the HTML5 Canvas element, you will need to find a way to handle your sprites animation. In this article, we will introduce HTML5 Canvas animation and walks you through creating an animation of character walking.
If you are new to HTML5 Canvas element, I suggest you to read this article first “HTML5 Canvas For Absolute Beginners“.
We are going to use the following PNG file as source of our sprite sequence:
![]()
PNG sprite sequence
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<title>Animating Sprites In HTML5 Canvas | onlyWebPro.com</title>
</head>
<body>
<canvas id="myCanvas" width="100" height="100">
<!-- Insert fallback content here -->
Sorry, your browser doesn't support canvas technology
</canvas>
<script>
//Script goes here
</script>
</body>
</html>
We will create a canvas for displaying our sprites and set the width, height of the canvas to 100px. Please bear in mind that, the width and height of each frame of the PNG sprite sequence is 100 pixel:
![]()
Besides, please remember to assign an id for our canvas element. For this case, we named it as “myCanvas“.
And that’s all for the HTML structure! Now, let’s take a look at the JavaScript section.
<script>
var width = 100,
height = 100,
frames = 4,
currentFrame = 0,
canvas = document.getElementById("myCanvas");
ctx = canvas.getContext("2d");
image = new Image()
image.src = 'sprite.png';
var draw = function(){
ctx.clearRect(0, 0, width, height);
ctx.drawImage(image, 0, height * currentFrame, width, height, 0, 0, width, height);
if (currentFrame == frames) {
currentFrame = 0;
} else {
currentFrame++;
}
}
setInterval(draw, 100);
</script>
As you seen above, the first thing we do in JavaScript section is to define the variables:
To animate the sprite, we need just display each frame of the sprite using drawImage() method. The drawImage() is a method that allow us to draw image or video onto canvas. Below is the syntax and parameters of drawImage():
context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);
| Parameter | Description |
|---|---|
| img | Specifies the image, canvas, or video element to use |
| sx | Optional parameter. The x coordinate where to start clipping |
| sy | Optional parameter. The y coordinate where to start clipping |
| swidth | Optional parameter. The width of the clipped image |
| sheight | Optional parameter. The height of the clipped image |
| x | The x coordinate where to place the image on the canvas |
| y | The y coordinate where to place the image on the canvas |
| width | Optional parameter. The width of the image to use (stretch or reduce the image) |
| height | Optional parameter. The height of the image to use (stretch or reduce the image) |
Next, we write a conditional statement to check if the currentFrame is less than total frames, then increment the currentFrame. The currentFrame is used to decide which frame of the sprite should we display on the canvas via drawImage() method.
Lastly, we use “setInterval()” to run the draw function every 100 milliseconds.
That’s it! Save your document and run it on supported browser!
PHP: Object Oriented Programming for Absolute Beginners
HTML5 Canvas For Absolute Beginners – Part 4
Introduction To Device Orientation With HTML5
CSS3: Spinner Animation
Solving CSS Float's Problem
Create Facebook App Style Slide-Out Navigation
Build Intelligent Layout Using CSS Calc() Property
Catch It!
Make A jQuery Sticky Header In 5 Minutes
Koubachi Web Game
HTML5 Brain Challenge - Maths Edition