{"id":313,"date":"2009-06-12T16:17:41","date_gmt":"2009-06-12T23:17:41","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1525"},"modified":"2009-06-12T16:17:41","modified_gmt":"2009-06-12T23:17:41","slug":"vectorpark-style-cylinder","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=313","title":{"rendered":"Vectorpark Style Cylinder"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nconst TWO_PI:Number = Math.PI * 2;\r\nconst PI_HALF_PI:Number = Math.PI + Math.PI \/ 2;\r\nx = stage.stageWidth \/ 2;\r\ny = stage.stageHeight \/ 2;\r\nvar runCylinder:Function = makeCylinder(0,0, 100, 300, 0xFF0000, 0xFFCC00);\r\nvar dx:Number = 0;\r\nvar dy:Number = 0;\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n\tdx += (mouseX \/ 50 - dx) \/ 4;\r\n\tdy += (mouseY - dy) \/ 4;\r\n\trunCylinder(dx, dy);\r\n}\r\nfunction makeCylinder(x:Number, y:Number, size:Number, length:Number, colA:uint, colB:uint):Function{\r\n\tvar s:Sprite = Sprite(addChild(new Sprite()));\r\n\ts.x = x;\r\n\ts.y = y;\r\n\tvar acol:uint = colB;\r\n\tvar bcol:uint = colB;\r\n\tvar halfLength:Number = length \/ 2;\r\n\tvar capA:Sprite = Sprite(s.addChild(new Sprite()));\r\n\tvar box:Sprite = Sprite(s.addChild(new Sprite()));\r\n\tvar w:Number = size * 2;\r\n\tvar h:Number = length;\r\n\twith (box.graphics)  beginFill(colA), drawRect(-w\/2, -h\/2, w,h);\r\n\tvar capB:Sprite = Sprite(s.addChild(new Sprite()));\r\n\treturn function(theta:Number, rot:Number){\r\n\t\ttheta %= TWO_PI;\r\n\t\tif (theta &lt;0){\r\n\t\t\ttheta = TWO_PI - -theta;\r\n\t\t}\r\n\t\tvar scale:Number = Math.cos(theta);\r\n\t\tif (theta&gt; 1.57){\r\n\t\t\tacol = colA;\r\n\t\t\ts.addChild(capB);\r\n\t\t\tbcol = colB;\r\n\t\t} else {\r\n\t\t\tacol = colB;\r\n\t\t\tbcol = colA;\r\n\t\t\ts.addChild(capA);\r\n\t\t}\r\n\t\tif (theta&gt; PI_HALF_PI){\r\n\t\t\tacol = colB;\r\n\t\t\tbcol = colA;\r\n\t\t\ts.addChild(capA);\r\n\t\t}\r\n\t\t\r\n\t\tvar sin:Number = Math.sin(theta);\r\n\t\tbox.scaleY = sin;\r\n\t\tcapA.y = halfLength * sin;\r\n\t\tcapB.y = -capA.y;\r\n\t\twith(capA.graphics) clear(), beginFill(acol), scaleYcircle(capA.graphics,size, scale);\r\n\t\twith(capB.graphics) clear(), beginFill(bcol), scaleYcircle(capB.graphics,size, scale);\r\n\t\ts.rotation = rot;\r\n\t}\r\n}\r\n\/\/ original circle function by senocular (www.senocular.com) from here http:\/\/www.actionscript.org\/forums\/showthread.php3?s=&amp;threadid=30328\r\n\/\/ circle that can be scaled on the y axis\r\nfunction scaleYcircle(g:Graphics, r:Number, s:Number = 1):void {\r\n\tvar c1:Number = r * (Math.SQRT2 - 1);\r\n\tvar c2:Number = r * Math.SQRT2 \/ 2;\r\n\tvar rs:Number = r * s, c1s:Number = c1 * s, c2s:Number = c2 * s;\r\n\tvar x_r:Number =  -r, y_r:Number = -rs, x_c2:Number =  -c2;\r\n\tvar y_c2:Number =  -c2s, x_c1:Number =  -c1, y_c1:Number =  -c1s\r\n\tg.moveTo(r, 0), g.curveTo(r, c1s, c2, c2s);\r\n\tg.curveTo(c1, rs, 0, rs), g.curveTo(x_c1,rs, x_c2, c2s);\r\n\tg.curveTo(x_r, c1s, x_r, 0), g.curveTo(x_r,y_c1,x_c2,y_c2);\r\n\tg.curveTo(x_c1,y_r,0,y_r), g.curveTo(c1,y_r,c2,y_c2);\r\n\tg.curveTo(r,y_c1,r,0);\r\n}\r\n<\/pre>\n<p>This is similar to the last post I did&#8230;. inspired by some of the great stuff over at <a href=\"http:\/\/vectorpark.com\/\" target=\"blank\">VectorPark.com<\/a>&#8230; this snippet creates a 3D cylinder out of two circles and a box&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>const TWO_PI:Number = Math.PI * 2; const PI_HALF_PI:Number = Math.PI + Math.PI \/ 2; x = stage.stageWidth \/ 2; y = stage.stageHeight \/ 2; var runCylinder:Function = makeCylinder(0,0, 100, 300, 0xFF0000, 0xFFCC00); var dx:Number = 0; var dy:Number = 0; addEventListener(Event.ENTER_FRAME, onLoop); function onLoop(evt:Event):void { dx += (mouseX \/ 50 &#8211; dx) \/ 4; dy [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,33],"tags":[3,14,4],"class_list":["post-313","post","type-post","status-publish","format-standard","hentry","category-3d","category-graphics","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/313","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=313"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/313\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}