Layer rotation data
Is it possible to extract rotation property from a layer?
For text layers, I managed to compute rotation in degrees with this formula:
Math.round(Math.asin(node.get('typeTool').transform.xy) * (180 / Math.PI))
I don't think the above answer is sufficient. I tried it and it only seemed to work for very simple rotations (e.g. 45 degrees). If the rotation went beyond 90 degrees, or if there were was some skewing to the shape, it gave increasingly incorrect values.
I managed to get it working by using both the xx and xy values, giving us a scaled vector between the origin (i.e. top left point of the text layer's bounding box) and the top right point of the bounding box. As long as no skewing is done, this one vector should be enough to tell us the rotation of the whole layer.
Using methods outlaid here: https://www.dummies.com/education/science/physics/how-to-find-a-vectors-magnitude-and-direction/, I derived a formula to use these values to get the angle of the vector:
Math.round(Math.atan(transform.xy / transform.xx) * (180 / Math.PI))
However, this will only give values in the range [-90, 90], so there are also special conditions that the linked page mentions:
- If the
xvalue is negative and theyvalue is positive, we know the rotation is between 90 and 180 degrees. If this is the case, the answer the above formula gets will be negative and bound to the wrong axis, so this can be fixed by adding180 - If both the
xandyvalues are negative, we know the rotation is between 180 and 270 degrees. Similarly, we must add180to our rotation to get the correct value - Lastly, if the
yvalue is negative and thexvalue is positive, we know the rotation is between 270 and 360 degrees. Our formula above will represent this as negatives, bound to the range [-90, 0]. The correct, positive rotation in degrees can be found by simply adding360to this
All in all, my final code looks something like this:
let rotation = Math.round(Math.atan(transform.xy / transform.xx) * (180 / Math.PI))
if (transform.xx < 0) {
rotation += 180
} else if (rotation < 0) {
rotation += 360
}
Took far longer than I'd have liked to work this all out but hopefully some people can get some benefit out of it.
transform
Can i find the angle if only have x,y, width, height?
I don't think the above answer is sufficient. I tried it and it only seemed to work for very simple rotations (e.g. 45 degrees). If the rotation went beyond 90 degrees, or if there were was some skewing to the shape, it gave increasingly incorrect values.
I managed to get it working by using both the
xxandxyvalues, giving us a scaled vector between the origin (i.e. top left point of the text layer's bounding box) and the top right point of the bounding box. As long as no skewing is done, this one vector should be enough to tell us the rotation of the whole layer.Using methods outlaid here: https://www.dummies.com/education/science/physics/how-to-find-a-vectors-magnitude-and-direction/, I derived a formula to use these values to get the angle of the vector:
Math.round(Math.atan(transform.xy / transform.xx) * (180 / Math.PI))However, this will only give values in the range [-90, 90], so there are also special conditions that the linked page mentions:
- If the
xvalue is negative and theyvalue is positive, we know the rotation is between 90 and 180 degrees. If this is the case, the answer the above formula gets will be negative and bound to the wrong axis, so this can be fixed by adding180- If both the
xandyvalues are negative, we know the rotation is between 180 and 270 degrees. Similarly, we must add180to our rotation to get the correct value- Lastly, if the
yvalue is negative and thexvalue is positive, we know the rotation is between 270 and 360 degrees. Our formula above will represent this as negatives, bound to the range [-90, 0]. The correct, positive rotation in degrees can be found by simply adding360to thisAll in all, my final code looks something like this:
let rotation = Math.round(Math.atan(transform.xy / transform.xx) * (180 / Math.PI)) if (transform.xx < 0) { rotation += 180 } else if (rotation < 0) { rotation += 360 }Took far longer than I'd have liked to work this all out but hopefully some people can get some benefit out of it.
Could I find the angle if only have x,y, width, height?