Making Sprite-based Games with Canvas
jlongster.com
jlongster.com
These tricks matter because redrawing the entire screen every frame is usually an expensive operation; this is not noticable on small examples but it can become an issue on larger, more complex games.
Great article otherwise.
It may be worth following up this article with another one that includes techniques like this.
Thanks
Remember that each canvas element is still part of the DOM, so you can put one on top of another just like you could with ordinary HTML.
>image-rendering: optimizeSpeed;
In some cases, you will want to scale the images drawn to the canvas, but not the canvas itself. This causes a lot of headache because of image smoothing and is not resolved by the above CSS. The answer is to set the image smoothing property of the canvas context to false. For example:
ctx.mozImageSmoothingEnabled = false;
ctx.webkitImageSmoothingEnabled = false;
Worked great on Chrome and Firefox. For some reason, though, this does not appear to work in Safari. I'm still amazed that something so simple is so hard to accomplish across browsers (after all you're asking to turn off extra, slower functionality). A good article on the intricacies: http://phoboslab.org/log/2012/09/drawing-pixels-is-hard
I plan on tweaking this article with improvements like that.
There was a Google contraption editing game(a Google IO demo?) that suffered from this approach quite badly. It was a set-up-and-go game which should perform deterministically, but was influenced by variations in frame-rate.
The easiest approach to fixing this is simply quantised time.
var now = Date.now();
while(gameAge < now) {
update();
gameAge+=stepDuration;
}
render();
//with usual caveats of resyncing if now-gameAge is too large
The much harder (but possibly ideal) approach is to not even have such a time step at all and move in variable increments calculated at the point of interaction between entities. An example of this would be a snooker simulation which moves in steps when the balls make contact. Balls slowing, rolling and curving become calculus problems. Produces a very nice result but your brain explodes. bullets.push({ pos: [x, y],
dir: 'forward',
sprite: new
Sprite('img/sprites.png', [0, 39], [18, 8]) });
and var bullet = bullets[i];
switch(bullet.dir) {
case 'up': bullet.pos[1] -= bulletSpeed * dt; break;
case 'down': bullet.pos[1] += bulletSpeed * dt;
break;
default:
bullet.pos[0] += bulletSpeed * dt;
}
You could consider simply adding a velocity to the bullet at spawn time like so: bullets.push({ pos: [x, y],
dir: [0,10],
sprite: new
Sprite('img/sprites.png', [0, 39], [18, 8]) });
and then in the update code replace the switch stuff with: bullet.pos[0] += bullet.dir[0] * dt;
bullet.pos[1] += bullet.dir[1] * dt;
That should eliminate branching and make the interpreter much happier.Additionally, by making the speed/velocity a member of the bullet instance, you can do things like have bullets that slow down or speed up if they have a think method or something, or that do homing (for this, look up dot-products and cross-products for steering).
http://en.wikipedia.org/wiki/HTML5_Audio
edit: I'd also guess because audio is hard and that it is probably easier to make graphics and gaming logic than making sounds/music properly.
From the first paragraph:
"Fieldrunners is an award-winning tower-defense style game that was originally released for iPhone in 2008. Since then it has been ported to many other platforms. One of the most recent platforms was the Chrome browser in October 2011. One of the challenges of porting Fieldrunners to an HTML5 platform was how to play sound."
I plan on writing a followup to include sound, though, or possible extending this article.
What the other comments say is true, also. It's not extremely straightforward. The state of APIs on the web is very mixed, but I think in a year or so the Web Audio API will in most places.
However making music is an art not all programmers have mastered, so most would likely skip that step.