Tileset for HTML5 canvas game
Asked Answered
P

3

5

I'm trying to make a game in HTML5 canvas, but instead of uploading a ton of images I want to just upload one image that has all of the tiles on it. The problem is, I don't know how to make only one part of the image show up. Basically I want to do what Google does with this image: http://www.google.com/images/srpr/nav_logo27.png except with fixed height/width tiles. Can someone explain to me how to do this? Also, if it's different in canvas than in a regular html page without canvas, how would I do it in canvas?

Pantheas answered 11/12, 2010 at 23:16 Comment(0)
A
13

You can use the slicing parameters of drawImage

drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

alt text

  • sx, sy: left and top offset of the part to be sliced
  • sWidth, sHeight: dimensions of the part to be sliced
  • dx, dy: left and top offset for the image on the canvas to be rendered at
  • dWidth, dHeight: image dimensions on the canvas

More info at: Using images - Slicing (MDC)

Acquah answered 12/12, 2010 at 9:6 Comment(0)
D
0

Take a look at SpriteJS https://github.com/batiste/sprite.js/blob/master/sprite.js there the work is based on offset at the moment of drawing the sprite from a single picture.

Dalury answered 11/12, 2010 at 23:27 Comment(0)
M
0

This is how I do it with GWT: http://code.google.com/p/gwt-examples/wiki/gwt_hmtl5

Markitamarkka answered 26/3, 2011 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.