{"id":155,"date":"2009-01-15T00:00:08","date_gmt":"2009-01-15T07:00:08","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=528"},"modified":"2009-01-15T00:00:08","modified_gmt":"2009-01-15T07:00:08","slug":"bitmapdata-lock-unlock","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=155","title":{"rendered":"BitmapData lock() &#038; unlock()"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar size:Number = 400;\r\nvar pixelNum:Number = size * size;\r\nvar pixels:Vector.&lt;uint&gt; = new Vector.&lt;uint&gt;();\r\nvar r:uint;\r\nvar g:uint;\r\nvar b:uint;\r\n\r\nvar canvas:BitmapData = new BitmapData(size,size,false,0x000000);\r\naddChild(new Bitmap(canvas));\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\r\n\tcanvas.lock();\r\n\r\n\t\/\/ do any kind of pixel manipulation here:\r\n\t\r\n\tfor (var i:int = 0; i&lt;pixelNum; i++) {\r\n\t\t\/\/ draw a gradient that changes based on\r\n\t    \/\/ mouse position\r\n\t\tr = i % size + mouseX;\r\n\t\tb = i \/ size + mouseY;\r\n\t\tg = (r + b) % mouseX;\r\n\t\tpixels&#x5B;i] = r &lt;&lt; 16 | g &lt;&lt; 8 | b;\r\n\t}\r\n\tcanvas.setVector(canvas.rect, pixels);\r\n\t\/\/\r\n\r\n\tcanvas.unlock();\r\n}\r\n<\/pre>\n<p>Using BitmapData.lock() and BitmapData.unlock() can speed up BitmaData drawing operations significantly. Simply lock your BitmapData before you draw to it, and unlock it when your done.<\/p>\n<p>I often forget to do this until I get to the optimize phase of a project&#8230; but I think I&#8217;d like to get in the habit of doing it all the time&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var size:Number = 400; var pixelNum:Number = size * size; var pixels:Vector.&lt;uint&gt; = new Vector.&lt;uint&gt;(); var r:uint; var g:uint; var b:uint; var canvas:BitmapData = new BitmapData(size,size,false,0x000000); addChild(new Bitmap(canvas)); addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { canvas.lock(); \/\/ do any kind of pixel manipulation here: for (var i:int = 0; i&lt;pixelNum; i++) { \/\/ draw a gradient that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,25],"tags":[3,4],"class_list":["post-155","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-pixel-manipulation","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=155"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/155\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}