({
nodes,
target,
controlsRef,
moved,
enabled,
}: {
nodes: readonly GraphNode[];
target: CameraTarget | null;
controlsRef: RefObject<OrbitControlsImpl | null>;
/** Wird von der Steuerung gesetzt, sobald der Leser zieht, dreht oder zoomt. */
moved: RefObject<boolean>;
enabled: boolean;
})
| 399 | * bewegt hat: wer gezoomt hat, wollte genau diesen Ausschnitt. |
| 400 | */ |
| 401 | export function FitContainment({ |
| 402 | nodes, |
| 403 | target, |
| 404 | controlsRef, |
| 405 | moved, |
| 406 | enabled, |
| 407 | }: { |
| 408 | nodes: readonly GraphNode[]; |
| 409 | target: CameraTarget | null; |
| 410 | controlsRef: RefObject<OrbitControlsImpl | null>; |
| 411 | /** Wird von der Steuerung gesetzt, sobald der Leser zieht, dreht oder zoomt. */ |
| 412 | moved: RefObject<boolean>; |
| 413 | enabled: boolean; |
| 414 | }): null { |
| 415 | const camera = useThree((state) => state.camera); |
| 416 | const size = useThree((state) => state.size); |
| 417 | const armed = useRef<CameraTarget | null>(null); |
| 418 | |
| 419 | useEffect(() => { |
| 420 | if (target === null) { |
| 421 | return; |
| 422 | } |
| 423 | armed.current = target.immediate === true ? target : null; |
| 424 | moved.current = false; |
| 425 | }, [target, moved]); |
| 426 | |
| 427 | useEffect(() => { |
| 428 | const fit = armed.current; |
| 429 | if (!enabled || fit === null || moved.current || nodes.length === 0 || size.height <= 0 |
| 430 | || !(camera instanceof THREE.PerspectiveCamera)) { |
| 431 | return; |
| 432 | } |
| 433 | camera.updateMatrixWorld(); |
| 434 | const view = nodes.map((node) => |
| 435 | new THREE.Vector3(node.x, node.y, node.z).applyMatrix4(camera.matrixWorldInverse)); |
| 436 | const shift = Math.max(containShift(view, camera.fov, size.width / size.height), |
| 437 | FRAME_MIN_DISTANCE - camera.position.distanceTo(fit.lookAt)); |
| 438 | if (!(Math.abs(shift) > 0.5)) { |
| 439 | return; |
| 440 | } |
| 441 | const direction = camera.getWorldDirection(new THREE.Vector3()); |
| 442 | camera.position.addScaledVector(direction, -shift); |
| 443 | camera.updateMatrixWorld(); |
| 444 | controlsRef.current?.update(); |
| 445 | }, [nodes, enabled, camera, size, controlsRef, moved]); |
| 446 | |
| 447 | return null; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Aenderung 17: die Wurzel ist zu finden. |
nothing calls this directly
no test coverage detected