Skip to main content
paul_james123
Inspiring
February 24, 2019
Answered

Finding the Coordinates/ Location of an Element

  • February 24, 2019
  • 2 replies
  • 2286 views

Hi All,

I'm probably not using the correct search terms to find the answer in this forum:

I just need to find the x and y coordinates for an instance -relative to the stage.

(I'm publishing my FLA as a Canvas project using Animate CC.)

myCoordinate = this.myInstance.getBoundingClientRect();

alert (myCoordinate.top);

...Doesn't seem to work in my script window.

Thanks!

    This topic has been closed for replies.
    Correct answer kglad

    var instanceX=this.myInstanceName.x; // sim for y

    2 replies

    Legend
    February 25, 2019

    He appears to be working in a Canvas document.

    Paul, getBoundingClientRect() is browser DOM method. It does not exist in the Animate canvas API. If your object is nested within another clip, you'll probably need to use localToGlobal. But if it's sitting directly on the stage, you just access the clip's x and y properties.

    paul_james123
    Inspiring
    February 25, 2019

    Clay UUID wrote:

    "Paul, getBoundingClientRect() is browser DOM method. It does not exist in the Animate canvas API. If your object is nested within another clip, you'll probably need to use localToGlobal. But if it's sitting directly on the stage, you just access the clip's x and y properties."

    Yes it's just sitting on the stage. How can I access (find) the X and Y properties to find the coordinates?

    Thanks

    kglad
    Community Expert
    kgladCommunity ExpertCorrect answer
    Community Expert
    February 25, 2019

    var instanceX=this.myInstanceName.x; // sim for y

    kglad
    Community Expert
    Community Expert
    February 25, 2019

    var rect:Rectangle=this.myInstance.getBounds(this);

    paul_james123
    Inspiring
    February 25, 2019

    Thanks Kglad.

    If I use the 'var,' the FLA fails (a blank browser window results when publishing), so I put this in my Animate script window:

    rect:Rectangle = this.myInstanceName.getBounds(this);

    alert(rect);

    ...but no alert shows in the browser window upon publish.

    Does that syntax not work with an Animate (Canvas) project?

    I also tried:

    myBounds = this.myInstanceName.getBounds(this);

    alert(myBounds);

    ...but he alert shows "[Rectangle (x=0 y=0 width=42 height=42)]"  ---no x and y coordinates relative to the stage.

    Am I doing something wrong? I just want to find the x and y coordinates (location) of the instance relative to the stage.

    Thanks much