I need to draw basic geometric shapes progressively. That is, when I draw a line, you need to see it extend as in an animation. There are several questions here on how to do it, and I adopted this approach.
My problems start when some lines have been drawn already, and after the user clicks a button, several more lines should be drawn. The drawing of the new lines happens in a very uneven way.
I have made a jsfiddle to illustrate. It starts with two lines being drawn properly. Then, if the user clicks 'Draw next', two more lines are drawn, but the drawing is not smooth.
I have been trying to debug, and there is some trouble with the step function of jquery's animate.
var stepCount = 0;
$('path[fill*="none"]').animate({
'to': 1
}, {
duration: duration,
step: function(pos, fx) {
//console.log("fx = ", fx);
stepCount += 1;
var offset = length * fx.pos;
var subpath = line.getSubpath(0, offset);
canvas.path(subpath).attr({
'stroke-width': 2,
stroke: "black"
});
},
complete: function() {
console.log("stepCount = ", stepCount);
}
});
I have counted the steps, and logged them for each line in the complete function:
stepCount = 139
stepCount = 140
stepCount = 278
stepCount = 1261
stepCount = 1262
....
stepCount = 2318
so the first two stepCounts, represent the first two lines. 139, 140, which is reasonable. Then the third line gets a count of 278 - double the amount of steps. But for the fourth, it goes haywire, it prints out a long list of stepCounts from 1261 to 2318.
I could use some help with this :-)
correction: the third stepCount log happens after the first two lines have been drawn, and before the user clicks 'Draw next'.
solved: ok, still using the approach of progressively drawing ever longer subpaths, I modified the jsfiddle by using melc's solution. I also solved this unholy path mess in the DOM which David Fregoli pointed out. On every iteration of the step function, I remove the previous subpath. And finally, in the complete function I show the original path. This not only makes for a clean DOM, but also solves some anti-aliasing problems.