Skip to content

Instantly share code, notes, and snippets.

@JeremyJaydan
Created June 10, 2022 10:51
Show Gist options
  • Save JeremyJaydan/f7ba5f40574841b97d745f8f4ef88c07 to your computer and use it in GitHub Desktop.
Save JeremyJaydan/f7ba5f40574841b97d745f8f4ef88c07 to your computer and use it in GitHub Desktop.
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