All files / tools/src/utilities/spatial getDisplayedCanvasSize.ts

0% Statements 0/5
0% Branches 0/10
0% Functions 0/1
0% Lines 0/5

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                                                         
import { utilities as csUtils } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
/**
 * Returns the displayed canvas size for a viewport, in the same coordinate
 * space that `viewport.worldToCanvas` returns points in.
 *
 * Native ("generic") viewports render to a separate visible canvas; their
 * `viewport.canvas` is a hidden cornerstone-canvas whose client size is 0,
 * which would collapse geometry computed from it. worldToCanvas already
 * returns coordinates in the element's displayed space for those viewports,
 * so the element size is used instead.
 */
export default function getDisplayedCanvasSize(viewport: Types.IViewport): {
  clientWidth: number;
  clientHeight: number;
} {
  if (csUtils.isGenericViewport(viewport)) {
    const { element } = viewport;
    return {
      clientWidth: element?.clientWidth ?? 0,
      clientHeight: element?.clientHeight ?? 0,
    };
  }
 
  const { clientWidth = 0, clientHeight = 0 } = viewport.canvas ?? {};
  return { clientWidth, clientHeight };
}