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?