Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

187
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda