Aug 25

From Hakim El Hattab (who has some very nifty HTML5 experiments up) comes some nice tips on using the Canvas tag:

Cross browser implementation

There are no real discrepancies between the canvas outputs of different browsers so long as the JavaScript code is written correctly (if not, browsers tend to try and fix things for you, often resulting in varying results).

Performance

When working with animation on canvas, performance can be a challenge since bitmap operations are very processing expensive, especially at high resolutions. One important optimization rule to follow is to reuse as many pixels as possible between frames. What I mean by that is the fewer pixels that need to be processed each frame, the faster your program will run. A good example of this is when erasing pixels with the clearRect(x,y,w,h)method, it is very beneficial to clear and redraw only the pixels that have changed and not, for instance, a full screen 1920×1280 sized canvas. Unlike the Flash Player’s redraw regions, this management of “dirty rectangles” needs to be done manually for canvas.

State stack & transformation

The canvas can be manipulated via transformations such as rotation and scaling, resulting in a change to the canvas co-ordinate system. This is where it’s important to know about the state stack for which two methods are available: “save” (pushes the current state to the stack) and “restore” (reverts to the previous state). This enables you to apply transformation to a drawing and then restore back to the previous state to make sure the next shape is not affected by any earlier transformation. The states also include properties such as the fill and stroke colors.

Compositing

A very powerful tool at hand when working with canvas is compositing modes which, amongst other things, allow for masking and layering. As an example, you can check out Bakemono, where composite modes are used to mask the eye and mouth. There’s a wide array of available composite modes and they are all set through the canvas context’s “globalCompositeOperation” property.

Anti-aliasing

To allow for sub-pixel drawings, all browser implementations of canvas employ anti-aliasing (although this does not seem to be a requirement in the HTML5 spec). Anti-aliasing can be important to keep in mind if you want to draw crisp lines and notice the result looks blurred. To work around this you will need to either round to integer values or offset by half a pixel depending on if you’re drawing fills or strokes.

Clearing the canvas

To clear the entire canvas of any existing pixels you would normally use the clearRect(x,y,w,h) function but there is another option available. Whenever the width/height of the canvas are set, even if they are set to the value they already have, the canvas is reset. This is good to know when working with a dynamically sized canvas as you will notice drawings disappearing.

[via Mr. Doob]

Cross browser implementation

There are no real discrepancies between the canvas outputs of different browsers so long as the JavaScript code is written correctly (if not, browsers tend to try and fix things for you, often resulting in varying results).

Performance

When working with animation on canvas, performance can be a challenge since bitmap operations are very processing expensive, especially at high resolutions. One important optimization rule to follow is to reuse as many pixels as possible between frames. What I mean by that is the fewer pixels that need to be processed each frame, the faster your program will run. A good example of this is when erasing pixels with the clearRect(x,y,w,h)method, it is very beneficial to clear and redraw only the pixels that have changed and not, for instance, a full screen 1920×1280 sized canvas. Unlike the Flash Player’s redraw regions, this management of “dirty rectangles” needs to be done manually for canvas.

State stack & transformation

The canvas can be manipulated via transformations such as rotation and scaling, resulting in a change to the canvas co-ordinate system. This is where it’s important to know about the state stack for which two methods are available: “save” (pushes the current state to the stack) and “restore” (reverts to the previous state). This enables you to apply transformation to a drawing and then restore back to the previous state to make sure the next shape is not affected by any earlier transformation. The states also include properties such as the fill and stroke colors.

Compositing

A very powerful tool at hand when working with canvas is compositing modes which, amongst other things, allow for masking and layering. As an example, you can check out Bakemono, where composite modes are used to mask the eye and mouth. There’s a wide array of available composite modes and they are all set through the canvas context’s “globalCompositeOperation” property.

Anti-aliasing

To allow for sub-pixel drawings, all browser implementations of canvas employ anti-aliasing (although this does not seem to be a requirement in the HTML5 spec). Anti-aliasing can be important to keep in mind if you want to draw crisp lines and notice the result looks blurred. To work around this you will need to either round to integer values or offset by half a pixel depending on if you’re drawing fills or strokes.

Clearing the canvas

To clear the entire canvas of any existing pixels you would normally use the clearRect(x,y,w,h) function but there is another option available. Whenever the width/height of the canvas are set, even if they are set to the value they already have, the canvas is reset. This is good to know when working with a dynamically sized canvas as you will notice drawings disappearing.

Continue reading »

Tagged with:
Jun 15

Do you remember being really impressed by the initial Aves game engine that uses Canvas and HTML5 technology to deliver a compelling social gaming platform on Web technology? Well, now the Dextrose crew are back in action having released their second prototype of their upcoming browser-game middleware at E3 2010 in Los Angeles:

The new prototype is shown live to interested licensees and magazine editors on the exhibition floor. Additionally, a video with six minutes of in-game footage has been released on YouTube. Compared to the medieval-styled prototype from some weeks ago, Dextrose introduces even more innovation and performance boosts not yet seen in the browsergames industry.

  • unbelievable performance in the browser: Aves Engine renders hundreds of players in realtime in one viewport
  • introduces innovative Action Surface-Technology which allows the integration of any Web 2.0 or custom widget into your game
  • high-scalable viewports to any screen resolution size with nearly no performance impact
  • very large optimizations towards the mobile market. Technical stunning games now possible on WebKit-based Smartphones and iPad
  • a refurbished and extended realtime world editor
  • sprite animation classes

I had a chance to play with the prototype, and man was it nice. The iPad is fairly underpowered when it comes to intensive tasks like this, and it works very well indeed. The team had to spend a lot of time optimizing the engine for that form factor, and they are now in good steed for other mobile devices.

Moving around the world with iPad and laptops alike showed how well the UI keeps up with goings-on. This is because good old Mr. NodeJS is behind the scenes scaling up nicely for the crew. The multiplayer engine is smart enough to gracefully failback from WebSockets to long polling to standard boring polling that the iPad requires. All seamless to the programmer. The developer just gets to riff on top of the simple to hack on game engine.

Since the engine is built on the Web, the team could create Web surfaces with ease. This enables you to create mashups of content to embed directly into the game. Walk up to a laptop monitor in the game and see that it has a real browser window embedded and play with it. The world is truly your oyster with this engine.

If you are interested in building up a large player world, and want to get across a myriad of devices thanks to the Web platform, look this up to give it a roll.

Great work guys!

Continue reading »

Tagged with:
preload preload preload