Mouse Wheel Zoom Map - DataMaps.js

I am using DataMaps.js global map . I want to implement Mouse Zoom when the mouse wheel is moving. An example of static scaling:

var zoom = new Datamap({
  element: document.getElementById("zoom_map"),
  scope: 'world',
  // Zoom in on Africa
  setProjection: function(element) {
    var projection = d3.geo.equirectangular()
      .center([23, -3])
      .rotate([4.4, 0])
      .scale(400)
      .translate([element.offsetWidth / 2, element.offsetHeight / 2]);
    var path = d3.geo.path()
      .projection(projection);

    return {path: path, projection: projection};
  }
});

      

Also, I have a Mouse Wheel event:

$('#zoom_map').bind('DOMMouseScroll mousewheel', function(e){
    if (e.originalEvent.wheelDelta > 0 || e.originalEvent.detail < 0) {
        console.log("+");
        e.preventDefault();
    }
    else{
        console.log("-");
        e.preventDefault();
    }
});

      

I tried to combine these parts. Also, I tried to modify datamaps.js . But unfortunately, I will fail.

+3


source to share


1 answer


you can use option done

to make a callback of the zoom function



done: function(datamap){
    datamap.svg.call(d3.behavior.zoom().on("zoom", redraw));
    function redraw() {
        datamap.svg.selectAll("g").attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
    }
}

      

+11


source







All Articles