Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
d3.js Zoomable forceSimulation: Limit x/y Transform on Drag

I have a d3 force simulation inside a zoom layer. Basically this example modified with further functionality: https://bl.ocks.org/puzzler10/4438752bb93f45dc5ad5214efaa12e4a

My Question is about the zoom configuration.

let transformXYLimit = 700;

var zoom_handler = d3.zoom()
    .scaleExtent([.7, 5.5])
    .on("zoom", (event) => {
        // This x/y limitation does not work properly
        event.transform.x = Math.min(transformXYLimit, Math.max(event.transform.x, -transformXYLimit));
        event.transform.y = Math.min(transformXYLimit, Math.max(event.transform.y, -transformXYLimit));
        zoomlayer.attr("transform", event.transform);
    });

zoom_handler(svg);

I have managed to limit the zoom with the "scaleExtent()" function, however i also want to limit how much the user can drag the "zoomlayer" in the x/y direction so he wont "loose" the graph into nothingness when he drags around too much. Ive tried to limit it by setting a fixed limit to the event.transform.x/y variables however combined with a higher zoom this wont let me see the whole graph. Is there any "official", proper way to limit these x/y drag coordinates?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!