From ea318c5d2375dee2a2e59d679117075fe06502d7 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Tue, 23 Aug 2016 22:32:54 -0400 Subject: [PATCH] do dragging before zooming --- D3.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/D3.md b/D3.md index 12a3e41..9131c8a 100644 --- a/D3.md +++ b/D3.md @@ -216,21 +216,6 @@ use `d3.event.stopPropagation();` when events conflict ## Behaviors -### Zooming - -```javascript -//generator for a behavior -//scale from 1 - 10 -//.on function says, when there's an event of type 'zoom', call the 'zoomed' function. Could be any event -var zoom = d3.behavior.zoom().scaleExtent([1,10]).on('zoom', zoomed); -var svg = d3.select('#viz-wrapper').append('svg').call(zoom); -function zoomed(){ - console.log(d3.event.translate);//get mouse position - console.log(d3.event.scale);//bounded by 1,10 as set up above - viz.attr('transform', 'translate(' + d3.event.translate + ')' + 'scale(' + d3.event.scale + ')'); -} -``` - ### Dragging ```javascript @@ -248,6 +233,21 @@ function dragStart(d){ //d is the data for the dragged object } ``` +### Zooming + +```javascript +//generator for a behavior +//scale from 1 - 10 +//.on function says, when there's an event of type 'zoom', call the 'zoomed' function. Could be any event +var zoom = d3.behavior.zoom().scaleExtent([1,10]).on('zoom', zoomed); +var svg = d3.select('#viz-wrapper').append('svg').call(zoom); +function zoomed(){ + console.log(d3.event.translate);//get mouse position + console.log(d3.event.scale);//bounded by 1,10 as set up above + viz.attr('transform', 'translate(' + d3.event.translate + ')' + 'scale(' + d3.event.scale + ')'); +} +``` + You can use the xScale.invert and yScale.invert to get data from d3.event.x and d3.event.y