Here lies the secret to the failure.
Last active
April 18, 2017 22:34
-
-
Save puzzler10/d98ca2993392609d55a9d4ec9d31cbd3 to your computer and use it in GitHub Desktop.
failed zoom and drag
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> | |
<meta charset="utf-8"> | |
<style> | |
</style> | |
<svg width="800" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
width = +svg.attr("width"), | |
height = +svg.attr("height"); | |
//create some circles at random points on the screen | |
//create 50 circles of radius 20 | |
//specify centre points randomly through the map function | |
var radius = 20; | |
var circle_data = d3.range(50).map(function() { | |
return{ | |
x : Math.round(Math.random() * (width - radius*2 ) + radius), | |
y : Math.round(Math.random() * (height - radius*2 ) + radius) | |
}; | |
}); | |
var rect = svg.append("g") | |
.attr("class", "rect") | |
.append("rect") | |
.attr("width", width) | |
.attr("height", height) | |
.style("fill", "black") | |
.style("pointer-events", "all") | |
; | |
//add svg circles | |
var circles = d3.select("svg") | |
.append("g") | |
.attr("class", "circles") | |
.selectAll("circle") | |
.data(circle_data) | |
.enter() | |
.append("circle") | |
.attr("cx", function(d) {return(d.x)}) | |
.attr("cy", function(d) {return(d.y)}) | |
.attr("r", radius) | |
.attr("fill", "yellow"); | |
//create drag handler with d3.drag() | |
//only interested in "drag" event listener, not "start" or "end" | |
var drag_handler = d3.drag() | |
.on("drag", drag_drag); | |
function drag_drag(d) { | |
console.log("drag triggered") | |
d3.select(this) | |
.attr("cx", d.x = d3.event.x ) | |
.attr("cy", d.y = d3.event.y ); | |
}; | |
//apply the drag_handler to our circles | |
drag_handler(circles); | |
//create zoom handler | |
var zoom_handler = d3.zoom() | |
.on("zoom", zoom_actions); | |
//specify what to do when zoom event listener is triggered | |
function zoom_actions(){ | |
circles.attr("transform", d3.event.transform); | |
} | |
//add zoom behaviour to the rectangle | |
//same as rect.call(zoom_handler); | |
zoom_handler(rect); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment