Last active
March 29, 2023 11:45
-
-
Save mootari/64ff2d2b0b68c7e1ae6c6475f1015e1c to your computer and use it in GitHub Desktop.
Zoom to fit, using D3.js 4.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const box = $groupsContainer.node().getBBox(); | |
const scale = Math.min(window.innerWidth / box.width, window.innerHeight / box.height); | |
// Reset transform. | |
let transform = d3.zoomIdentity; | |
// Center [0, 0]. | |
transform = transform.translate(window.innerWidth / 2, window.innerHeight / 2); | |
// Apply scale. | |
transform = transform.scale(scale); | |
// Center elements. | |
transform = transform.translate(-box.x - box.width / 2, -box.y - box.height / 2); | |
zoom.transform($svg, transform); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment