HTML/JS/CSS Isometric Grid with semi-transparent click-through tiles

Adam M-W picture Adam M-W · Feb 14, 2010 · Viewed 8k times · Source

I'm trying to create a web app/game that uses a side-on 'isometric' view and transparent tiles. I can display them ok (but not great) using a PHP formula that just sets each div (each tile) as position:absolute and set the top and left parameters. The problem is how do I catch clicks on a tile and let tiles with transparent bits click-through to the tile below it.

An example of my problem is at http://stuff.adammw.homeip.net/other/fv/farmville_2.html

Answer

bobince picture bobince · Feb 14, 2010

You won't be able to do this with the tile elements themselves, as they are always rectangular. You'll have to monitor the mouse position within a parent element containing all the tiles and manually work out which pixels correspond to which tiles.

For an isometric mapping of position to tile this would be easy, but that would be taking the point position as a place ‘on the ground’; it wouldn't allow you to use the image's mask data to hit-test objects like the trees that render in front of the ground. (Do you want to do that anyway? Currently some trees totally obscure the ground tile below them.)

If you really need to hit test against image masks, you can:

a. write a script to extract the mask data from the image and include it in the JavaScript source as encoded binary bitmasks. I've done this for collision detection in JavaScript games, but it's not much fun as it can take a lot of space and you have to re-export the data when you update the graphics.

b. extract the image data for testing using a <canvas> and getImageData. At which point you might consider using a canvas for rendering too, given that you'll have already lost IE users.