{"id":210,"date":"2009-03-05T00:00:15","date_gmt":"2009-03-05T07:00:15","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=890"},"modified":"2009-03-05T00:00:15","modified_gmt":"2009-03-05T07:00:15","slug":"animate-graphicslineto","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=210","title":{"rendered":"Animate Graphics.lineTo()"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ line thickness, line color, start point, end point, divisor, callback\r\nfunction animateLineTo( thick:Number, col:uint, sp:Point, ep:Point, div:Number=4, callback:Function=null):void {\r\n\t var s:Shape  = Shape(addChild(new Shape()));\r\n\t var ap:Point = sp.clone();\r\n\t div = Math.max(2, div);\r\n\t setTimeout(runLineAnimation, 1000\/stage.frameRate, s.graphics , thick , col , sp , ep , ap, div, callback);\r\n}\r\n\r\nfunction runLineAnimation(g:Graphics , thick:Number, col:uint, sp:Point, ep:Point, ap:Point, div:Number, callback:Function):void {\r\n\t ap.x += (ep.x - ap.x) \/ div;\r\n\t ap.y += (ep.y - ap.y) \/ div;\r\n\t  with(g){\r\n\t\tclear();\r\n\t\tlineStyle(thick, col);\r\n\t\tmoveTo(sp.x, sp.y);\r\n\t\tlineTo(ap.x, ap.y);\r\n\t  }\r\n\t   if (Math.abs(ap.x - ep.x) &lt; 1 &amp;&amp; Math.abs(ap.y- ep.y)  &lt; 1){\r\n\t\t   \/\/ done\r\n\t\t   if (callback!=null){\r\n\t\t\t   callback();   \r\n\t\t   }\r\n\t   }else{\r\n\t\t    setTimeout(runLineAnimation, 1000\/stage.frameRate, g , thick , col , sp , ep , ap, div, callback);\r\n\t   }\r\n}\r\n\/\/\r\n\/\/ test out the animateLineTo function:\r\n\/\/\r\nvar a:Point = new Point(100,100);\r\nvar b:Point = new Point(150, 200);\r\nvar c:Point = new Point(300, 190);\r\nvar d:Point = new Point(280, 90)\r\n\r\nanimateLineTo(0, 0xFF0000, a, b, 4, line2);\r\n\r\nfunction line2():void{\r\n\tanimateLineTo(0, 0xFF0000, b, c, 4, line3);\r\n}\r\n\r\nfunction line3():void{\r\n\tanimateLineTo(0, 0xFF0000, c, d, 4, line4);\r\n}\r\n\r\nfunction line4():void{\r\n\tanimateLineTo(0, 0xFF0000, d, a, 4);\r\n}\r\n<\/pre>\n<p>The above demos a function called animateLineTo() that will draw a line using zeno&#8217;s paradox. <\/p>\n<p>There are a couple weird\/interesting tricks going on here. I&#8217;m using setTimeout() over and over rathaer than using setInterval(). This makes it so I don&#8217;t need to store an interval id. I&#8217;m also using a delay of 1000\/stage.frameRate, this attempts to cause the setTimeout() to run at the same frequency as an enter frame would. This is important because having a setTimeout()\/setInterval() that is doing animation and attempting to run more frequently than the framerate can cause problems and is a waste of cpu power.<\/p>\n<p>This snippet was spurned by a student question about how to animate a Graphics.lineTo() from point A to point B. Here is the original snippet, which is easier to read and more bare bones:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar p0:Point = new Point(100,100);\r\nvar p1:Point = new Point(400,200);\r\nvar dp:Point = new Point(p0.x,p0.y);\r\n\r\nvar s:Sprite = Sprite(addChild(new Sprite()));\r\n\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\ts.graphics.clear()\r\n\ts.graphics.lineStyle(0,0x000000);\r\n\ts.graphics.moveTo(p0.x, p0.y);\r\n\ts.graphics.lineTo(dp.x, dp.y);\r\n\tdp.x += (p1.x - dp.x) \/ 4;\r\n\tdp.y += (p1.y - dp.y) \/ 4;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ line thickness, line color, start point, end point, divisor, callback function animateLineTo( thick:Number, col:uint, sp:Point, ep:Point, div:Number=4, callback:Function=null):void { var s:Shape = Shape(addChild(new Shape())); var ap:Point = sp.clone(); div = Math.max(2, div); setTimeout(runLineAnimation, 1000\/stage.frameRate, s.graphics , thick , col , sp , ep , ap, div, callback); } function runLineAnimation(g:Graphics , thick:Number, col:uint, sp:Point, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[3,4],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-motion","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/210","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=210"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}