Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
d3js How to enable drag or pinch/zoom function to my map

I am working on a D3js project where I have this map with my data.

What I want to do is define my zoom method to zoom direct to the center of the SVG, not the bottom right corner. Or at least I want to have the ability to drag the whole SVG after zooming in, that would be less pain because the user would be able to see their own country.

const width = 700;
const height = 500;
const svg = d3.select('section').append('svg').attr('width', width).attr('height', height);
const projection = d3.geoMercator().scale(100).translate([width / 2, height / 1.4]);
const path = d3.geoPath(projection);
const g = svg.append('g');

d3.json('https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json').then(data => {
  const countries = topojson.feature(data, data.objects.countries);
  g.selectAll('path').data(countries.features).enter().append('path').attr('class', 'country').attr('d', path);

  var zoom = d3.zoom().on('zoom', function() {
    svg.attr('transform', d3.event.transform);
  })

  d3.select('#zoomin').on('click', function() {
    zoom.scaleBy(svg.transition().duration(750), 1.3);
  });

  d3.select('#zoomout').on('click', function() {
    zoom.scaleBy(svg.transition().duration(750), 1 / 1.3);
  });
});
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/3.0.2/topojson.min.js" integrity="sha512-4UKI/XKm3xrvJ6pZS5oTRvIQGIzZFoXR71rRBb1y2N+PbwAsKa5tPl2J6WvbEvwN3TxQCm8hMzsl/pO+82iRlg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<section>
</section>

<button id="zoomin">+</button>
<button id "zoomout">-</button>

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda