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

94
Visualizações
add delay to iteraction

I'm building an animation with multiple drawn circles going left to right. However, the animation starts at the same time for all circles, and I need them to have a timed interval (for example 1 second) between each other. Any idea on how to manage this? I've tried setInterval without success. The drawing of circles is as follows:

function isPrime(num) {
  for(var i = 2; i < num; i++)
    if(num % i === 0) return false;
  return num > 1;
}
const settings = {
  width: 300,
  height: 930,
  radius: 13,
  gap: 5,
  circles: 30,
};
const canvas = document.getElementById("canvas");
canvas.width = settings.width;
canvas.height = settings.height;
const ctx = canvas.getContext("2d");
var randomNumber = [];


const circles = [...Array(settings.circles).keys()].map((i) => ({
  number: randomNumber[i],
  x: settings.radius,
  y: settings.radius + (settings.radius * 2 + settings.gap) * i,
  radius: settings.radius,
  dx: 100, // This is the speed in pixels per second
  dy: 0,
  isPrime: isPrime(randomNumber[i]),
}));

function drawCircle(circle) {
  i = 0;
  if (circle.number > 0 && circle.number <= 10) {
    ctx.strokeStyle = "#0b0bf1";
  } else if (circle.number > 10 && circle.number <= 20) {
    ctx.strokeStyle = "#f10b0b";
  } else if (circle.number > 20 && circle.number <= 30) {
    ctx.strokeStyle = "#0bf163";
  } else if (circle.number > 30 && circle.number <= 40) {
    ctx.strokeStyle = "#f1da0b";
  } else if (circle.number > 40 && circle.number <= 50) {
    ctx.strokeStyle = "#950bf1";
  } else if (circle.number > 50 && circle.number <= 60) {
    ctx.strokeStyle = "#0bf1e5";
  }
  ctx.beginPath();
  ctx.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2, false);
  ctx.stroke();
  ctx.fillText(circle.number, circle.x - 5, circle.y + 3);

}

function updateCircle(circle, dt) {
  circle.x = clamp(
    circle.x + circle.dx * dt,
    circle.radius + 1,
    settings.width - circle.radius - 1
  );
  circle.y = clamp(
    circle.y + circle.dy * dt,
    circle.radius + 1,
    settings.height - circle.radius - 1
  );
}

function animate() {
  ctx.clearRect(0, 0, settings.width, settings.height);
  circles.forEach(drawCircle);

  requestAnimationFrame(animate);
}
animate();
update((dt) => circles.forEach((circle) => updateCircle(circle, dt)), 50);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you want to do like

let i = 0, l = circles.length;
let intv = setInterval(()=>{
  drawCircle(circles[i++]);
  if(i === l){
    clearInterval(intv); intv = undefined;
  }
}, 100); // change interval as desired

instead of circles.forEach(drawCircle);.

That requestAnimationFrame looks pretty useless like that too.

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