Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Things we learned about Canvas

Things we learned about Canvas

Tips and tricks we learned about canvas while programming Marble Run. Marble Run won Mozillas Game On 2010 challenge.

Avatar for David Strauß

David Strauß

October 21, 2011
Tweet

Other Decks in Programming

Transcript

  1. API

  2. 0 0,5 1 1,5 2 2,5 3 3,5 4 0,5

    0 CODE SCREEN 1,5 1 2,5 2 3,5 3 4 
  3. 0 0,5 1 1,5 2 2,5 3 3,5 4 0,5

    0 1,5 1 2,5 2 3,5 3 4  CODE SCREEN
  4. // requestAnim shim layer by Paul Irish window.requestAnimFrame = (function(){

    return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(/* function */ callback, /* DOMElement */ element){ window.setTimeout(callback, 25); }; })();
  5. function drawStatics() { if (renderStaticsNew) { // clear whole canvas

    staticCanvas.width = staticCanvas.width; // drawings ... renderStaticsNew = false; } },
  6. function drawDynamics() { // clear Dirty Rectangles for (var i

    = 0; i < dirtyRects.length; i++) { dynamicContext.clearRect( dirtyRects[i].x, dirtyRects[i].y, dirtyRects[i].width, dirtyRects[i].height ); } // drawings ... }
  7. var pattern; var image = new Image(); image.src = "path.png";

    image.onload = function() { pattern = context.createPattern(this, "repeat"); } … context.fillStyle = pattern;