I want to draw a certain path step by step, with a certain time period between any two steps.
The code I have for now is
let canvas = document.getElementById("c");
let ctx = canvas.getContext('2d');
let p = 'M10 10 h 80 v 80 h -80 Z';
let pathArr = ["M10", "10 h", "80 v", "80 h", "-80 Z"];
let lgt = pathArr.length;
let i = 0;
(function draw() {
if(i < lgt){
let pathElem = new Path2D(pathArr[i]);
ctx.stroke(pathElem);
i++;
setTimeout(draw, 200);
}
})();
The intended function is to make what ctx.stroke(p)
would do, but step by step, like stroking the elements of pathArr
one at a time, with a delay of 200ms between every two strokes.
But, this code doesn't work as intended. What am I doing wrong, and what should I do to make it work as desired?