MCPcopy Create free account
hub / github.com/ScriptedAlchemy/tracedecay / fitCameraToNodes

Function fitCameraToNodes

dashboard/graph/src/canvasHelpers.ts:106–138  ·  view source on GitHub ↗
(
  size: CanvasSize,
  sim: FramedSimulation,
  camera: Camera,
  smooth = false,
)

Source from the content-addressed store, hash-verified

104 * target instead of snapping, for per-frame tracking while the layout settles.
105 */
106export function fitCameraToNodes(
107 size: CanvasSize,
108 sim: FramedSimulation,
109 camera: Camera,
110 smooth = false,
111) {
112 if (sim.nodes.length === 0) return;
113 let minX = Infinity;
114 let minY = Infinity;
115 let maxX = -Infinity;
116 let maxY = -Infinity;
117 for (const node of sim.nodes) {
118 minX = Math.min(minX, node.x - node.radius);
119 minY = Math.min(minY, node.y - node.radius);
120 maxX = Math.max(maxX, node.x + node.radius);
121 maxY = Math.max(maxY, node.y + node.radius);
122 }
123 const spanX = Math.max(1, maxX - minX);
124 const spanY = Math.max(1, maxY - minY);
125 const fitK = Math.min(size.width / spanX, size.height / spanY) * 0.88;
126 const targetX = (minX + maxX) / 2;
127 const targetY = (minY + maxY) / 2;
128 const targetK = Math.min(5, Math.max(0.12, fitK));
129 if (smooth) {
130 camera.x += (targetX - camera.x) * 0.2;
131 camera.y += (targetY - camera.y) * 0.2;
132 camera.k += (targetK - camera.k) * 0.2;
133 } else {
134 camera.x = targetX;
135 camera.y = targetY;
136 camera.k = targetK;
137 }
138}
139
140export function toWorldPoint(
141 camera: Camera,

Callers 2

GraphCanvasFunction · 0.90
frameFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected