background preloader

Tutorial

Facebook Twitter

Quick intro to using SVG in an HTML page. How to use sprite sheets with HTML5 canvas. Sprite sheets have been a part of game development since the mid-70’s and there’s a reason they’re still being used today.

How to use sprite sheets with HTML5 canvas

It uses less memory and processor power because you can load all of a character’s positions and poses in one image file instead of a different file for each one.Then you just move the sprite sheet to only show the part you want. And the great thing is, you can do this with the HTML5 canvas tag too, which makes building games more efficient. To do this, you just need to add a few move parameters to the basic drawImage function. Let’s use the ship from my canvas shooter game as an example. If we want to drive the ship on the canvas, then we would use this code: Day 11 – Animating with sprite sheets.

The holidays are over and we’re all back at the office getting back to the regular routine and feeling a bit like robots.

Day 11 – Animating with sprite sheets

No? OK, that’s maybe a stretch. Hopefully you at least like robots though, because we’re about to animate a little robot heart using sprite sheets twice: one using canvas and one using a div. Animating with a sprite sheet is like moving a connected series of images past a set viewing area. Think of a viewmaster or a film strip if you’re old enough. The image below shows you approximately what’s going on behind the scenes.

The sprite sheet We’ll be using the same sprite sheet image (this one) for both variations. First up, the canvas version Once we’ve done our initial file set up, the first thing to do is load in our sprite sheet image. Great! All those extra arguments are there because we are defining two different rectangles The first rectangle (xpos, ypos, frameSize, frameSize) defines the rectangle in our source image that we want to copy from. One down! JavaScript Day 20: SpriteSheets. Today we’ll look at a few other things to do with images.

JavaScript Day 20: SpriteSheets

We’ll start with a simplified html file: Note that now we only have the canvas. We will be loading the image dynamically in JavaScript. There’s more than one way to create an image in JavaScript. You can use document.createElement() or use some built in jQuery stuff, or go bare bones and just say new Image(). In today’s example we’ll be using the third version of drawImage to implement a sprite sheet.

I’ve used my tool, SWFSheet to generate the sprite sheet you see here: Full size, it is 512×512, with 6o frames, each one 60×60 pixels. The third overload for drawImage looks like this: context.drawImage(image, srcX, srcY, srcW, srcH, dstX, dstY, dstW, dstH); Args 2-5 define a rectangle in the source image you are drawing. 6-9 define the rectangle on the canvas you want to draw into. The source rect will start out 0, 0, 60, 60, then move to 60, 0, 60, 60, then 120, 0, 60, 60 and so on until it hits the last frame in the first row.

HTML5 Gaming: animating sprites in Canvas with EaselJS - David Rousset. When you want to write casual games using the HTML5 Canvas element, you’ll need to find a way to handle your sprites.

HTML5 Gaming: animating sprites in Canvas with EaselJS - David Rousset

There are several libraries available to help you writing games such as ImpactJS, CraftyJS and so on. On my side, I’ve decided to use EaselJS which has been used to write PiratesLoveDaisies: an HTML5 Tower Defense game as well as for the very cool Atari Arcade suite. This awesome library works in all modern HTML5 browsers and could even help you building a Windows 8 HTML5 games. For instance, if you’re running the Windows 8, you can install the Pirates Love Daisies game from the Windows Store here: Pirates Love Daisies for Windows 8 In this first article, we’re going to see how to use your existing sprite elements and animate them.

This article is the first of a series of 3 that will guide you to build a complete platformer game from scratch: I’ve also written 3 others articles going further with the same game & assets: Introduction Our monster running: But wait! David.