{"id":117,"date":"2012-04-01T12:01:24","date_gmt":"2012-04-01T06:31:24","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=117"},"modified":"2012-04-01T12:01:24","modified_gmt":"2012-04-01T06:31:24","slug":"improving-animation-performance-in-html5-canvas","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/improving-animation-performance-in-html5-canvas\/","title":{"rendered":"Improving Animation Performance in HTML5 Canvas"},"content":{"rendered":"<p>I have been working on an application that performs some animation in HTML5 Canvas. The animation involves drawing of stokes,images and moving images. Initially I took the easiest path for coding animation, which is clearing canvas every time and drawing new positions of objects. When tested this application on my laptop, in Chrome, everything worked fine and performance was satisfactory.<\/p>\n<p>However when I ran the same application on my Xoom tablet, animation was extremely slow. Obviously clearing canvas and redrawing everything again and again was not a good idea. I did know that this approach was bad for animation, but since it worked fine on my laptop, I did not bother to change it initially. However I was surprised by how bad it performed on Android. I saw many complaints about performance of Canvas on Android in many forum posts and blogs. Apparently, as per many posts, performance of Canvas is much better on iOS than Android, but I can&#8217;t confirm this.<br \/>\n<!--more--><br \/>\nMost of the recommendations online for improving performance fell in two categories 1. Redraw only what has changed 2. Use multiple layers of canvas to cache objects that do not change during animation. The first option did not suit my application, because I had too many objects on the Canvas and I thought computing which objects need to be redrawn would have been only little better or as bad as redrawing every frame.<\/p>\n<p>Fortunately my application does not have too many images moving at the same time. When an image changes position, I need to redraw what was hidden by the image in its previous location and draw the image at its new location. So I thought I would draw all objects, except the one that is about to be moved on a backup Canvas, and every time image moves, convert main canvas to an image and then draw this image every time image is moved before drawing moving image at a new location.One side effect of this approach is that image that is moving appear in the front even if there were other objects on top of it. But that is acceptable in my application.<\/p>\n<p>So I started looking for Canvas API for converting it to an image. Initially I found two APIs for this &#8211;<\/p>\n<ol>\n<li>Canvas.toDataURL(imageType) : This function creates image (of given image type &#8211; this argument is optional) data as base64 encoded. This is useful if you want to save\/send data in text format. But the size of data becomes too big. Also this function is not implemented in Android 2.2\/2.3. It is implemented in Android 4.0 (ICS).<\/li>\n<li><a href=\"http:\/\/dev.w3.org\/html5\/2dcontext\/#pixel-manipulation\">Canvas.getImageData and Canvas.putImageData<\/a> : You can specify top,left coordinates of canvas from where to get image data and width and height of image from that location. This returns ImageData object. You can use it in putImageData function to write the data back on the Canvas.<\/li>\n<\/ol>\n<p>I did not want to use toDataURL because of base64 format (too big). get\/put imageData looked promising. However for some reason it did not work as expected when used to get\/put only part of the image. I still need to debug this issue.<\/p>\n<p>Then I read <a href=\"http:\/\/dev.w3.org\/html5\/2dcontext\/#drawing-images-to-the-canvas\">this description<\/a> of Canvas.drawImage() &#8211; you can use this function on &#8220;HTMLImageElement, an <strong>HTMLCanvasElement<\/strong>, or an HTMLVideoElement&#8221;. So you can use one Canvas as the first argument to this function and copy its content to another Canvas. This was something I was looking for. So I changed my animation code as follows &#8211;<\/p>\n<ol>\n<li>Create a backup Canvas element in the document.<\/li>\n<li>Just before an image is to be moved on the main Canvas, draw everything from the main canvas to the backup canvas, except the image to be moved.This content (on the backup Canvas) is not going to change during animation.<\/li>\n<li>When the image is moved, copy static content from the backup Canvas to the main Canvas at previous image location. For example, if the image that is moving, has width w1 and height h1 and it is being moved from x1, y1 to x2, y2 then I copy image from the backup Canvas from x1, y1, width w1 and height h1to the main Canvas at x1,y1. This will expose previously hidden object under the image that is being moved. Then I draw the image on the main Canvas at x2,y2<\/li>\n<\/ol>\n<p>After above changes the performance is much better on Android. It is still not as good as I would have liked, but there is significant performance improvement.<\/p>\n<p>-Ram Kulkarni<\/p>\n<p><strong>Update : <\/strong><a title=\"Improving Animation Performance in HTML5 Canvas \u2013 Part II\" href=\"http:\/\/ramkulkarni.com\/blog\/improving-animation-performance-in-html5-canvas-part-ii\/\" target=\"_blank\">Improving Animation Performance in HTML5 Canvas &#8211; Part II<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been working on an application that performs some animation in HTML5 Canvas. The animation involves drawing of stokes,images and moving images. Initially I took the easiest path for coding animation, which is clearing canvas every time and drawing new positions of objects. When tested this application on my laptop, in Chrome, everything worked &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/improving-animation-performance-in-html5-canvas\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Improving Animation Performance in HTML5 Canvas&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[3,10,1],"tags":[8,9,11],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-1T","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/117"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":0,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}