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

141
Vistas
how to make a square that moves horizontally from left to right, resets and starts from a random y (105 or 35)? in javascript

for a project, I need to make, using ctx, a square that starts from a random y (35 or 105), moves from y = 0 to the end of the canvas, and gets back to the beginning, once again, 35 or 105 randomly.

It needs to move at 20 pixels every frame.

I created a function that draws a square:

function drawSquareBlue() {
            ctx.beginPath();
            ctx.fillRect(squareX, squareY, squareSize2, squareSize2);
        }

then, in the main function, I tried to start the code that moves the square:

if (squareX <= 0)
            diffY = -diffY;
            squareX = squareX + diffY;

And I didn't know what to do next. I tried setting squareX to 0, but the square gets stuck. I also don't know what to do for it to run at 20pixels/frame

about 4 years ago · Juan Pablo Isaza
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