Built with blockbuilder.org
Created
March 9, 2020 17:17
-
-
Save mforando/92b71bcb243ca5e2d8b453bbc7e0ea95 to your computer and use it in GitHub Desktop.
Cancelling Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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