Skip to content

Instantly share code, notes, and snippets.

@wisedier
Created February 5, 2020 17:48
Show Gist options
  • Save wisedier/06006f3f71da10fc700758436fc8d3f0 to your computer and use it in GitHub Desktop.
Save wisedier/06006f3f71da10fc700758436fc8d3f0 to your computer and use it in GitHub Desktop.
d3 zoom
interface GraphNode {
id: string | number;
type: DrawerTypes;
scale: number;
hover: boolean;
active: boolean;
neighbors: (D3GraphNode | GraphEdge)[];
}
interface D3GraphNode extends d3.SimulationNodeDatum, GraphNode {
}
const focus = (container: HTMLDivElement, d: D3GraphNode) => {
const root = d3.select(`.${styles.root}`);
const bbox = (d3.select(`[data-id="${d.id}"]`).node() as SVGGraphicsElement).getBBox();
const t0 = d3.zoomTransform(root.node() as any);
const x = Math.round(container.offsetWidth / 2) / t0.k - (d.x! - Math.round(bbox.width / 2));
const y = Math.round(container.offsetHeight / 2) / t0.k - (d.y! - Math.round(bbox.height / 2));
const t1 = d3.zoomIdentity.scale(t0.k).translate(x, y);
root.call(zoom.duration(1000).transform, t1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment