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

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

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                                            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';
 
// <rect x="120" y="100" width="100" height="100" />
export default function drawRect(
  svgDrawingHelper: SVGDrawingHelper,
  annotationUID: string,
  rectangleUID: string,
  start: Types.Point2,
  end: 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 tlhc = [Math.min(start[0], end[0]), Math.min(start[1], end[1])];
  const width = Math.abs(start[0] - end[0]);
  const height = Math.abs(start[1] - end[1]);
 
  const attributes = {
    x: `${tlhc[0]}`,
    y: `${tlhc[1]}`,
    width: `${width}`,
    height: `${height}`,
    stroke: color,
    fill: 'transparent',
    '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);
  }
}