{"id":392,"date":"2009-08-30T01:22:13","date_gmt":"2009-08-30T08:22:13","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=2070"},"modified":"2009-08-30T01:22:13","modified_gmt":"2009-08-30T08:22:13","slug":"globaltolocal-in-3d","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=392","title":{"rendered":"globalToLocal() in 3D"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n&#x5B;SWF(frameRate=60, backgroundColor=0x000000, width=500, height=500)]\r\nstage.quality = &quot;medium&quot;;\r\nvar frame:Sprite = Sprite(addChild(new Sprite()));\r\nwith (frame.graphics) beginFill(0xCCCCCC), drawRect(-200, -200, 400, 400), endFill();\r\nframe.x = stage.stageWidth \/ 2;\r\nframe.y = stage.stageHeight \/ 2;\r\nframe.z = 100;\r\n\r\nvar canvas:Shape = Shape(frame.addChild(new Shape()));\r\nvar msk:Shape = Shape(frame.addChild(new Shape()));\r\nwith (msk.graphics) beginFill(0x00FF00), drawRect(-200, -200, 400, 400), endFill();\r\ncanvas.mask = msk\r\n\r\nvar txt:TextField = TextField(addChild(new TextField()));\r\ntxt.defaultTextFormat = new TextFormat(&quot;_sans&quot;, 12);\r\ntxt.x = txt.y = 10;\r\ntxt.textColor = 0xFFFFFF, txt.autoSize=&quot;left&quot;, txt.text = &quot;Draw on the 3D plane...&quot;;\r\n\r\nstage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);\r\nstage.addEventListener(MouseEvent.MOUSE_UP, onUp);\r\nfunction onDown(evt:MouseEvent):void{\r\n\tcanvas.graphics.lineStyle(4, 0x000000);\r\n\tvar pnt:Point = frame.globalToLocal(new Point(mouseX, mouseY));\r\n\tcanvas.graphics.moveTo(pnt.x, pnt.y);\r\n\taddEventListener(Event.ENTER_FRAME, onDraw);\r\n}\r\nfunction onUp(evt:MouseEvent):void{\r\n\tremoveEventListener(Event.ENTER_FRAME, onDraw);\r\n}\r\n\r\nvar t:Number = 0;\r\naddEventListener(Event.ENTER_FRAME, onLoop);\r\nfunction onLoop(evt:Event):void {\r\n    frame.rotationY = 35 * Math.sin(t);\r\n\tframe.rotationX = 35 * Math.cos(t);\r\n\tt+=0.02;\r\n}\r\n\r\nfunction onDraw(evt:Event):void {\r\n        var pnt:Point = frame.globalToLocal(new Point(mouseX, mouseY));\r\n\tcanvas.graphics.lineTo(pnt.x, pnt.y);\r\n}\r\n<\/pre>\n<p>This demo shows that globalToLocal() works with 3D &#8211; saving us the trouble of doing some extra math if we want to draw on 3D display objects&#8230;<\/p>\n<p><a href=\"http:\/\/wonderfl.net\/code\/14af37a14c28806027462883e24f1147d0c1bdcc\" target=\"blank\">Have a look at the swf&#8230;.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/actionsnippet.com\/wp-content\/draw3d.gif\" \/><br \/>\n<\/a><\/p>\n<p>Was made aware of this trick by watching a video that <a href=\"http:\/\/twitter.com\/kevinSuttle\">kevinSuttle<\/a> sent me via twitter. <a href=\"http:\/\/vplayer.oreilly.com\/?chapter=20&#038;video_product=urn%3Ax-domain%3Aoreilly.com%3Aproduct%3A9780596801540.VIDEO\" target=\"blank\">The video<\/a> is an interview with Chris Nuuja (one of the flash player engineers).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#x5B;SWF(frameRate=60, backgroundColor=0x000000, width=500, height=500)] stage.quality = &quot;medium&quot;; var frame:Sprite = Sprite(addChild(new Sprite())); with (frame.graphics) beginFill(0xCCCCCC), drawRect(-200, -200, 400, 400), endFill(); frame.x = stage.stageWidth \/ 2; frame.y = stage.stageHeight \/ 2; frame.z = 100; var canvas:Shape = Shape(frame.addChild(new Shape())); var msk:Shape = Shape(frame.addChild(new Shape())); with (msk.graphics) beginFill(0x00FF00), drawRect(-200, -200, 400, 400), endFill(); canvas.mask = msk var [&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,6,33,94,13],"tags":[3,14,4],"class_list":["post-392","post","type-post","status-publish","format-standard","hentry","category-3d","category-displayobject","category-graphics","category-misc","category-motion","tag-actionscript","tag-as3","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/392","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=392"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/392\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}