I am trying to make an animation on html5 canvas which is supposed to follow a draggable div when it is moved around, something like that:
draggable_div.draggable({
drag: function(e, ui) {
canvasDrawSomethingNearDraggable(ui);
}
stop: function(e, ui) {
canvasDrawSomethingNearDraggable(ui);
}
});
function canvasDrawRectangleUnderDraggable {
for (i = 0; i < 10; i++) { // draw overlapping circles in fixed order
context.beginPath();
context.arc(x[i], y[i], 10, 0, 2 * Math.PI, false);
context.fillStyle = c[i];
context.fill();
context.stroke();
}
}
but the animation lags behind when I move the draggable element (the faster I move it the bigger the gap and it closes only when draggable stop event triggers):
Is there a way to solve or lessen this problem? Should I calculate time between drag events and make fewer calls to drawing function this way or is there a better solution?