{"id":806,"date":"2014-02-03T16:46:22","date_gmt":"2014-02-03T16:46:22","guid":{"rendered":"http:\/\/local.sandjam.co.uk\/?p=806"},"modified":"2018-08-03T13:54:27","modified_gmt":"2018-08-03T12:54:27","slug":"html5-canvas-animations-top-tips","status":"publish","type":"post","link":"https:\/\/sandjam.co.uk\/sandjam\/2014\/02\/html5-canvas-animations-top-tips\/","title":{"rendered":"HTML5 Canvas Animations &#8211; Top Tips"},"content":{"rendered":"<p>I recently worked on a project creating an animated music visualisation using HTML5 canvas elements to create interactive animations.<\/p>\n<p>Since the canvas element is quite new, I thought it might be useful to compile a list of tips I&#8217;ve build up while working with it.<\/p>\n<h2>In-memory canvas caching<\/h2>\n<p>The biggest consideration with canvas animations in optimising the efficiency of the code so that on slower machines and devices the animation doesn&#8217;t appear laggy.<\/p>\n<p>The browser will attempt to re-draw the entire canvas every time the context is modified. So if you have complex paths to draw, or several graphics to render for a single frame it can be useful to create an in-memory canvas which you draw your frame on to. Then use a single drawImage() call to draw the in-memory canvas on to the visible canvas.<\/p>\n<h2>Layering up canvas elements<\/h2>\n<p>If you have used Photoshop or Flash in the past you may be familiar with the idea of layering up graphics to compose a scene. Breaking up your scene into layers of content can help save processing time by only updating layers which have changed, or updating background layers less frequently than foreground layers.<\/p>\n<p>This layering can be accomplished by absolutely positioning several canvas elements on top of each other, using their z-index property to make sure the layers sit in the right order.<\/p>\n<h2>CSS3 transitions<\/h2>\n<p>If you have a large graphic that you want to animate, like a background image, it may be worth using css3 transforms to move it rather than using a drawImage() command on a canvas element.<\/p>\n<p>Browsers are able to animate CSS3 transforms using keyframe animations a lot more efficiently than canvas re-draws are able to.<\/p>\n<h2>requestAnimationFrame<\/h2>\n<p>When animating frame-by-frame animations, one option is to use a setTimeout() function to re-draw the canvas element at a set interval. This will work, but there is a more efficient way.<\/p>\n<p>requestAnimationFrame tells the browser to draw the next frame but only if it&#8217;s not still busy drawing the previous frame. This ensures that if your animation starts running slowly your frame re-draws don&#8217;t get backed up.<\/p>\n<p>It also has the added bonus that if the window isn&#8217;t visible, the script won&#8217;t bother rendering the frame &#8211; so you&#8217;ll save processing power (and battery life on mobile devices).<\/p>\n<h2>Know your limits<\/h2>\n<p>The scope of what is possible with Canvas is almost endless, however the current device capabilities certainly aren&#8217;t.<\/p>\n<p>While you&#8217;re developing make sure you keep testing on a multitude of browsers and mobile devices to make sure your code isn&#8217;t overtaking the processing power available.<\/p>\n<p>Don&#8217;t forget that 3d animations aren&#8217;t supported by many browsers &#8211; crucially iOS.<\/p>\n<p>Android&#8217;s native browser is notoriously slow at processing canvas animation because it only uses software acceleration, and doesn&#8217;t make the processing power of the full device available to canvas.<\/p>\n<h2>Keep this to hand<\/h2>\n<p>It&#8217;s not cheating to use the <a href=\"https:\/\/simon.html5.org\/dump\/html5-canvas-cheat-sheet.html\">HTML 5 Canvas Cheatsheet<\/a>, so keep this handy.<\/p>\n<h2>Now, some cool examples<\/h2>\n<p>It wouldn&#8217;t be an HTML5 canvas starter post without a list of my favourite demos, so here goes:<\/p>\n<ul>\n<li><a href=\"http:\/\/9elements.com\/io\/projects\/html5\/canvas\/\">9Elements audio reacting particle system<\/a><\/li>\n<li><a href=\"http:\/\/hakim.se\/experiments\/html5\/blob\/03\/\">Interactive blobs<\/a><\/li>\n<li><a href=\"http:\/\/kennethjorgensen.com\/canvas\/tree.html\">Fractal trees<\/a><\/li>\n<li><a href=\"http:\/\/www.chromeexperiments.com\/arcadefire\/\">Arcade Fire &#8211; The Wilderness Downtown<\/a><\/li>\n<li><a href=\"http:\/\/www.professorcloud.com\/mainsite\/canvas-nebula.htm\">Nebula Cloud<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently worked on a project creating an animated music visualisation using HTML5 canvas elements to create interactive animations. Since the canvas element is quite new, I thought it might be useful to compile a list of tips I&#8217;ve build up while working with it. In-memory canvas caching The biggest consideration with canvas animations in [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[67],"tags":[76,35,87,14],"class_list":["post-806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-html5","tag-canvas","tag-css","tag-html5","tag-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/posts\/806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/comments?post=806"}],"version-history":[{"count":4,"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/posts\/806\/revisions"}],"predecessor-version":[{"id":1039,"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/posts\/806\/revisions\/1039"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/media\/807"}],"wp:attachment":[{"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/media?parent=806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/categories?post=806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sandjam.co.uk\/sandjam\/wp-json\/wp\/v2\/tags?post=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}