D3.js 4.x の forceSimulation を canvas で実行するデモ
Created
January 11, 2017 12:07
-
-
Save frogcat/a06132f64b7164c1b1993c49dcd9178f to your computer and use it in GitHub Desktop.
D3.js forceSimulation with canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Graph(canvas) { | |
var that = this; | |
this.canvas = canvas; | |
this.nodes = []; | |
this.links = []; | |
this.radius = 20; | |
this.distance = 80; | |
this.simulation = d3.forceSimulation(); | |
this.simulation.force("link", d3.forceLink().distance(function(d) { | |
return that.distance; | |
})); | |
this.simulation.force("charge", d3.forceManyBody()); | |
this.simulation.force("center", d3.forceCenter(canvas.width / 2, canvas.height / 2)); | |
this.simulation.on("tick", function() { | |
that.paint(); | |
}); | |
d3.select(canvas) | |
.call(d3.drag() | |
.container(canvas) | |
.subject(function() { | |
return that.simulation.find(d3.event.x, d3.event.y, that.radius); | |
}) | |
.on("start", function() { | |
if (!d3.event.active) that.simulation.alphaTarget(0.3).restart(); | |
d3.event.subject.fx = d3.event.subject.x; | |
d3.event.subject.fy = d3.event.subject.y; | |
}) | |
.on("drag", function() { | |
d3.event.subject.fx = d3.event.x; | |
d3.event.subject.fy = d3.event.y; | |
}) | |
.on("end", function() { | |
if (!d3.event.active) that.simulation.alphaTarget(0); | |
d3.event.subject.fx = null; | |
d3.event.subject.fy = null; | |
})); | |
} | |
Graph.prototype.setSize = function(width, height) { | |
this.simulation.force("center", d3.forceCenter(width / 2, height / 2)); | |
this.simulation.alphaTarget(0).restart(); | |
}; | |
Graph.prototype.getNodeById = function(id) { | |
return this.nodes.find(function(a) { | |
return a.id === id; | |
}); | |
}; | |
Graph.prototype.getLinkByIds = function(src, dst) { | |
return this.links.find(function(a) { | |
return (a.src === src && a.dst === dst) || (a.src === dst && a.dst === src); | |
}); | |
}; | |
Graph.prototype.add = function(src, dst) { | |
if (!this.getNodeById(src)) | |
this.nodes.push({ | |
id: src, | |
x: this.canvas.width * Math.random(), | |
y: this.canvas.height * Math.random() | |
}); | |
if (!this.getNodeById(dst)) | |
this.nodes.push({ | |
id: dst, | |
x: this.canvas.width * Math.random(), | |
y: this.canvas.height * Math.random() | |
}); | |
if (src !== dst && !this.getLinkByIds(src, dst)) | |
this.links.push({ | |
source: this.getNodeById(src), | |
target: this.getNodeById(dst) | |
}); | |
this.simulation.nodes(this.nodes); | |
this.simulation.force("link").links(this.links); | |
this.simulation.alphaTarget(0.3).restart(); | |
return this; | |
}; | |
Graph.prototype.paint = function() { | |
var c = this.canvas.getContext("2d"); | |
var r = this.radius; | |
// clear | |
c.clearRect(0, 0, this.canvas.width, this.canvas.height); | |
c.save(); | |
// 背面にリンクを描画 | |
this.links.forEach(function(d) { | |
c.strokeStyle = "#000000"; | |
c.lineWidth = 3; | |
c.beginPath(); | |
c.moveTo(d.source.x, d.source.y); | |
c.lineTo(d.target.x, d.target.y); | |
c.stroke(); | |
}); | |
// 前面にノードを描画 | |
this.nodes.forEach(function(d) { | |
// circle | |
c.fillStyle = "#ffffff"; | |
c.strokeStyle = "#000000"; | |
c.lineWidth = 3; | |
c.beginPath(); | |
c.moveTo(d.x + r, d.y); | |
c.arc(d.x, d.y, r, 0, 2 * Math.PI); | |
c.fill(); | |
c.stroke(); | |
// text | |
c.fillStyle = "#000"; | |
c.font = "24px sans-serif"; | |
c.fillText(d.id, d.x - r / 2, d.y + r / 2); | |
}); | |
// 反映する | |
c.restore(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>D3.js and Graph</title> | |
<script src="https://unpkg.com/d3@4.4.0/build/d3.min.js"></script> | |
<script src="graph.js"></script> | |
<style> | |
canvas { | |
display: block; | |
position: absolute; | |
top: 0; | |
left: 0; | |
bottom: 0; | |
right: 0; | |
background: #ccc; | |
} | |
</style> | |
</head> | |
<body> | |
<canvas id="canvas" width="800" height="600"></canvas> | |
<script> | |
var canvas = document.getElementById("canvas"); | |
canvas.width = window.innerWidth; | |
canvas.height = window.innerHeight; | |
var graph = new Graph(canvas); | |
var fire = function() { | |
var a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
var src = a[Math.floor(a.length * Math.random())]; | |
var dst = a[Math.floor(a.length * Math.random())]; | |
graph.add(src, dst); | |
}; | |
canvas.addEventListener("click", fire); | |
setInterval(fire, 1000); | |
window.addEventListener("resize", function() { | |
canvas.width = window.innerWidth; | |
canvas.height = window.innerHeight; | |
graph.setSize(canvas.width, canvas.height); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment