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

151
Visualizações
Why doesn't my code loop the animation correctly with Javascript and HTML5 canvas?

I'm super new to coding and I've been trying to figure out animations with Javascript and HTML5. I have this "loading bar" like animation where a rectangle expands until it fills up the canvas. The idea is that once the canvas is covered the box clears and starts over. Instead, the rectangle fills the canvas and just starts flickering. Any ideas?

window.onload = function() {
  var wipeWidth = 0
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext("2d");
  console.log(context);

  function drawRect() {
    context.clearRect(0,0,300,150)
    context.fillStyle = "#305ef2";
    context.rect(0, 0, wipeWidth, 150);
    context.fill()
    wipeWidth += 10
    if(wipeWidth > 300) {
      wipeWidth = 0;
    }
  }

  setInterval(drawRect,50)
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You forgot to close the path. You can use fillRect instead to avoid this.

window.onload = function() {
  var wipeWidth = 0
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext("2d");
  console.log(context);

  function drawRect() {
    context.clearRect(0, 0, 300, 150)
    context.fillStyle = "#305ef2";
    // this
    context.beginPath()
    context.rect(0, 0, wipeWidth, 150);
    context.fill()
    context.closePath()
    // or just this
    // context.fillRect(0, 0, wipeWidth, 150);
    wipeWidth += 10
    if (wipeWidth > 300) {
      wipeWidth = 0;
    }
  }

  setInterval(drawRect, 50)
}
<canvas id="canvas"></canvas>

about 4 years ago · Juan Pablo Isaza Relatório

0

fillRect, requestAnimationFrame

var wipeWidth = 0
var canvas = document.getElementById('canvas');
var context = canvas.getContext("2d");
var direction = +10;

function drawRect() {
  context.clearRect(0, 0, canvas.width, canvas.height);
  context.fillStyle = "#305ef2";
  context.fillRect(0, 0, wipeWidth, 150);
  wipeWidth += direction
  if (wipeWidth > canvas.width || wipeWidth < 0) {
    direction *= -1;
  }
  requestAnimationFrame(drawRect)
}

drawRect()
canvas {
  background: gray;
}
<canvas id="canvas" width="600" height="50"></canvas>

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