function animatePath2(path, context, speed) {
return new Promise((resolve) => {
const distancePerFrame = speed / 60;
function draw(prevX, prevY, nextX, nextY) {
const s = t([prevX, prevY])
const e = t([nextX, nextY])
context.lineTo(e[0], e[1]);
}
function frameDraw(coords) {
coords.forEach((coord, index) => {
if (index <= coords.length - 2) {
draw(coords[index][0], coords[index][1], coords[index + 1][0], coords[index + 1][1])
}
});
}
const chunks = [];
let reducedPath = path;
let chunking = true;
while (chunking) {
const c = chunkPath(reducedPath, distancePerFrame);
chunks.push(c.chunk);
reducedPath = c.reducedPath;
chunking = c.end === false;
}
let chunkIndex = 0;
function chunkLoop() {
frameDraw(chunks[chunkIndex]);
if (chunkIndex + 1 < chunks.length) {
context.stroke();
window.requestAnimationFrame(chunkLoop);
chunkIndex = chunkIndex + 1;
} else {
context.fillStyle = '#2D3142'
ctx.fill();
ctx.globalCompositeOperation = 'source-atop';
context.shadowBlur = 25;
context.shadowColor = '#000000';
context.beginPath();
context.moveTo(...t(path[0][0], path[0][0]));
frameDraw(path);
ctx.lineWidth = 2.5
ctx.strokeStyle = '#131313'
context.stroke();
context.stroke();
context.closePath();
ctx.globalCompositeOperation = 'source-over';
context.shadowBlur = null;
context.shadowColor = null;
ctx.lineWidth = 1
context.stroke();
ctx.lineWidth = c.height / parseInt(c.style.height.substring(0, c.style.height.length - 1), 10);
resolve();
}
highlight(chunks[chunkIndex]);
}
context.beginPath();
context.moveTo(...t(chunks[0][0][0], chunks[0][0][1]));
chunkLoop();
});
}