Well, this guy from GoogleTechTalks answers this question in this video on javascript-based game engines. http://www.youtube.com/watch?feature=player_detailpage&v=_RRnyChxijA#t=1610s It should start where it is explained.
Edit: Therefore, I will summarize what is said in the video, and give an example code. It was a lot harder than I expected. The trick is to upload the image to the canvas, and then check every pixel if it is transparent. Data is placed in an array with two dimensions. Like alphaData [pixelRow] [pixelCol]. A 0 represents transparency, but 1 does not. When the alphaData array is complete, it is placed in the global var a.
var a; function alphaDataPNG(url, width, height) { var start = false; var context = null; var c = document.createElement("canvas"); if(c.getContext) { context = c.getContext("2d"); if(context.getImageData) { start = true; } } if(start) { var alphaData = []; var loadImage = new Image(); loadImage.style.position = "absolute"; loadImage.style.left = "-10000px"; document.body.appendChild(loadImage); loadImage.onload = function() { c.width = width; c.height = height; c.style.width = width + "px"; c.style.height = height + "px"; context.drawImage(this, 0, 0, width, height); try { try { var imgDat = context.getImageData(0, 0, width, height); } catch (e) { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); var imgDat = context.getImageData(0, 0, width, height); } } catch (e) { throw new Error("unable to access image data: " + e); } var imgData = imgDat.data; for(var i = 0, n = imgData.length; i < n; i += 4) { var row = Math.floor((i / 4) / width); var col = (i/4) - (row * width); if(!alphaData[row]) alphaData[row] = []; alphaData[row][col] = imgData[i+3] == 0 ? 0 : 1; } a=alphaData; }; loadImage.src = url; } else { return false; } }
I had errors when starting local in Firefox, and the try catch statement solved it. Oh, I have to eat ...
Edit 2: So, I finished my lunch, I would like to add some sources that I used, and which may be useful.
https://developer.mozilla.org/En/HTML/Canvas/Pixel_manipulation_with_canvas imageData object information.
http://blog.nihilogic.dk/2008/05/compression-using-canvas-and-png.html Even more information about the imageData object and its use.
http://www.nihilogic.dk/labs/canvascompress/pngdata.js A really useful example of using imageData, the code I provided is very similar to this for the most part.
http://www.youtube.com/watch?v=_RRnyChxijA Information about script game engines in javascript is really very interesting.
http://blog.project-sierra.de/archives/1577 Information on using enablePrivilege in firefox.