{"id":158,"date":"2009-01-16T00:00:30","date_gmt":"2009-01-16T07:00:30","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=539"},"modified":"2009-01-16T00:00:30","modified_gmt":"2009-01-16T07:00:30","slug":"image-airbrush","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=158","title":{"rendered":"Image Airbrush"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)]\r\n\r\n\/\/ draw an ugly gradient\r\nvar size:int = 500;\r\nvar pixNum:int = size * size;\r\nvar gradient:BitmapData = new BitmapData(size, size, true, 0xFF000000);\r\n\r\ngradient.lock();\r\nvar xp:int, yp:int;\r\nfor (var i:int = 0; i&lt;pixNum; i++){\r\n\txp = i % size;\r\n\typ = i \/ size;\r\n\tgradient.setPixel(xp, yp, (yp \/= 2) &lt;&lt; 16 | (255 - yp) &lt;&lt; 8 | (xp \/ 2) );\r\n}\r\ngradient.unlock();\r\n\r\n\/\/ draw an alphaChannel (radial gradient)\r\nvar radius:Number = 50;\r\nvar diameter:Number = radius * 2;\r\n\r\npixNum = diameter * diameter;\r\n\r\nvar brushAlpha = new BitmapData(diameter, diameter, true, 0x00000000);\r\nvar dx:int, dy:int;\r\nvar ratio:Number = 255 \/ radius;\r\nvar a:int;\r\n\r\nbrushAlpha.lock();\r\nfor (i = 0; i&lt;pixNum; i++){\r\n\txp = i % diameter;\r\n\typ = i \/ diameter;\r\n\tdx = xp - radius;\r\n\tdy = yp - radius;\r\n\ta = int(255 - Math.min(255,Math.sqrt(dx * dx + dy * dy) * ratio));\r\n\tbrushAlpha.setPixel32(xp, yp, a &lt;&lt; 24);\r\n}\r\nbrushAlpha.unlock();\r\n\r\n\/\/ create a black canvas\r\nvar canvas:BitmapData = new BitmapData(size, size, true, 0xFF000000);\r\naddChild(new Bitmap(canvas));\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\t\/\/ draw the gradient onto the canvas using the alphaChannel (brushAlpha);\r\n\txp = mouseX - radius;\r\n\typ = mouseY - radius;\r\n\tcanvas.copyPixels(gradient, \r\n\t\t\t        new Rectangle(xp, yp, diameter, diameter), \r\n\t\t\t        new Point(xp, yp), brushAlpha, new Point(0,0), true);\r\n}\r\n<\/pre>\n<p>This demo creates an airbrush that paints one BitmapData onto another. This is achieved by using the alpha related arguments of the BitmapData.copyPixels() function. If your not familiar with these you should take some time to play around with them&#8230; they&#8217;re very powerful.<\/p>\n<p>copyPixels() is the fastest way to draw in flash, so if you need speed, copyPixels() is the way to go. It&#8217;s much faster than using draw().<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)] \/\/ draw an ugly gradient var size:int = 500; var pixNum:int = size * size; var gradient:BitmapData = new BitmapData(size, size, true, 0xFF000000); gradient.lock(); var xp:int, yp:int; for (var i:int = 0; i&lt;pixNum; i++){ xp = i % size; yp = i \/ size; gradient.setPixel(xp, yp, (yp \/= 2) &lt;&lt; 16 [&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,26],"tags":[3,4],"class_list":["post-158","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-setpixel","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/158","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=158"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/158\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}