{"id":314,"date":"2009-06-13T16:07:12","date_gmt":"2009-06-13T23:07:12","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1528"},"modified":"2009-06-13T16:07:12","modified_gmt":"2009-06-13T23:07:12","slug":"igraphicsdata-example","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=314","title":{"rendered":"IGraphicsData Example"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ note the high framerate for testing purposes\r\n&#x5B;SWF(width = 800, height = 600, frameRate=60)]\r\n\/\/ red, yellow, blue\r\nvar fills:Vector.&lt;IGraphicsData&gt; = Vector.&lt;IGraphicsData&gt;(&#x5B;new GraphicsSolidFill(0xFF0000),  new GraphicsSolidFill(0xFFCC00), new GraphicsSolidFill(0x0033FF)]);\r\n\r\nvar stroke:IGraphicsData = new GraphicsStroke();\r\n\r\nvar cmds:Vector.&lt;int&gt; = new Vector.&lt;int&gt;();\r\nvar ci:int = 0;\r\nvar dat:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;();\r\nvar di:int = 0;\r\nvar igraph:Vector.&lt;IGraphicsData&gt; = new Vector.&lt;IGraphicsData&gt;();\r\nvar ig:int  = 0;\r\nvar path:Vector.&lt;GraphicsPath&gt; = new Vector.&lt;GraphicsPath&gt;();\r\nvar boxNum:int = 1500;\r\nvar locX:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(boxNum);\r\nvar locY:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(boxNum);\r\nvar velX:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(boxNum);\r\nvar velY:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(boxNum);\r\nvar cols:int = 50;\r\nfor (var i:int = 0; i&lt;boxNum; i++){\r\n\tpath&#x5B;i] = new GraphicsPath(new Vector.&lt;int&gt;(), new Vector.&lt;Number&gt;());\r\n\tpath&#x5B;i].winding = GraphicsPathWinding.NON_ZERO;\r\n\tvar theta:Number = i * Math.PI\/180;\r\n\tvar sin:Number = Math.cos(theta);\r\n\tvar cos:Number =  Math.sin(theta);\r\n\tvar r:Number = Math.random()*100;\r\n\tvar vr:Number = r * 0.05;\r\n\tvelX&#x5B;i] = vr * cos;\r\n\tvelY&#x5B;i] = vr * sin;\r\n\tlocX&#x5B;i] = 400 + r * cos;\r\n    locY&#x5B;i] = 300 + r * sin;\r\n}\r\n\r\n\/\/ box vars\r\nvar xp:Number, yp:Number, size:Number, hs:Number;\r\n\/\/ corners\r\nvar x0:Number, y0:Number, x1:Number, y1:Number;\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tgraphics.clear();\r\n\tig = 0;\r\n\tfor (i= 0; i&lt;boxNum; i++){\r\n\t\t\/\/ inline function:\r\n\t\tlocX&#x5B;i] += velX&#x5B;i];\r\n\t\tlocY&#x5B;i] += velY&#x5B;i];\r\n\t\txp = locX&#x5B;i];\r\n\t\typ = locY&#x5B;i];\r\n\t\tif (xp &lt; 0 || xp &gt; 800){\r\n\t\t\tvelX&#x5B;i] *= -1;\r\n\t\t}\r\n\t\tif (yp &lt; 0 || yp &gt; 600){\r\n\t\t\tvelY&#x5B;i] *= -1;\r\n\t\t}\r\n\t\tsize = 10 + i % 10;\r\n\t\ths = size * 0.5;\r\n\t\tx0 = xp - hs, y0 = yp - hs;\r\n\t\tx1 = xp + hs, y1 = yp + hs;\r\n\t\tci = 0;\r\n\t\tdi = 0;\r\n\t\tcmds = path&#x5B;i].commands;\r\n\t\tdat = path&#x5B;i].data;\r\n\t\t\/\/ GraphicsPathCommand.MOVE_TO\r\n\t\tcmds&#x5B;ci++] = 1;\r\n\t\tdat&#x5B;di++] = x0 , dat&#x5B;di++] = y0;\r\n\t\t\/\/ GraphicsPathCommand.LINE_TO\r\n\t\tcmds&#x5B;ci++] = 2;\r\n\t\tdat&#x5B;di++] = x1 , dat&#x5B;di++] = y0;\r\n\t\t\/\/ GraphicsPathCommand.LINE_TO\r\n\t\tcmds&#x5B;ci++] = 2;\r\n\t\tdat&#x5B;di++] = x1 , dat&#x5B;di++] = y1;\r\n\t\t\/\/ GraphicsPathCommand.LINE_TO\r\n\t\tcmds&#x5B;ci++] = 2;\r\n\t\tdat&#x5B;di++] = x0 , dat&#x5B;di++] = y1;\r\n\t\t\/\/ end inline function\r\n\t\t \r\n\t\tpath&#x5B;i].commands = cmds;\r\n\t\tpath&#x5B;i].data = dat;\r\n\t\tigraph&#x5B;ig++] = fills&#x5B;i % 3];\r\n\t\tigraph&#x5B;ig++] = path&#x5B;i];\r\n\t}\r\n\t\/\/ everything is drawn with one function call\r\n    graphics.drawGraphicsData(igraph);\r\n}\r\n<\/pre>\n<p>I haven&#8217;t spent much time with IGraphicsData. It&#8217;s a very cool feature and I think its going to take me awhile to realize its full potential. Since I have to start somewhere I decided to write this snippet to show that it is rather fast. I also recalled reading some <a href=\"http:\/\/www.bytearray.org\/?p=423\" target=\"blank\">benchmark info for the new fp10 graphics stuff over at bytearray.org&#8230;.<\/a> pretty nice info there&#8230; <\/p>\n<p>This snippet is pretty optimized, there are a few areas (such as the recurring population of the cmds Vector) that could be optimized more but are left this way for SOME flexibility.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ note the high framerate for testing purposes &#x5B;SWF(width = 800, height = 600, frameRate=60)] \/\/ red, yellow, blue var fills:Vector.&lt;IGraphicsData&gt; = Vector.&lt;IGraphicsData&gt;(&#x5B;new GraphicsSolidFill(0xFF0000), new GraphicsSolidFill(0xFFCC00), new GraphicsSolidFill(0x0033FF)]); var stroke:IGraphicsData = new GraphicsStroke(); var cmds:Vector.&lt;int&gt; = new Vector.&lt;int&gt;(); var ci:int = 0; var dat:Vector.&lt;Number&gt; = new Vector.&lt;Number&gt;(); var di:int = 0; var igraph:Vector.&lt;IGraphicsData&gt; = new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,94,13,79],"tags":[3,14,4],"class_list":["post-314","post","type-post","status-publish","format-standard","hentry","category-graphics","category-misc","category-motion","category-vector","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/314","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=314"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/314\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}