7 Replies Latest reply on Aug 13, 2010 12:24 AM by MarkTaylor46

    Local to Global Conversion

    Raphael_352

      Hello Flexperts,

       

      I would like to determine the position of a canvas in global coordinates, but I got something wrong I guess.

       

      Why does

       

      var topLeftGlobal:Point = _myCanvas.localToGlobal(new Point(0,0));

       

      return (0,0) when _myCanvas is not in the top left corner?

       

      Thanks,

      Raphael

        • 1. Re: Local to Global Conversion
          rtalton Level 4

          It's returning 0,0 because you have specified those values when you created the new Point.

           

          Try:

          var topLeftGlobal:Point = _myCanvas.localToGlobal(new Point(_myCanvas.x, _myCanvas.y));

          1 person found this helpful
          • 2. Re: Local to Global Conversion
            Raphael_352 Level 1

            Thank you very much for your reply!!!

             

            The problem is still not solved though.

             

            If you look at

            http://livedocs.adobe.com/flex/3/html/help.html?content=containers_intro_5.html

             

            it seems reasonable to assume that the local coordinates (0,0) would lead to the top left corner postion in global coordinates.

             

            Message was edited by: Raphael_352

            • 3. Re: Local to Global Conversion
              Flex harUI Adobe Employee

              0,0 is your top-left in local coordinates.  The x,y values are the

              coordinates in the parent's coordinate space.

              • 4. Re: Local to Global Conversion
                Raphael_352 Level 1

                Yes, exactly (0,0) is top-left in local coordinates. That I know already.

                 

                So why does globalToLocal(new Point(0,0)) return (0,0) ?

                • 5. Re: Local to Global Conversion
                  MarkTaylor46

                  localToGlobal method does not work correctly until after the applicationComplete event has fired. it will not work correctly on  a creationComplete event.

                   

                  Regards,

                   

                  Mark.

                  1 person found this helpful
                  • 6. Re: Local to Global Conversion
                    Raphael_352 Level 1

                    Yes, this is a very good comment. I made shure thought, that this computation happens after the initialization is fully completed and the component has been added to stage. By now I believe I have found a bug in the api and will try to produce a small example that reproduces it when I find the time.

                     

                    Thanks for your efforts.

                    • 7. Re: Local to Global Conversion
                      MarkTaylor46 Level 2

                      Here is a working example that I used to prove that it works as expected. I hope this help you further testing.

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                                             xmlns:s="library://ns.adobe.com/flex/spark"
                                             xmlns:mx="library://ns.adobe.com/flex/mx"
                                             applicationComplete="init()">
                          <fx:Declarations>
                              <!-- Place non-visual elements (e.g., services, value objects) here -->
                          </fx:Declarations>
                         
                          <fx:Script>
                              <![CDATA[
                                 
                                  private function init():void {
                                      var mcpoint:Point = new Point(_mycanvas.x, _mycanvas.y);
                                      var answerpoint:Point = _mycanvas.localToGlobal(mcpoint);
                                      trace(outsidebox.stage, _mycanvas.stage) ;
                                      trace(answerpoint);
                                  }
                                 
                              ]]>
                          </fx:Script>
                         
                          <s:BorderContainer id="outsidebox" backgroundColor="#00ff00" width="500" height="600" x="10" y="10">
                              <s:BorderContainer id="_mycanvas" backgroundColor="#ff0000" x="10" y="10" width="300" height="500"/>
                          </s:BorderContainer>
                      </s:WindowedApplication>

                       

                       

                       

                      Regards,

                       

                      Mark.