1

I'm working on a simple web animation. Thing is, I was used to work on C code, and delay my functions using sleep() and usleep(). ATM, I'm trying to write a function that slowly draws a rectangle by expanding it. Thing is, it gets done in no time, while I'd like it to be seen as an animation (the code below is much clearer than my explanations).

JavaScript

function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
    if (typeof stroke == "undefined")
    stroke = true;
    if (typeof radius === "undefined")
    radius = 5;
    ctx.beginPath();
    ctx.moveTo(x + radius, y);
    ctx.lineTo(x + width - radius, y);
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
    ctx.lineTo(x + width, y + height - radius);
    ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
    ctx.lineTo(x + radius, y + height);
    ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
    ctx.lineTo(x, y + radius);
    ctx.quadraticCurveTo(x, y, x + radius, y);
    ctx.closePath();
    if (fill)
    ctx.fill();
}

function expand(width, height, ctx) {
    var i;
    var j;

    i = 20;
    j = 20;
    while (i < width) {
        roundRect(ctx, 0, 90, i, j, 5, "ForestGreen", "ForestGreen");
        i++;
    }
    while (j < height - 50) {
        roundRect(ctx, 0, 90, i, j, 5, "ForestGreen", "ForestGreen");
        j++;
    }
}

Do you guys know how I could handle this?

Ex-iT
  • 1,479
  • 2
  • 12
  • 20
Pierre Olivier Tran
  • 1,669
  • 4
  • 24
  • 40
  • Take a look at http://stackoverflow.com/questions/1447407/whats-the-equivalent-of-javas-thread-sleep-in-javascript – dachi Mar 01 '14 at 16:09
  • 2
    Not directly related to your question... but search on the web for "SVG and CSS animations", depending on your requirements it can be more convenient than using the canvas directly. (your animations will get hardware acceleration if the browser supports them :) ) – Diego Mar 01 '14 at 16:13
  • sorry for wasting your time. – Ace of Spade Sep 13 '18 at 19:13

1 Answers1

0

I guess you're searching for

va ms = 1000; //1 second
setTimeout(function(){}, ms);
steo
  • 4,586
  • 2
  • 33
  • 64