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

205
Vistas
Is there a way to rotate a <canvas> element and treat the input as its rotated value?

I have a canvas with ID foobar. I've already drawn to it.

let canvas = document.getElementById("foobar");

I need to rotate this canvas 90°. Rotating #foobar with CSS doesn't work:

#foobar {
  /* When I click somewhere on the canvas, it registers the position of the click
     as what the position would be pre-rotation rather than post-rotation. */
  transform: rotate(90deg);
}

In addition, rotating the context only works when an element has yet to be drawn, so this won't work either:

let context = canvas.getContext("2d");
context.rotate(90 * (Math.PI / 180));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When registering clicks, try to use event.offsetX and event.offsetY. These should line up properly with the canvas coordinates. For example:

<canvas>
</canvas>

<style>
   canvas {
      transform: rotate(90deg);
      /* just so the canvas is visible */
      border-color: black;
      border-style: solid;
   }
</style>

<script>
   // when canvas is clicked, draw a dot
   const canvas = document.querySelector('canvas');
   const ctx = canvas.getContext('2d');
   canvas.addEventListener('click', (e) => {
      const x = e.offsetX;
      const y = e.offsetY
      ctx.beginPath();
      ctx.arc(x, y, 10, 0, 2 * Math.PI);
      ctx.fillStyle = 'red';
      ctx.fill();
   });
</script>

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