{"id":266,"date":"2009-04-26T18:09:33","date_gmt":"2009-04-27T01:09:33","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1233"},"modified":"2009-04-26T18:09:33","modified_gmt":"2009-04-27T01:09:33","slug":"swirl-gradient","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=266","title":{"rendered":"Swirl Gradient"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar canvas:BitmapData=new BitmapData(200,200,false,0x000000);\r\naddChild(new Bitmap(canvas));\r\nscaleX=scaleY=2;\r\nvar pixNum:int=canvas.width*canvas.height;\r\n\r\nvar xp:Vector.&lt;int&gt; = new Vector.&lt;int&gt;();\r\nvar yp:Vector.&lt;int&gt; = new Vector.&lt;int&gt;();\r\nvar radius:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar theta:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nfor (var i:int = 0; i&lt;pixNum; i++) {\r\n\txp.push(i % 200);\r\n\typ.push(int(i \/ 200));\r\n\tvar dx:Number=100-xp&#x5B;i];\r\n\tvar dy:Number=100-yp&#x5B;i];\r\n\ttheta.push(Math.atan2(dy, dx));\r\n\tradius.push(Math.sqrt(dx * dx + dy * dy)\/20);\r\n}\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tcanvas.lock();\r\n\tvar n:Number = mouseX \/ 100;\r\n\tfor (var i:int = 0; i&lt;pixNum; i++) {\r\n\t\tvar swirl:Number = 1+Math.sin(6*Math.cos(radius&#x5B;i]) -n*theta&#x5B;i]);\r\n\t\tcanvas.setPixel(xp&#x5B;i], yp&#x5B;i],  Math.abs(255 - swirl * 255));\r\n\t}\r\n\tcanvas.unlock();\r\n}\r\n<\/pre>\n<p>This snippet creates a swirl gradient. While this snippet is not highly optimized, it does implement a basic optimization technique &#8230; I cache some repetitive calculations in Vectors and then use them on the main loop. <\/p>\n<p>I got the function for a swirl gradient from <a href=\"http:\/\/mathworld.wolfram.com\/Swirl.html\" target=\"blank\">mathworld&#8230;<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>var canvas:BitmapData=new BitmapData(200,200,false,0x000000); addChild(new Bitmap(canvas)); scaleX=scaleY=2; var pixNum:int=canvas.width*canvas.height; var xp:Vector.&lt;int&gt; = new Vector.&lt;int&gt;(); var yp:Vector.&lt;int&gt; = new Vector.&lt;int&gt;(); var radius:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var theta:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); for (var i:int = 0; i&lt;pixNum; i++) { xp.push(i % 200); yp.push(int(i \/ 200)); var dx:Number=100-xp&#x5B;i]; var dy:Number=100-yp&#x5B;i]; theta.push(Math.atan2(dy, dx)); radius.push(Math.sqrt(dx * dx + dy * dy)\/20); [&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,121,25,26],"tags":[3,4],"class_list":["post-266","post","type-post","status-publish","format-standard","hentry","category-bitmapdata","category-math","category-pixel-manipulation","category-setpixel","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/266","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=266"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/266\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}