All files / core/src/RenderingEngine/GenericViewport/Planar planarProjectionFallback.ts

0% Statements 0/9
0% Branches 0/6
0% Functions 0/2
0% Lines 0/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                                                                                                       
import type { Point2 } from '../../../types';
import { normalizePlanarScale } from './planarCameraScale';
import { normalizePlanarViewState } from './planarViewState';
import type { PlanarViewState } from './PlanarViewportTypes';
 
/**
 * Derives a canvas-space pan for semantic planar state before data geometry is
 * available.
 */
export function getPlanarProjectionFallbackPan(
  viewState: PlanarViewState,
  canvasWidth: number,
  canvasHeight: number
): Point2 {
  const anchorCanvas = viewState.anchorCanvas ?? [0.5, 0.5];
  const anchorWorld = viewState.anchorWorld ?? [0, 0, 0];
  const [scaleX, scaleY] = normalizePlanarScale(viewState.scale);
 
  return [
    (0 - anchorWorld[0]) * scaleX + (anchorCanvas[0] - 0.5) * canvasWidth,
    (0 - anchorWorld[1]) * scaleY + (anchorCanvas[1] - 0.5) * canvasHeight,
  ];
}
 
/**
 * Applies a fallback canvas-space pan by moving the normalized anchor canvas.
 */
export function getPlanarProjectionFallbackViewStateWithPan(
  viewState: PlanarViewState,
  nextPan: Point2,
  canvasWidth: number,
  canvasHeight: number
): PlanarViewState {
  const currentPan = getPlanarProjectionFallbackPan(
    viewState,
    canvasWidth,
    canvasHeight
  );
  const [ax, ay] = viewState.anchorCanvas ?? [0.5, 0.5];
  const deltaX = nextPan[0] - currentPan[0];
  const deltaY = nextPan[1] - currentPan[1];
 
  return normalizePlanarViewState({
    ...viewState,
    anchorCanvas: [
      ax + deltaX / Math.max(canvasWidth, 1),
      ay + deltaY / Math.max(canvasHeight, 1),
    ],
    displayArea: undefined,
  });
}