A HTML5 game that crashes every browser - browser makers, please do better
Hi HN,
Not sure where else to best reach the browser people, but I've made a little space shooter HTML5 game using canvas 2D. Here's the URL:
http://www.scirra.com/labs/spaceblaster-webapp/
Arrow keys to move, hold space to shoot, press shift to fire rockets.
This game crashes every single browser on Windows 7 64-bit. I've had it crash on Chrome 14.0.835.202, Firefox 7.0.1, Internet Explorer 9.0.8112.16421, Opera 11.51 and even Safari 5.1.1 for Windows. Sometimes it crashes within 30 sec, but it almost always crashes before completing the game.
Please either tell me what I'm doing wrong, or fix your browsers. Everyone loves HTML5, right? So please make sure it works properly, because current browsers just don't seem to be good enough.
15 comments
[ 4.6 ms ] story [ 57.1 ms ] threadIf your code crashes every browser, your code is shit. Yes the browsers could do better, but you shouldn't write shit code that crashes them.
Have you tried to put them into sprite sheets?
I've recently been reading into making use of sprite sheets with Canvas apps, and the drawImage method makes using sprite sheets a breeze. You simply need to keep track of cellsize, number of cells, and your current cell.
Well, if you're using dom-based animation, spritesheets also work better for animations since it's much faster to adjust background-image-position in a div, compared to just replacing the background-image.
I wasn't even aware of using background-image-position as a way to do this... I'll stick that one in my cap, thanks!
I'd say the main benefit of spritesheets as i've seen it would be what you've already commented on: Fewer GETs against the server (when you're thinking in terms of scale, for a large project with lots of assets, i'd say this becomes important, but of course that's a fairly subjective metric), as well as organization of files.
You could go with naming conventions... but then for a 45 frame animation, you have 45 distinct files all named something like "SPECIFIC_ANIMATION_FRAME_X", as opposed to using a dynamic approach, utilizing the cell-size of the frames and the width and height of the image overall, to calculate the frames on the fly; To me, that is far more maintainable than a huge pile of aptly named images.
You could make the claim that calculating out the frame each time is wasted cpu cycles that could be spent elsewhere, but I feel like it's the right approach.
But that could just be my opinion. I would love for someone to weigh in with actual metrics on whether or not using a dynamic approach is actually faster or slower in a meaningful way than a brute-force style naming convention animation system.
Anyways, you're right, with lots of assets it probably is a much better way to organize sprites into spritesheet. I just never did anything on that scale. 45 animation frames? Sheesh, I'm happy if I can get 5 or 6. :)
I genuinely have no idea how I can improve the quality of the code in this.
I won't jump to conclusions, but this attitude is kind of indicative of the Dunning-Kruger effect (http://en.wikipedia.org/wiki/Dunning-Kruger_effect) being at play, especially given that you seem to be blaming browser makers for your game crashing.
Even the very best programmers can think of ways to improve the quality of their code. In fact, they're more likely to find problems in their own code - that's part of why they're the best.
In any case, assuming you aren't suffering from the Dunning–Kruger effect, can you mention what attempts you've made to narrow down the problem?
Have you tried disabling parts of the game (like rendering for example) to see if the browser still crashes? That would be a simple, albeit tedious, way of narrowing down where the problem is. Come up with a minimal crash case.
If you find a legitimate problem across all 4 browsers, then you can use that minimal case to demonstrate the problem and file a bug report. Or, more likely, you'll figure out what's wrong in your code this way and fix it.
Your code can only be as good as the platform you're working with. You can't code features beyond what are supported, and if you're trying to do too much, you need to scale back. If you based a games design on the idea that every computer was quad core with a huge graphics card, it would be pretty amazing, but no-one except a select few could play it.
Short of it is, you're code doesn't work, whatever is wrong with it, it doesn't work. And you can't expect browsers to change to suit you, if you really don't know how to improve it, scrap it and start again but at the moment you can't blame the browsers for not being able to support your, probably brilliant, game.
nookie here mentions memory use, so why don't you use the profiler in Chrome's web developer tools to find the memory leak (assuming there is a memory leak, as nookie implies). I believe they have some tools to help you in that regard.
One thing I found on StackOverflow (I cannot find the article again, I apologize... I should've bookmarked it) was that if you're drawing lines and not using the "beginPath" call, you'll get a memory leak. No idea why this would be.
I realized I had exactly that problem in my sample canvas app, and making the change actually reduced my memory overhead. In Opera and Chrome, anyways. Firefox still had wild memory fluctuations, but that didn't surprise me.
Moral of the story: There are probably several things you could tweak to reduce your memory usage, as it does seem that a lot of canvas code out there is memory-abusive.
Mac Lion 10.7.2 + Chrome 14.0.835.202 Regular Build