Created
June 10, 2022 10:51
-
-
Save JeremyJaydan/f7ba5f40574841b97d745f8f4ef88c07 to your computer and use it in GitHub Desktop.
figma plugin rotation function based on https://gist.github.com/LukeFinch/d3c93d79a9dcd6970358be1d17838318
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setRotation(node, angle){ | |
let { x, y, width, height, rotation } = node; | |
const theta = angle * (Math.PI / 180); | |
const originTheta = rotation * (Math.PI / 180); | |
const centerX = x + width / 2; | |
const centerY = y + height / 2; | |
const originX = (-Math.cos(originTheta) * x + y * -Math.sin(originTheta) - centerY * -Math.sin(originTheta) - centerX * -Math.cos(originTheta) + centerX) - width; | |
const originY = (Math.sin(originTheta) * x + centerX * -Math.sin(originTheta) + y * -Math.cos(originTheta) - centerY * -Math.cos(originTheta) + centerY) - height; | |
const originCenterX = originX + width / 2; | |
const originCenterY = originY + height / 2; | |
const newX = (Math.cos(theta) * originX + originY * Math.sin(theta) - originCenterY * Math.sin(theta) - originCenterX * Math.cos(theta) + originCenterX); | |
const newY = (-Math.sin(theta) * originX + originCenterX * Math.sin(theta) + originY * Math.cos(theta) - originCenterY * Math.cos(theta) + originCenterY); | |
const transform = [ | |
[Math.cos(theta), Math.sin(theta), newX], | |
[-Math.sin(theta), Math.cos(theta), newY] | |
]; | |
node.relativeTransform = transform; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment