From b7e20a43104e83f8c4a5e0ce8ef38890286962c7 Mon Sep 17 00:00:00 2001 From: Jason Davies Date: Wed, 15 Jun 2011 23:20:14 +0100 Subject: [PATCH] Couple of tweaks to reset gravity. Firstly, when the mouse exits the area the gravity coordinates are nullified. Secondly, if the user switches to another window, the gravity coordinates are nullified (otherwise the appropriate "mouseout" event may be missed). --- examples/voroboids/voroboids.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/voroboids/voroboids.js b/examples/voroboids/voroboids.js index 5e98cae9deb0e..fa12c89671fa6 100644 --- a/examples/voroboids/voroboids.js +++ b/examples/voroboids/voroboids.js @@ -1,6 +1,6 @@ var w = 960, h = 500, - mouse = [0, 0], + mouse = [null, null], fill = d3.scale.linear().domain([0, 1e4]).range(["brown", "steelblue"]); // Initialise boids. @@ -16,6 +16,8 @@ var vertices = boids.map(function(boid) { return boid(boids); }); +d3.select(window).on("blur", nullGravity); + var svg = d3.select("#vis") .append("svg:svg") .attr("width", w) @@ -25,7 +27,8 @@ var svg = d3.select("#vis") var m = d3.svg.mouse(this); mouse[0] = m[0]; mouse[1] = m[1]; - }); + }) + .on("mouseout", nullGravity); svg.selectAll("path") .data(d3.geom.voronoi(vertices)) @@ -56,3 +59,7 @@ d3.timer(function() { .attr("d", function(d) { return "M" + d.join("L") + "Z"; }) .style("fill", function(d) { return fill((d3.geom.polygon(d).area())); }); }); + +function nullGravity() { + mouse[0] = mouse[1] = null; +}