This post was originally published at Tutorial: Extending anchor points

In Corona, all display objects have an anchor point. Essentially, this can be imagined as a “pin” point within the object’s bounds around which all transforms occur — x/y positioning, rotation, and scaling. For example, if the anchor point is defined at the top-left corner of an object, rotation of the object will cause it to rotate around that point.

History and background

In legacy versions of Corona, this concept was known as the reference point and it was handled by two deprecated approaches. The first was to use the object:setReferencePoint() API which accepted 9 possible constants as means to set the point at either the center of the object or at specific points around the object’s edges. For example, you could specify display.TopLeftReferencePoint to “pin” the object at its top-left corner, or display.TopCenterReferencePoint for the horizontal center point along the object’s top edge.

Alternatively, you could define a more fine-tuned reference point by setting the xReference or yReference properties of the object. This allowed developers to set the reference point to any point within the object’s bounds, or even to a theoretical point outside the object’s bounds by setting values beyond its edges.

Current method

In modern versions of Corona, both of the above methods have been replaced by anchor points. However, instead of setting a constant or content area value, anchor points are typically defined as decimal values between 0.0 and 1.0, where 0.0 represents the top or left edge, 0.5 represents the center, and 1.0 represents the bottom or right edge. As an example, if you wanted to mimic the legacy display.TopLeftReferencePoint, you could set both anchor points to 0.0 (or simply 0):

In this way, anchor points can achieve what the previous methods could — however, one thing that was initially “eliminated” with the introduction of anchor points was the ability to set the point outside of the object’s bounds, previously achieved by setting the object’s xReference or yReference properties to values beyond its edges.

In specific, the default behavior of anchor points is that they are limited to the range between 0.0 and 1.0, so values set outside this range will be clamped to the respective limit.

Extending anchor points

To re-introduce the concept of non-clamped anchor points, our engineers have added a new "isAnchorClamped" option for display.setDefault(), available in Daily Build 2015.2733 and later. When set to false, you can define anchor points outside of the object’s bounds:

Now, anchor points set to values outside the 01 range will be honored:

Example usage

One example where this approach is useful is rotating an object around another object, like a planet orbiting its star. If the anchor point is clamped to points inside or at the object’s edge, the object can only rotate in a limited perspective. Now, with "isAnchorClamped" set to false, you can easily make the planet orbit the star:

Conclusion

With this new setting, you now have access to the full power that reference points provided in legacy versions of Corona, including points both inside and outside of the object’s bounds. Remember, however, that this feature is only available in Daily Build 2015.2733 or later.


Originally posted here:

This post was originally published at Tutorial: Extending anchor points