Skip to content

Instantly share code, notes, and snippets.

@mforando
Created March 9, 2020 17:17
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 mforando/84545fa412ea76f9ba8694b2ce91b1d1 to your computer and use it in GitHub Desktop.
Save mforando/84545fa412ea76f9ba8694b2ce91b1d1 to your computer and use it in GitHub Desktop.
Cancelling Events
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
var data = d3.range(100).map((d)=>{
return {'cx':Math.random()*450 + 25,'cy':Math.random()*450 + 25}
})
var svg = d3.select("body").append("svg")
.attr("width", 500)
.attr("height", 500)
var circles = svg.selectAll("circle").data(data);
circles.enter()
.append('circle')
.attr("cx", (d)=>{return d.cx;})
.attr("cy", (d)=>{return d.cy;})
.attr("r", 10)
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut)
.on('click', handleClick)
;
function handleMouseOver(){
d3.select(this).transition().style('fill', 'red')
};
function handleMouseOut(){
d3.select(this).transition().style('fill', 'black')
};
function handleClick(){
d3.select(this).style('fill', 'green')
svg.selectAll("circle")
.on('mouseout', null)
.on('mouseover', null);
};
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment