Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda