Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Crop a rectangle with coordinates relative to a rotated image in HTML Canvas

I have the following crop coordinates relative to this rotated image:

crop information relative to the 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:

enter image description here

My approach so far is:

  1. Rotate the image first.

  2. Translate the coordinates shown above to be relative to the container.

  3. 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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

My image with some calculations

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!