Using d3-force with gka's d3.forceExtent to simulate piles of bouncy-ish circles being dropped. The idea is to use this to visualise different magnitudes.
Last active
March 6, 2018 09:29
-
-
Save tlfrd/59477ab8c350753c88d70e50a18424fb to your computer and use it in GitHub Desktop.
Force Piles
This file contains hidden or 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
| license: mit |
This file contains hidden or 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
| d3.forceExtent = function(extent) { | |
| var nodes; | |
| if (extent == null) extent=[[0,800], [0,500]]; | |
| function force() { | |
| var i, | |
| n = nodes.length, | |
| node, | |
| r = 0; | |
| for (i = 0; i < n; ++i) { | |
| node = nodes[i]; | |
| r = (node.radius || 0); | |
| node.x = Math.max(Math.min(node.x, extent[0][1]-r), extent[0][0]+r); | |
| node.y = Math.max(Math.min(node.y, extent[1][1]-r), extent[1][0]+r); | |
| } | |
| } | |
| force.initialize = function(_) { nodes = _; }; | |
| force.extent = function(_) { | |
| return arguments.length ? (extent = _, force) : extent; | |
| }; | |
| return force; | |
| }; |
This file contains hidden or 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> | |
| <head> | |
| <meta charset="utf-8"> | |
| <script src="https://d3js.org/d3.v4.min.js"></script> | |
| <script src="d3-forceextent.js"></script> | |
| <style> | |
| body { margin: 0; position: fixed; top: 0; right: 0; bottom: 0; left: 0; } | |
| body { | |
| font-family: monospace; | |
| } | |
| circle { | |
| fill: gold; | |
| stroke: black; | |
| } | |
| .coin-text { | |
| opacity: 0.5; | |
| } | |
| .counter { | |
| font-size: 20px; | |
| } | |
| .animate { | |
| color: black; | |
| position: absolute; | |
| top: 10px; | |
| left: 10px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <script> | |
| var margin = { top: 50, right: 50, bottom: 50, left: 50 }; | |
| var width = 960 - margin.left - margin.right, | |
| height = 500 - margin.top - margin.bottom; | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width + margin.left + margin.right) | |
| .attr("height", height + margin.top + margin.bottom) | |
| .append("g") | |
| .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
| var noOfCirclesA = 1; | |
| noOfCirclesB = 40; | |
| var aPos = width / 7; | |
| bPos = width - width / 4; | |
| var te = d3.easeBounce; | |
| var radius = 15; | |
| var dataA = d3.range(noOfCirclesA); | |
| dataA = dataA.map(function(d) { | |
| return { | |
| id: d | |
| } | |
| }); | |
| var dataB = d3.range(noOfCirclesB); | |
| dataB = dataB.map(function(d) { | |
| return { | |
| id: d | |
| } | |
| }); | |
| var extent = [[0, width], [0, height]]; | |
| var simulationA = d3.forceSimulation(dataA) | |
| .force("x", d3.forceX(aPos)) | |
| .force("y", d3.forceY(height)) | |
| .force("extent", d3.forceExtent(extent)) | |
| .force("collide", d3.forceCollide(radius + 2)) | |
| .stop(); | |
| var simulationB = d3.forceSimulation(dataB) | |
| .force("x", d3.forceX(bPos)) | |
| .force("y", d3.forceY(height)) | |
| .force("extent", d3.forceExtent(extent)) | |
| .force("collide", d3.forceCollide(radius + 2)) | |
| .stop(); | |
| for (var i = 0; i < 120; ++i) simulationA.tick(); | |
| for (var i = 0; i < 120; ++i) simulationB.tick(); | |
| var counterA = 0, | |
| counterB = 0; | |
| var legendA = svg.append("g") | |
| .append("text") | |
| .attr("class", "counter") | |
| .attr("x", aPos) | |
| .attr("text-anchor", "middle") | |
| .text(0); | |
| var legendB = svg.append("g") | |
| .append("text") | |
| .attr("class", "counter") | |
| .attr("x", bPos) | |
| .attr("text-anchor", "middle") | |
| .text(0); | |
| function increment(pile) { | |
| if (pile === "a") { | |
| counterA++; | |
| legendA.text(counterA); | |
| } else { | |
| counterB++; | |
| legendB.text(counterB); | |
| } | |
| } | |
| addCircles(dataA, "a"); | |
| addCircles(dataB, "b"); | |
| function addCircles(data, pile) { | |
| var circle = svg.append("g") | |
| .attr("class", "circles") | |
| .selectAll("g").data(data) | |
| .enter().append("g") | |
| .attr("transform", function(d) { | |
| return "translate(" + d.x + "," + (-margin.top - radius * 2) + ")" | |
| }); | |
| circle.append("circle") | |
| .attr("r", radius) | |
| circle.append("text") | |
| .attr("class", "coin-text") | |
| .attr("text-anchor", "middle") | |
| .attr("dy", radius / 3) | |
| .text("£"); | |
| circle.transition() | |
| .duration(750) | |
| .delay(function(d, i) { | |
| return (height - d.y) * 20; | |
| }) | |
| .ease(te) | |
| .attr("transform", function(d) { | |
| return "translate(" + d.x + ", " + d.y + ")"; | |
| }) | |
| .on("end", function() { | |
| increment(pile); | |
| }); | |
| } | |
| </script> | |
| </body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment