score:0
Accepted answer
Here is an example that i made to understand the most basic zoom behaviour in D3.js
A canvas (svg)
is appended to the body of the HTML. Then a blue rectangle is appended and then a red rectangle is appended. The zoom behaviour is added with the line
.call(d3.behavior.zoom().on("zoom",redraw))
here the redraw function is called when D3 detects that a "zoom" was triggered on the canvas.
The code example is invoked in a "class" object in javaScript:
function ExampleZoom(){
/*** Class defining a zoomable svg. SVG AKA canvas in HTML5 ***/
var self = this;
self.margin = {top: 20, right: 20, bottom: 30, left: 40};
self.width = 960 - self.margin.left - self.margin.right;
self.height = 500 - self.margin.top - self.margin.bottom;
self.svg = d3.select("body").append("svg:svg")
.attr("width", self.width)
.attr("height", self.height)
.call(d3.behavior.zoom().on("zoom",redraw))
.append("g")
.attr("transform", "translate(" + self.margin.left + "," + self.margin.top + ")");
self.svg.append("svg:rect")
.attr("width",40)
.attr("height", 40)
.attr("y", 100)
.attr("x", 100)
.style("fill", "steelblue");
self.svg.append("svg:rect")
.attr("width",10)
.attr("height", 10)
.attr("y", 200)
.attr("x", 200)
.style("fill", "red");
//Redraw for zoom
function redraw() {
console.log("here", d3.event.translate, d3.event.scale);
self.svg.attr("transform","translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
}
}//End of class
var ex = new ExampleZoom();
Source: stackoverflow.com
Related Query
- Allow zoom with buttons only, allow pan with mouse drag
- Zoom/pan in D3 with mouse anywhere in window
- Using d3-3d with pan & zoom while retaining rotation
- d3.js pan and zoom jumps when using mouse after programatic zoom
- Adding several y axes with zoom / pan in D3js
- d3 click event is stopped with zoom and pan layer
- D3 v4 pan with mouse wheel
- Zoom with Slider and mouse scroll in d3.js
- How to implement mouse wheel zoom in D3 like Google Map with overlay and Ctrl + Scroll event?
- d3 zoom not centering on mouse position with radial tree
- Add mouse pan on left mouse button on 2D graph created with D3.js fork 3D Force-Directed Graph
- Combining a selection-rectangle with zoom + pan in a D3.js 4.x Force Simulation
- Zoom Pan images with d3.js
- performing zoom with mouse wheel programmatically for D3 parallel coordinates plot
- Add svg pan zoom to dynamically created svg with D3js
- How do I draw gridlines in d3.js with zoom and pan
- D3: zoom and pan with rect drag
- d3 zoom and pan with hover functions
- D3 Zoom & Pan - How to enable zooming & panning anywhere on the chart and restrict showing circle within the range while panning & zooming
- d3 force layout zoom and pan conflict with node drag
- d3.js Map (<svg>) Auto Fit into Parent Container and Resize with Window
- Achieving animated zoom with d3 and Leaflet
- d3 zoom and pan upgrade to version 4
- D3 Pan Zoom Overflow
- D3 v4 Brush and Zoom on the same element (without mouse events conflicting)
- d3 zoom behavior when window is resized
- nvd3.js-Line Chart with View Finder: rotate axis labels and show line values when mouse over
- d3 v4 - zoom with buttons in conflicts with zoom behaviour
- Horizontal pan with ordinal x axis in d3
- d3 zoom with touch devices sometimes behaves weirdly (using IONIC)
More Query from same tag
- How to add icons to D3 axis
- modifying d3.js donut chart to read from json array
- d3.on("mouseover") event does not work with nested SVG elements
- Adding a column to csv based on other rows with d3
- d3 transition between time groupings
- How to sort a map-of-maps (hierarchy) with D3.js
- Programmatically triggering a D3 brush event
- Crossfilter filter based on textbox
- Unable to append a rect to a SVG using d3
- dynamic js with phantomjs in local html file
- D3 line graph with date slider
- Tooltip is not showing at appended 'rect' on top of point/circle in d3 chart
- Embedded Google Map with d3.js overlay shows UI options and overlay element but NOT the map itself
- Horizontal CSS Funnel
- Horizontal Stacked Bar Chart with Two Y Axis Issues
- Golbal variable type error in d3.js. How to solve it?
- D3 pack: setting same radius for the nodes that include the different number of children
- Layering D3 Graphs: Swimlanes + Data Connections
- Throw error whenever attempting to use geoproject command in terminal
- Why does D3 not insert elements inside the selected element, when using data().enter()?
- NVD3 Stacked Area Chart
- d3.js y-axis not displaying months correctly
- D3: How to do an update pattern on normalized stacked bar chart?
- d3 - understanding the documentation for d3.svg.line
- d3.js stacked bar chart with positive and negative values
- arctween and dataset change in donut chart with d3,js
- Accessing dimensions of SVG component in Aurelia
- How to rotate D3 Chart XAxis label in vertical
- D3 chart can't update -- enter and exit property of selection both empty
- SVG path element created by d3 (d3.svg.arc) disappearing when mask is applied