Note that there are some explanatory texts on larger screens.

plurals
  1. POInteracting forces with D3.js
    primarykey
    data
    text
    <p>I'm trying to 'simulate' gravitational forces on particles as they move towards a 'focal' point. Practically speaking, I am trying to modify the following code so that the particles are pulled slightly off-course by the orange node on their way to the blue node. My problem is that I am having trouble conceptualising this using D3.js force directed layouts. I realise this is a pretty vague question, but any help is greatly appreciated! Image and code are below:</p> <p><img src="https://i.stack.imgur.com/QtAjm.gif" alt="enter image description here"></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8"/&gt; &lt;title&gt;Force Layouts - Quantitative Foci&lt;/title&gt; &lt;script type="text/javascript" src="http://d3js.org/d3.v2.min.js?2.8.1"&gt;&lt;/script&gt; &lt;style type="text/css"&gt; circle { stroke: #fff; } svg { fill: #fff; stroke: #000; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="body"&gt; &lt;div id="chart"&gt;&lt;/div&gt; &lt;/div&gt; &lt;script type="text/javascript"&gt; var w = 1280, h = 800, color = d3.scale.category10(); var force = d3.layout.force() .gravity(0) .charge(-5) .linkStrength(0) .size([w, h]); var links = force.links(), nodes = force.nodes(), centers = [ {type: 0, x: 3 * w / 6, y: 2 * h / 6, fixed: true}, {type: 1, x: 4 * w / 6, y: 4 * h / 6, fixed: true} ]; var svg = d3.select("#chart").append("svg:svg") .attr("width", w) .attr("height", h); svg.append("svg:rect") .attr("width", w) .attr("height", h); svg.selectAll("circle") .data(centers) .enter().append("svg:circle") .attr("r", 12) .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }) .style("fill", fill) .call(force.drag); force.on("tick", function(e) { var k = e.alpha * .1; nodes.forEach(function(node) { var center = centers[node.type]; node.x += (center.x - node.x) * k; node.y += (center.y - node.y) * k; }); svg.selectAll("circle") .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }); svg.selectAll("line") .attr("x1", function(d) { return d.source.x; }) .attr("y1", function(d) { return d.source.y; }) .attr("x2", function(d) { return d.target.x; }) .attr("y2", function(d) { return d.target.y; }); }); var p0; svg.on("mousemove", function() { var p1 = d3.svg.mouse(this), a = {type: 0, x: p1[0], y: p1[1], px: (p0 || (p0 = p1))[0], py: p0[1]}, b = {type: 1, x: centers[1].x, y: centers[1].y, fixed:true}, link = {source: a, target: b}; p0 = p1; svg.selectAll() .data([a, b]) .enter().append("svg:circle") .attr("cx", function(d) { return d.x; }) .attr("cy", function(d) { return d.y; }) .attr("r", 4.5) .style("fill", fill) .transition() .delay(3000) .attr("r", 1e-6) .remove(); svg.insert("svg:line", "circle") .data([link]) .transition() .delay(3000) .each("end", function() { nodes.splice(nodes.indexOf(a), 1); nodes.splice(nodes.indexOf(b), 1); links.splice(links.indexOf(link), 1); }) .remove(); nodes.push(a, b); links.push(link); force.start(); }); function fill(d) { return color(d.type); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><a href="https://i.stack.imgur.com/QtAjm.gif" rel="nofollow noreferrer">1</a> <a href="http://jsfiddle.net/fbW7T/1/" rel="nofollow noreferrer">http://jsfiddle.net/fbW7T/1/</a> -- before.</p> <p>[2] <a href="http://jsfiddle.net/fbW7T/2/" rel="nofollow noreferrer">http://jsfiddle.net/fbW7T/2/</a> -- after, with Lephix' proposal.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload