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

226
Vistas
¿Por qué esto solo gira y no se mueve hacia la derecha?

Tengo la siguiente pagina web:

 var box_x = 30; var box_y = 30; var box_width = 30; var box_height = 30; var box_direction = 20; // Degrees clockwise function draw_box() { var c = document.getElementById("area"); var ctx = c.getContext("2d"); ctx.clearRect(0, 0, 500, 500); ctx.rotate(box_direction * Math.PI / 180); ctx.fillRect(box_x, box_y, 100, 50); box_x += 2; } setInterval(draw_box, 1000);
 <h1>Welcome</h1> <canvas id="area" width="500px" height="500px" style="border: 1px solid #0000aa" /> </canvas>

¿Por qué la caja no se mueve simplemente hacia la derecha?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

ctx.rotate gira todo el lienzo desde la esquina superior izquierda, por lo que primero debe usar ctx.translate para mover la esquina superior izquierda al pivote (que es la esquina superior izquierda del cuadro) y luego hacer lo mismo pero al revés para restablecer la rotación y traslación del lienzo

 var box_x = 30; var box_y = 30; var box_width = 30; var box_height = 30; var box_direction = 20; // Degrees clockwise function draw_box() { var c = document.getElementById("area"); var ctx = c.getContext("2d"); ctx.clearRect(0, 0, 500, 500); ctx.translate(box_x, box_y); ctx.rotate(box_direction * Math.PI / 180); ctx.fillRect(0, 0, 100, 50); ctx.rotate(-box_direction * Math.PI / 180); ctx.translate(-box_x, -box_y); box_x += 2; } setInterval(draw_box, 1000);
 <h1>Welcome</h1> <canvas id="area" width="500px" height="500px" style="border: 1px solid #0000aa" /> </canvas>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Porque tienes ctx.rotate(box_direction * Math.PI / 180); . Esto está girando con la caja.

 <html> <head> <title>Rectangles wandering around aminlessly</title> </head> <body <h1>Welcome</h1> <canvas id="area" width="500px" height="500px" style="border: 1px solid #0000aa"/> </canvas> <script lang="javascript"> var box_x = 30; var box_y = 30; var box_width = 30; var box_height = 30; var box_direction = 20; // Degrees clockwise function draw_box() { var c = document.getElementById("area"); var ctx = c.getContext("2d"); ctx.clearRect(0, 0, 500, 500); ctx.fillRect(box_x, box_y, 100, 50); box_x += 2; } setInterval(draw_box, 1000); </script> </body>

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