In my last tutorial I talked about how the isometric map could be enhanced, for example by using canvas as a tool to determinate the real position of an object and not the whole image including transparency. In this small article I would like to introduce this technique. I got inspired by Dextrose AG and their faboulus upcoming AVES Engine. I’m sure their approach is much more efficient and functional. But mine should give you a good point to start.
It really is just a good starting point and should not be used in a public project, just like the isometric map plugin. However it will give you instantly access to the following features:
Is it fast? Is it crossbrowser? Well I must admit, I didn’t heavily test it, I just wanted to introduce the technique itself. But without processing objects behind transparent areas, I was able to load 1.500 IMG objects with different images and without any performance loss. I think this is more than most need.
If you normally hover an image it also recognized it on the transparent pixels. In our case it’s the same, but we attached a javascript event to the mousemove event. At the background an invisible canvas element with the same size as your image gets created and the same image gets loaded there. While moving on the image, canvas reads the pixel data of the exact same position on the canvas. Like this:
var pixel = context.getImageData(34,21,1,1); return (pixel.data[3]===0) ? true:false;
In this case canvas reads the data of x: 34 y: 21 with only 1 px width and height. The data constists of r,g,b and the alpha value, which is the only important one in our case. 1 is no transparence and 0 invisible. So all we need to know is if the pixel is totally transparent (0). Actually pretty easy isn’t it? If the pixel we’re hovering is NOT transparent, we just attach a class to the object to make it active. This is it. Of course it’s a way harder to make objects behind the transparent areas work.

I will post the script of this tomorrow as there are still some small bugs and performance issues. But for now you can just test it over here:
Thanks for your time, I hope you like this article. More to come!
Best Regards,
Christian Weber
CEO of HydraGames

14:06
Mads
When will you release the script?
14:34
Christian Weber
Actually it should be already but did have some trouble over here. Will try to get it up this weekend.
Thanks for your interest
15:01
nerdfactor
is there a chance that we can get the script this weekend
or is the demo script not very different and we can manage to build the rest on our own?
18:57
WP Themes
Amiable post and this fill someone in on helped me alot in my college assignement. Thank you on your information.
11:19
Christian Weber
Sry for not coming up with the source… again. I’m really stuck in a load of work with a strict deadline at the moment. However the script on the example page remains the same, all I will do is to make it better readable and add detailed comments for every line to make it more understandable, so everybody knows where to change things and how to enhance it easily. If you don’t need something like this, just go on and get the script from the example page.
01:53
Simon
Hey Christian,
Great stuff you’ve done with the object selection script. I have a question: Does it make a difference if I use margin-left and margin-top to define my objects’ positions instead of left and top?
Also in your example it does not select the ruins when moving the mouse over a transparent part of house0 (z-index of 10). Nor does it select the house behind the ruins when over a transparent section of the ruins. Does the script only account for 1 sub layer per object?
I have tried changing the detail option in the config to true but I get a ‘too much recursion’ error or this weird one with firebug:
An invalid or illegal string was specified” code: “12
var dd = cx.getImageData(x,y,1,1);
pixelSelect.js (line 233).
I would love to discuss this with you further as I have a game in development that would like to use this script for its tile selection.
Cheers,
Simon
01:58
Simon
Actually the line in the script is 215 for that error, I had added some extra lines and brackets to improve my understanding of some sections.
16:18
Bosco
Hey Christian!
Thank you for your exceptional work – I too have been fairly inspired by the Aves Engine, and it seems as though projects based on related technologies are few and far between.
I was originally using something akin to a giant table element skewed and rotated into isometric using CSS3 transforms for my map, but I ran into the problem that I could directly insert game objects into cells. Doing so caused the game object to transform, as well, making it appear flat and flush with the terrain. And using the opposite transform to make objects normal again resulted in blurry objects, presumably from whatever resampling the browser uses.
Anyway, thanks again for your work – it has incredibly enlightening
12:20
Christian Weber
If you want to use margin instead of absolute positioning you need to adjust a few lines in the source. Shouldn’t be a big thing and I ran into the same problem with objects in the background. It did work but i ran into big problems there was no object.