All files / packages/tools/src/drawingSvg drawRectByCoordinates.ts

100% Statements 20/20
50% Branches 4/8
100% Functions 1/1
100% Lines 20/20

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                                        13x                       13x   13x 13x 13x   13x   13x 13x         13x       13x         13x       13x                       13x 2x   2x   11x   11x 11x     11x   11x      
import type { Types } from '@cornerstonejs/core';
 
import _getHash from './_getHash';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import { SVGDrawingHelper } from '../types';
 
export default function drawRectByCoordinates(
  svgDrawingHelper: SVGDrawingHelper,
  annotationUID: string,
  rectangleUID: string,
  canvasCoordinates: Types.Point2[],
  options = {},
  dataId = ''
): void {
  const {
    color,
    width: _width,
    lineWidth,
    lineDash,
  } = Object.assign(
    {
      color: 'rgb(0, 255, 0)',
      width: '2',
      lineWidth: undefined,
      lineDash: undefined,
    },
    options
  );
 
  // for supporting both lineWidth and width options
 
  const strokeWidth = lineWidth || _width;
 
  const svgns = 'http://www.w3.org/2000/svg';
  const svgNodeHash = _getHash(annotationUID, 'rect', rectangleUID);
  const existingRect = svgDrawingHelper.getSvgNode(svgNodeHash);
 
  const [topLeft, topRight, bottomLeft, bottomRight] = canvasCoordinates;
 
  const width = Math.hypot(topLeft[0] - topRight[0], topLeft[1] - topRight[1]);
  const height = Math.hypot(
    topLeft[0] - bottomLeft[0],
    topLeft[1] - bottomLeft[1]
  );
 
  const center = [
    (bottomRight[0] + topLeft[0]) / 2,
    (bottomRight[1] + topLeft[1]) / 2,
  ];
  const leftEdgeCenter = [
    (bottomLeft[0] + topLeft[0]) / 2,
    (bottomLeft[1] + topLeft[1]) / 2,
  ];
  const angle =
    (Math.atan2(center[1] - leftEdgeCenter[1], center[0] - leftEdgeCenter[0]) *
      180) /
    Math.PI;
 
  const attributes = {
    x: `${center[0] - width / 2}`,
    y: `${center[1] - height / 2}`,
    width: `${width}`,
    height: `${height}`,
    stroke: color,
    fill: 'transparent',
    transform: `rotate(${angle} ${center[0]} ${center[1]})`,
    'stroke-width': strokeWidth,
    'stroke-dasharray': lineDash,
  };
 
  if (existingRect) {
    setAttributesIfNecessary(attributes, existingRect);
 
    svgDrawingHelper.setNodeTouched(svgNodeHash);
  } else {
    const svgRectElement = document.createElementNS(svgns, 'rect');
 
    Eif (dataId !== '') {
      svgRectElement.setAttribute('data-id', dataId);
    }
 
    setNewAttributesIfValid(attributes, svgRectElement);
 
    svgDrawingHelper.appendNode(svgRectElement, svgNodeHash);
  }
}