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

Things we learned about Canvas

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Things we learned about Canvas

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

David Strauß

October 21, 2011

Transcript

40 slides
  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. ⟶

  6. ⟶

  7. function drawStatics() { if (renderStaticsNew) { // clear whole canvas

    staticCanvas.width = staticCanvas.width; // drawings ... renderStaticsNew = false; } },
  8. 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 ... }
  9. var pattern; var image = new Image(); image.src = "path.png";

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

Other Decks in Programming