For some months I’ve been playing around with HTML 5’s Canvas element because I was curious about it, especially after seeing it run Flash. I hacked at it for a while then realized I had produced an Asteroids game.
Play It!
The source code is up on GitHub!
The Canvas element is super cool. Basically all of the sprites in the game are simple paths defined by an array of points. These points are translated, rotated and scaled based on their state at any given point in the game using the standard Canvas transformations. Plus it gives it a nice retro feel.
Let me know what you think!
Here’s some cool links to things that helped me out with the game:
- Determining Whether A Point Is Inside A Complex Polygon. Firefox does the wrong thing when it comes to isPointInPath(), the main Canvas method I used for collision detection; It doesn’t apply the current transformations before determining if there’s been a collision (and they won’t fix it for some reason). I implemented this method to get around the problem but it works fast enough it could be used on all browsers.
- Metanet Software, the makers of the awesome flash game N, have made a couple of extremely useful tutorials on collision detection. These describe some of the techniques I used in the game, in particular the grid based broad-phase they describe in their second tutorial.
- I used typeface.js ’s convert tool to convert the awesome Vector Battle font into some JSON point data that could be easily used in the canvas. Typeface.js is a neat library for easily embedding arbitrary fonts in the page. On supported browsers it creates small canvas elements and renders the font inside based on the CSS. I borrowed bits from the library to get the font rendering.
Zach
| 13-May-10 at 6:56 am | Permalink
Great demo. I don’t remember from the original game… but should I be able to accelerate past my bullets?
Martin Cron
| 13-May-10 at 7:33 am | Permalink
Excellent work.
I did a version of Asteroids in C#/GDI a few years ago to experiment with some techniques/language features, it may be the most fun I’ve had coding in years.
Labgrab
| 13-May-10 at 8:10 am | Permalink
Nice work on the programming. I hurt some asteroids!
⬡
| 13-May-10 at 8:25 am | Permalink
Not very playable in Firefox when the page keeps scrolling when I press the arrow keys.
bruce
| 13-May-10 at 9:46 am | Permalink
Wow! My favourite game as a teenager in the early 80s!
You don’t list Opera 10.50 as a suggested browser to play it in (which I’ve been using with no problems). WHat are the issues you’ve found running it on Opera?
Ravi
| 13-May-10 at 5:02 pm | Permalink
More awesomness!!!
Sohn
| 13-May-10 at 6:00 pm | Permalink
Cool! You did a great job.
Adam
| 13-May-10 at 7:43 pm | Permalink
Accelerating past bullets is a feature not a bug! =) Very fun!
Joe Lewis
| 13-May-10 at 8:08 pm | Permalink
This looks spectacular on iPad. Now if only there were some way to bring up the soft keyboard so I could play it!
Doug
| 13-May-10 at 9:52 pm | Permalink
Thanks for all the great comments!
Zach: I kind of like it
If you get your ships speed right and then turn 180 degrees the ship and bullet velocities cancel each other out leaving little mines!
Bruce: honestly I hadn’t tested it in Opera! I did most of the development using Safari. Thanks for the tip though, I’ve updated the browser list
⬡: The down arrow key wasn’t mapped to anything in the game so it wasn’t getting captured. I’ve now disabled it. Probably should map it to hyperspace.
Joe Lewis: I really want to get it working on the iPad! I imagine I can have it expand to fill the screen, just need to set up controls and get the framerate up a bit.
bruce
| 13-May-10 at 10:43 pm | Permalink
Thanks Doug!
BTW, yes – down arrow could be mapped to hyperspace,but probably so should space bar; after all, it’s the panic button so you need a big hit target for a panic button.
bruce
| 13-May-10 at 10:48 pm | Permalink
WHat am I talking about? Space bar is “fire”. Maybe “enter” could be mapped to hypersapce as well as down arrow
bill creswell
| 14-May-10 at 1:16 am | Permalink
Makes me want to get a trackball!
ralu rm
| 14-May-10 at 2:17 am | Permalink
great job.. move on with great images and sound effects .. as quake2 by google… http://code.google.com/p/quake2-gwt-port/
great work!!!
Ali Macdonald
| 14-May-10 at 2:21 am | Permalink
Fantastic stuff! Very playable in Chrome on W7, I’m guessing sound effects are a bridge too far? I have heard there is an issue with delay given no event trigger/listening – is that right?
Sean
| 14-May-10 at 2:24 am | Permalink
Freaking awesome. I spent a lot of time in the bowling alley behind my school playing this game.
skintrade
| 14-May-10 at 2:32 am | Permalink
Shame it’s not usable on my Android phone
Leo
| 14-May-10 at 3:03 am | Permalink
Great job, very nice demo!
devolute
| 14-May-10 at 5:08 am | Permalink
Very impressive. I’m on an incredibly old PC with Chrome here and it runs pretty well.
Phil
| 14-May-10 at 5:57 am | Permalink
Shame I cannot play on iPhone as cannot activate keyboard
ted
| 14-May-10 at 6:12 am | Permalink
Chrome works too.
vivek dani
| 14-May-10 at 6:54 am | Permalink
Amazing stuff!!!
These kind of things push limits of software & tech.
Keehun
| 14-May-10 at 7:19 am | Permalink
Wow! This is really fluid, amazing, and takes only tiny CPU. Great!
What kind of scores can people get? I’m getting low 4000s
JR Fent
| 14-May-10 at 7:37 am | Permalink
AWESOME! Shows your mad skills Doug. Great job.
James
| 14-May-10 at 9:01 am | Permalink
awesome game, It was to hard for me to move around and fire so I just liked to stay in the center.
Feature Request
Weapon Upgrades
pinboard May 14, 2010 — arghh.net | 14-May-10 at 9:51 am | Permalink
[...] HTML 5 Asteroids | Doug McInnes Cool. Asteroids game in HTML 5. Also some great HTML 5 education here. via @mikeloukides [...]
slyy
| 14-May-10 at 11:19 am | Permalink
cool stuff!
Phil Brammer
| 14-May-10 at 11:59 am | Permalink
Beautiful. Did it take ages to program?
Andy
| 14-May-10 at 3:04 pm | Permalink
Awesome. I love it!
tony
| 14-May-10 at 6:52 pm | Permalink
great stuff…
Doug
| 15-May-10 at 11:15 am | Permalink
Ali Macdonald: I had implemented sound and had it working fairly well on Safari but Chrome and Firefox gave me trouble so it’s been disabled. I’ll eventually get back to it
Phil Brammer: I started this at the end of January and worked on it off and on since then. If you look at the project history in git here’s a HUGE gap in March when I wasn’t working on it because work got crazy hectic around that time so I would just come home and sleep
kevin cassidy
| 16-May-10 at 11:46 pm | Permalink
can we agree that soon we will no longer be dependent on flash?
Kevin Roast
| 17-May-10 at 2:28 am | Permalink
Nice work, very close to the original! Obviously I *cannot* resist a shameless plug of my canvas asteroids game: http://www.kevs3d.co.uk/dev/asteroids
Un juego totalmente hecho en HTML5 « Luigi Keynes | 17-May-10 at 7:15 pm | Permalink
[...] Para mas detalles sobre como se hizo el juego sigan este link. [...]
Doug
| 17-May-10 at 8:25 pm | Permalink
Kevin Roast: I saw your game a couple of weeks ago, awesome work!
Michael Wengler
| 18-May-10 at 10:14 am | Permalink
Incredible! Feels EXACTLY right!
-An old guy.
HTML 5 Asteroids | Doug McInnes | portail de news (nouveautés) | 19-May-10 at 8:33 am | Permalink
[...] la lecture ici: HTML 5 Asteroids | Doug McInnes Tags: asteroids, been-playing, element-because, html, seeing-it-run, some-months | [...]
Dave
| 19-May-10 at 9:46 am | Permalink
Awesome. I take it sound has yet to be implemented?
Lucas Abranches
| 19-May-10 at 10:01 am | Permalink
very nice..
Cutter
| 19-May-10 at 12:57 pm | Permalink
A wonderful recreation of a nice coin-op of the golden age: could you add some sound, when and if possible? And, you could add the hyperspace effect on the ship, and add the small UFO also, if you want your recreation more faithful. I suggest you, if canvas allows it, to program another “pumped” asteroid along with this one, with colored graphics, sounds and some new design. in the main menu you could put starts for the vanilla one and the pumped one! Thanks anyway for your effort!
Josh
| 19-May-10 at 3:11 pm | Permalink
I just spent 30 minutes discovering I’m as bad at this game as I ever was. Space is a crazy place. Nice work.
Wayne
| 19-May-10 at 5:34 pm | Permalink
It’s gorgeous, and great work, but would be /so/ much better in white on black rather than black on white.
rjg
| 19-May-10 at 10:08 pm | Permalink
Wow, top notch excellent and fun! Really impressive what you can do with canvas, really amazing.
Would be nice if you had a way to do custom keyboard, or just a few choices there. my roommate had this machine in our place in college and the keys would match up just like the arcade using: z-x (rotate) and /-right shift (thrust – fire). You could then use spacebar for hyperspace. Also, using the shift key is killer for laying out a tight burst of shots.
F-n awesome I love this game!
Doug
| 19-May-10 at 10:25 pm | Permalink
Dave: I got some sounds effects working fairly well in Safari but it was inconsistent between browsers so I disabled it for now.
Cutter: I’m currently working on iPad controls. The source is on GitHub so feel free to hack at it!
Wayne: If you’re on a Mac hit Command-Alt-Ctrl-8
Actually I prefer it white on black but should be straightforward to add an option!
rjg: Good ideas! Hyperspace is definitely slated for the future.
Thanks everyone for the kudos!
FreeRange
| 23-May-10 at 5:40 pm | Permalink
Great Job! We all are looking so much forward to the death of Flash and all these wonderful efforts help! Free The Internet!!!!!!
Tanner
| 23-May-10 at 11:31 pm | Permalink
the original game only let you have 4 bullets onscreen at a time
Chris Martin
| 24-May-10 at 12:44 pm | Permalink
I noticed a couple of errors in firefox 3.6.3. If you hit the space bar rapidly to fire, it sends you to the bottom of the page. Seems like the game is loosing focus? I tried clicking inside the window but it still did the same.The other is after you thrust, you cant slow your self down. I don’t know if that’s an error or not. Hope this helps if those are errors, if not, april fools.
Loweded Wookie
| 24-May-10 at 11:20 pm | Permalink
This is awesome.
Is there anyway to get the code because I’d love to see how this is done and play around with the code.
Loweded Wookie
| 24-May-10 at 11:21 pm | Permalink
Scratch that last comment. The link I followed to get here took me directly to the game but once I posted the comment it took me to this page which had the download link.
Still awesome though. Thanks for the effort.
Michael Wengler
| 25-May-10 at 9:29 am | Permalink
It seems as though the score above 10000 does not indicate correctly.
Khai
| 26-May-10 at 10:17 am | Permalink
Well done!!
Tin
| 27-May-10 at 10:01 am | Permalink
Amazing! That’s great – just needs some sound… any idea how Google did that with their Pacman?
Doug
| 27-May-10 at 10:50 pm | Permalink
Chris Martin: that’s odd it capture all the space bar presses. You can hold it down to fire rapidly.
Tin: From what I understand they used Flash for the sound
Bobby Bones
| 29-May-10 at 2:38 pm | Permalink
Have you looked at your site in Chrome. doh!
Emanuele Rogledi
| 13-Jul-10 at 11:16 am | Permalink
Excellent implementation of Asteroids