Skip to content

Commit

Permalink
feat(#20): add zoomToFit() function
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMartiniMo committed Feb 7, 2020
1 parent eb1bb87 commit f57859c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/dtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ const dTree = {
if (node) {
_zoomTo(node.x, node.y, zoom, duration)
}
},
zoomToFit: function (duration = 500) {
const groupBounds = treeBuilder.g.node().getBBox()
const width = groupBounds.width
const height = groupBounds.height
const fullWidth = treeBuilder.svg.node().clientWidth
const fullHeight = treeBuilder.svg.node().clientHeight
const scale = 0.95 / Math.max(width / fullWidth, height / fullHeight)

treeBuilder.svg
.transition()
.duration(duration)
.call(
treeBuilder.zoom.transform,
d3.zoomIdentity
.translate(
fullWidth / 2 - scale * (groupBounds.x + width / 2),
fullHeight / 2 - scale * (groupBounds.y + height / 2)
)
.scale(scale)
)
}
}
},
Expand Down

0 comments on commit f57859c

Please sign in to comment.