Skip to content

Commit

Permalink
Couple of tweaks to reset gravity.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jasondavies committed Jun 15, 2011
1 parent e2119b8 commit b7e20a4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions examples/voroboids/voroboids.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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;
}

0 comments on commit b7e20a4

Please sign in to comment.