Questions?
- How do you do localization in HTML5?
- Can you restrict HTML5 games geographically?
- Are you going to cover multiplayer games?
- How do you save your game?
- ... with loss of connection?
- How do you feel about the move from Mozilla
to support W3C EME?
Ask us more! ntu.questions@gmail.com
Performance
- What makes a good game Good?
Graphics
- Resolution & Framerate
- Games should aim for 60 fps
- Higher framerate = smoother, more fun
- Which would you rather play?
Low Framerate
High Framerate
Moral of the Story
- Don't do a lot of work in the tick
The Challenge
- OpenGL, DirectX
- HTML5
- but only indirect access to GPU
- great for web development
- really hard to optimize deeply
Canvas, why u no fast?
- Canvas Draws do this:
- javascript -> c++ -> OpenGL -> javascript
- very hard to optimize this
- OpenGL, many ways to optimize
- batch up draw calls before flushing buffer
- not tied to specific implementation
- ...but, there are still things we can do
Layers & Dirty Rectangles
- Don't draw entire seen every tick
- Use multiple canvases
- Only clear certain areas of screen
- known as "dirty rectangles"
Layers
#background {
z-index: 1;
}
#characters {
z-index: 2
}
#foreground {
z-index: 3
}
Layers
Pipe.prototype.draw = function(pipeCtx) {
this.ctx.clearRect(this.lastX, this.y, this.width, this.height);
this.ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
Scaling === BAD
Warning: NEATO canvas feature
- Canvas has nice auto-scaling feature
- ctx.drawImage: sw, sh, dw, dh
- DON'T USE IT
- Draw images at their native res
Rounding === GOOD
Warning: NEATO canvas feature
- drawImage with floats causes blending
- browser interpolates between pixels
- DON'T USE IT
- Truncate pixels values before draw
Check it out
Why learn canvas?
- Performance problems, weird gotchas
- better is WebGL, CSS transforms
- WebGL is really hard to learn, implement
- CSS tranforms has it's own weird gotchas
- Canvas is easy
- make POC fast, optimize later