Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda