function *canvasDrawing(grid, scale, steps = 1){
const step = scale * 5;
const size = Math.sqrt(grid.length);
const strokeCtx = DOM.context2d(size * step, size * step, 1);
const gradientCtx = DOM.context2d(size * step, size * step, 1);
const outCtx = DOM.context2d(size * step, size * step, 1);
gradientCtx.fillStyle = "white";
gradientCtx.fillRect(0, 0, size, size);
strokeCtx.lineWidth = Math.floor(scale * 2);
strokeCtx.lineCap = "round"
let x = 0, y = 0, px = 0, py = 0;;
for(let i = 0, idx = 0; i < grid.length; i++) {
gradientCtx.fillStyle = d3.interpolateSpectral(i / grid.length);
gradientCtx.fillRect(x, y, step, step);
strokeCtx.strokeStyle = d3.interpolateSpectral((grid.length - i) / grid.length);
strokeCtx.beginPath();
strokeCtx.moveTo(px + step/2, py + step/2);
strokeCtx.lineTo(x + step/2, y + step/2);
strokeCtx.stroke();
px = x;
py = y;
const direction = grid[idx] & 0b1111;
switch (direction) {
case T:
y -= step; idx -= size; break;
case R:
x += step; idx += 1; break
case B:
y += step; idx += size; break;
case L:
x -= step; idx -= 1; break;
}
if (i%steps === 0) {
outCtx.drawImage(gradientCtx.canvas, 0, 0);
outCtx.drawImage(strokeCtx.canvas, 0, 0);
yield outCtx.canvas
}
}
outCtx.drawImage(gradientCtx.canvas, 0, 0);
outCtx.drawImage(strokeCtx.canvas, 0, 0);
return outCtx.canvas;
}