Skip to content

Instantly share code, notes, and snippets.

@ljbrown238
Created February 16, 2014 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ljbrown238/9031061 to your computer and use it in GitHub Desktop.
Save ljbrown238/9031061 to your computer and use it in GitHub Desktop.
Cross Element Event Handling
{"description":"Cross Element Event Handling","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01}
var svg = d3.select("svg");
// Create the custom event dispatch object
var dispatch = d3.dispatch("customHover");
var instructions = svg.append("text")
.attr("x",20)
.attr("y",25)
.text("Move your mouse over the blue square to see x,y values appear.");
svg.append("rect")
.attr("x",20)
.attr("y",35)
.attr("width",100)
.attr("height",100)
.attr("fill","blue")
// Here we will call the custom dispatch function when an event happens
.on("mousemove", function() { dispatch.customHover(this); });
var myText1 = svg.append("text")
.attr("x",20)
.attr("y",156)
.text("mouse x: ");
var myText2 = svg.append("text")
.attr("x",20)
.attr("y",200)
.text("mouse y: ");
// We will listen for the custom event, and when it happens, we will
// update the attributes of the two SVG text elements
dispatch.on("customHover",function(_this) {
myText1.text("mouse x: " + d3.mouse(_this)[0]);
myText2.text("mouse x: " + d3.mouse(_this)[1]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment