I need to center an object in a canvas based on its rotation. I can't figure out the maths.
What information do I have?
- x, and y coordinates of the top left corner (see red circle of image)
- width and height of the object
- the rotation value in degrees
What have i tried so far?
// center horizontally
if (curretElement === null) return;
curretElement.x((canvas.width() / 2) - ((curretElement.width() * curretElement.scaleX()) / 2));
canvas.draw();
// center vertically
curretElement.y((canvas.height() / 2) - ((curretElement.height() * curretElement.scaleY()) / 2));
canvas.draw();
This centers the image when it's not rotated.
currentElement is the selected object.
canvas is the room where the object should be centered in.

(-width * scale / 2, -height * scale / 2), as you already calculate. Add that to the canvas center, and you have the coordinates for angle = 0, again as you already do. For a different angle, all you need to do is rotate the vector before adding it to the canvas center. You can use a 2D rotation matrix for that. - Chris G