psd.js icon indicating copy to clipboard operation
psd.js copied to clipboard

Layer rotation data

Open joliejuke-orga opened this issue 9 years ago • 3 comments

Is it possible to extract rotation property from a layer?

joliejuke-orga avatar Nov 06 '17 16:11 joliejuke-orga

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))

andreisocaciu avatar Nov 12 '19 12:11 andreisocaciu

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:

  1. If the x value is negative and the y value 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 adding 180
  2. If both the x and y values are negative, we know the rotation is between 180 and 270 degrees. Similarly, we must add 180 to our rotation to get the correct value
  3. Lastly, if the y value is negative and the x value 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 adding 360 to 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.

ConorKelleher avatar Dec 04 '19 12:12 ConorKelleher

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 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:

  1. If the x value is negative and the y value 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 adding 180
  2. If both the x and y values are negative, we know the rotation is between 180 and 270 degrees. Similarly, we must add 180 to our rotation to get the correct value
  3. Lastly, if the y value is negative and the x value 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 adding 360 to 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.

Could I find the angle if only have x,y, width, height?

uea30475 avatar Sep 26 '20 06:09 uea30475