Skip to content

Instantly share code, notes, and snippets.

@AndrewRayCode
Created November 20, 2015 07:06
Show Gist options
  • Select an option

  • Save AndrewRayCode/e47f598d03a09bbcc862 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewRayCode/e47f598d03a09bbcc862 to your computer and use it in GitHub Desktop.
zoomAll() {
// Placeholder for calculating the bounding box of all nodes
let bounds = {
top: Infinity,
left: Infinity,
bottom: -Infinity,
right: -Infinity
};
// Find the leftmost node edge, rightmost node edge, etc
_.each( this.props.nodes, ( node ) => {
if( node.offset.x < bounds.left ) {
bounds.left = node.offset.x;
}
if( node.offset.y < bounds.top ) {
bounds.top = node.offset.y;
}
let nodeBottom = node.offset.y + node.getHeight();
if( nodeBottom > bounds.bottom ) {
bounds.bottom = nodeBottom;
}
let nodeRight = node.offset.x + node.width;
if( nodeRight > bounds.right ) {
bounds.right = nodeRight;
}
});
// Calculate the center of the bounding box
let center = {
x: ( bounds.left + bounds.right ) / 2,
y: ( bounds.top + bounds.bottom ) / 2
};
// Calculate the zoom needed to contain the bounding box. We need to check
// which dimension is bigger to account for landscape vs portrait potential
// node layout
this.props.graph.zoom = Math.min(
this.state.svgRect.width / ( bounds.right - bounds.left ),
this.state.svgRect.height / ( bounds.bottom - bounds.top )
) - 0.02; // With some padding
// Zoom to our desired level...
this.context.executeAction( zoomGraph, {
shaderId: this.props.shaderId,
zoom: this.props.graph.zoom
});
// Then center on the middle of the bounding box
this.centerOn( center );
if( global.debug ) {
this.setState({ bounds });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment