I have the following crop coordinates relative to this rotated image:
The coordinates are relative to the rotated image's height and width. The rotation origin is the centre of the crop rectangle. The rotation of the crop is known.
My goal here is to use the drawImage call and the crop information above and render the following image:
My approach so far is:
Rotate the image first.
Translate the coordinates shown above to be relative to the container.
Call drawImage with the canvas containing the rotated image as the source and the translated coordinates.
drawImage(rotatedCanvas, rotatedX, rotatedY, cropWidth, cropHeight, 0, 0, width, height)
Issues:
I'm not sure how to find the centre point of the crop rectangle given the crop information above. Without this, I cannot set the correct origin for the rotation of the image
Without finding this centre point, I cannot use the following code to rotate the coordinates correctly.
// cx origin x, cy origin y
const rotate = (cx: number, cy: number, x: number, y: number, angle: number) => {
const radians = (Math.PI / 180) * angle;
const cos = Math.cos(radians);
const sin = Math.sin(radians);
const nx = cos * (x - cx) + sin * (y - cy) + cx;
const ny = cos * (y - cy) - sin * (x - cx) + cy;
return { nx, ny };
};
To be honest, I'm not sure if I'm going about this the wrong way completely.
Any advice would be greatly appreciated! Many thanks.
Posting this as an answer so we can make at least some progress here.
I made another image of the situation and assuming that those crop area "coordinates" are percentages of the underlying image width and height, we can use those to calculate the top-left and bottom-right coordinates of the crop rectangle.
If we further assume that the crop region is a square, finding the midpoint is just finding the line between the top-left and bottom-right coordinates of the crop area, and then finding the midpoint of that line.
Looking at the question though, the crop area doesn't seem to be square. In that case, we have couple options:
Option 1
Temporarily rotate the crop area (or the image) so that the "relative rotation" between them is 0. Now we can construct the top-right and bottom-left coordinates of the crop area directly, by using the top-left and bottom-right coordinates we already have, and then just rotate everything back
Option 2
Use the same, whatever method was used to acquire the top-left and bottom-right percentages to acquire the top-right and bottom-left coordinates, find the line between those coordinates as well and then find the intersection point of the two lines you now have to get the crop area midpoint
My image with some calculations