Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!