All files / packages/core/src/RenderingEngine/helpers/cpuFallback/rendering resize.ts

0% Statements 0/27
0% Branches 0/17
0% Functions 0/4
0% Lines 0/27

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                                                                                                                                                                                                                           
import fitToWindow from './fitToWindow';
import getImageSize from './getImageSize';
import { CPUFallbackEnabledElement } from '../../../../types';
 
/**
 * This module is responsible for enabling an element to display images with cornerstone
 *
 * @param {HTMLDivElement} element The DOM element enabled for Cornerstone
 * @param {HTMLDivElement} canvas The Canvas DOM element within the DOM element enabled for Cornerstone
 * @returns {void}
 */
function setCanvasSize(enabledElement: CPUFallbackEnabledElement) {
  const { canvas } = enabledElement;
  const { clientWidth, clientHeight } = canvas;
 
  // Set the canvas to be same resolution as the client.
  if (canvas.width !== clientWidth || canvas.height !== clientHeight) {
    canvas.width = clientWidth;
    canvas.height = clientHeight;
  }
}
 
/**
 * Checks if the image of a given enabled element fitted the window
 * before the resize
 *
 * @param {EnabledElement} enabledElement The Cornerstone Enabled Element
 * @param {number} oldCanvasWidth The width of the canvas before the resize
 * @param {number} oldCanvasHeight The height of the canvas before the resize
 * @return {Boolean} true if it fitted the windows, false otherwise
 */
function wasFitToWindow(
  enabledElement: CPUFallbackEnabledElement,
  oldCanvasWidth: number,
  oldCanvasHeight: number
): boolean {
  const scale = enabledElement.viewport.scale;
  const imageSize = getImageSize(
    enabledElement.image,
    enabledElement.viewport.rotation
  );
  const imageWidth = Math.round(imageSize.width * scale);
  const imageHeight = Math.round(imageSize.height * scale);
  const x = enabledElement.viewport.translation.x;
  const y = enabledElement.viewport.translation.y;
 
  return (
    (imageWidth === oldCanvasWidth && imageHeight <= oldCanvasHeight) ||
    (imageWidth <= oldCanvasWidth &&
      imageHeight === oldCanvasHeight &&
      x === 0 &&
      y === 0)
  );
}
 
/**
 * Rescale the image relative to the changed size of the canvas
 *
 * @param {EnabledElement} enabledElement The Cornerstone Enabled Element
 * @param {number} oldCanvasWidth The width of the canvas before the resize
 * @param {number} oldCanvasHeight The height of the canvas before the resize
 * @return {void}
 */
function relativeRescale(
  enabledElement: CPUFallbackEnabledElement,
  oldCanvasWidth: number,
  oldCanvasHeight: number
): void {
  const scale = enabledElement.viewport.scale;
  const canvasWidth = enabledElement.canvas.width;
  const canvasHeight = enabledElement.canvas.height;
  const relWidthChange = canvasWidth / oldCanvasWidth;
  const relHeightChange = canvasHeight / oldCanvasHeight;
  const relChange = Math.sqrt(relWidthChange * relHeightChange);
 
  enabledElement.viewport.scale = relChange * scale;
}
 
/**
 * Resizes an enabled element and optionally fits the image to window
 *
 * @param {HTMLDivElement} element The DOM element enabled for Cornerstone
 * @param {Boolean} forceFitToWindow true to to force a refit, false to rescale accordingly
 * @returns {void}
 */
export default function (
  enabledElement: CPUFallbackEnabledElement,
  forceFitToWindow = false
): void {
  const oldCanvasWidth = enabledElement.canvas.width;
  const oldCanvasHeight = enabledElement.canvas.height;
 
  setCanvasSize(enabledElement);
 
  if (enabledElement.image === undefined) {
    return;
  }
 
  if (
    forceFitToWindow ||
    wasFitToWindow(enabledElement, oldCanvasWidth, oldCanvasHeight)
  ) {
    // Fit the image to the window again if it fitted before the resize
    fitToWindow(enabledElement);
  } else {
    // Adapt the scale of a zoomed or panned image relative to the size change
    relativeRescale(enabledElement, oldCanvasWidth, oldCanvasHeight);
  }
}