canvas = {
let canvas = d3
.create("canvas")
.attr("width", width)
.attr("height", height)
.node();
let ctx = canvas.getContext("2d");
scaleCanvas(canvas, ctx);
let scaleFactor = 10;
var line = d3
.line()
.x(function(d) {
return -scaleFactor + d.x / scaleFactor;
})
.y(function(d) {
return -scaleFactor + d.y / scaleFactor;
})
.curve(d3.curveBasis)
.context(ctx);
ctx.lineCap = "round";
simulation.on("tick", tick => {
ctx.fillStyle = "rgba(255,255,255,0.7)";
ctx.fillRect(0, 0, width, height);
ctx.save();
ctx.translate(width / 2, height / 2);
simulation.nodes().forEach(paint);
ctx.restore();
});
invalidation.then(() => simulation.stop());
function paint(d, i) {
ctx.save();
ctx.translate(d.x, d.y);
ctx.strokeStyle = "#ccc";
ctx.beginPath();
ctx.arc(0, 0, d.r, 0, Math.PI * 2);
ctx.stroke();
d.face.strokes.forEach(function(s) {
ctx.strokeStyle = "#111";
ctx.lineWidth = 1;
ctx.beginPath();
line(s);
ctx.stroke();
});
ctx.restore();
}
ctx.save();
ctx.translate(width / 2, height / 2);
simulation.nodes().forEach(paint);
ctx.restore();
return canvas;
}