function render(t){ var intersections = allIntersections(lines) lines.forEach(function(d){ d.count = 0 }) intersections.forEach(function(i){ i.lines[0].count++, i.lines[1].count++ }) intersections.forEach(function(i){ i.count = Math.max(i.lines[0].count, i.lines[1].count) }) ctx.fillStyle = 'rgba(0, 0, 0, .01)' ctx.fillRect(0, 0, width, height) ctx.beginPath() lines.forEach(function(d){ ctx.moveTo(d[0][0], d[0][1]) ctx.lineTo(d[1][0], d[1][1]) }) ctx.strokeStyle = 'rgba(0,0,0,.1)' ctx.lineWidth = 1 ctx.stroke() intersections.forEach(function(i){ ctx.fillStyle = '#fff' ctx.fillStyle = color(i.count) var r = 1.5 ctx.beginPath() ctx.moveTo(i[0] + r, i[1]) ctx.arc(i[0], i[1], r, 0, 2 * Math.PI) ctx.fill() }) } render() if (window.timer) timer.stop() window.timer = d3.timer(function(t){ points.forEach(function(d){ var s = 1 d[0] += d.dx*s d[1] += d.dy*s if (d[0] < 0 && d.dx < 0 || d[0] > width && d.dx > 0) d.dx = -d.dx if (d[1] < 0 && d.dy < 0 || d[1] > height && d.dy > 0) d.dy = -d.dy }) render(t) })