JavaScript API. Is there a way to getPosition of element/object

Problem: Me. I am use to actionscript not javascript (although learned a lot reading here about the Javascript API in Saola)

Saola Animate version: 2.71

OS: Windows 10

Notes: I want to be able to move an element with a button by code (using += etc.). I have learned about the timeline and triggers (an option).
setPosition works great, but only once. I wanted to use a variable to get the current x and y then add to it each time button is clicked etc.

Hi,

You can get an element position from the dom object associated with the element. For example:

  var element = doc.getElement('element name');
  var elementDom = element.dom;
  var elementLeft = elementDom.offsetLeft;
  var elementTop = elementDom.offsetTop;
  // set new position
  elementLeft += 10;
  elementTop += 10;
  element.setPosition(elementLeft, elementTop);

You can also save the position to reuse without getting from the dom object. For example:

  if (!doc.myElementPos) {
    doc.myElementPos = {left: 100, top: 100};  // initial position
  }
  doc.myElementPos.left += 10;
  doc.myElementPos.top += 10;
  element.setPosition(doc.myElementPos.left, doc.myElementPos.top);

Regards

2 Likes

Thank you! I took a break from when I first purchased this software. I now have more time and have been learning how to use it and I continue to be SO IMPRESSED with it. Not too mention the amazing support. Means a lot to me. :smile: