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

92.59% Statements 25/27
58.33% Branches 7/12
100% Functions 1/1
90.9% Lines 20/22

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                              5x 5x                     5x   5x 5x 5x   5x 5x 5x 5x   5x 5x 5x   5x                     5x         5x   5x 5x     5x   5x          
import type { Types } from '@cornerstonejs/core';
import { SVGDrawingHelper } from '../types';
 
import _getHash from './_getHash';
import _setAttributesIfNecessary from './_setAttributesIfNecessary';
import _setNewAttributesIfValid from './_setNewAttributesIfValid';
 
function drawEllipse(
  svgDrawingHelper: SVGDrawingHelper,
  annotationUID: string,
  ellipseUID: string,
  corner1: Types.Point2,
  corner2: Types.Point2,
  options = {},
  dataId = ''
): void {
  const { color, width, lineWidth, lineDash } = Object.assign(
    {
      color: 'dodgerblue',
      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, 'ellipse', ellipseUID);
  const existingEllipse = svgDrawingHelper.getSvgNode(svgNodeHash);
 
  const w = Math.abs(corner1[0] - corner2[0]);
  const h = Math.abs(corner1[1] - corner2[1]);
  const xMin = Math.min(corner1[0], corner2[0]);
  const yMin = Math.min(corner1[1], corner2[1]);
 
  const center = [xMin + w / 2, yMin + h / 2];
  const radiusX = w / 2;
  const radiusY = h / 2;
 
  const attributes = {
    cx: `${center[0]}`,
    cy: `${center[1]}`,
    rx: `${radiusX}`,
    ry: `${radiusY}`,
    stroke: color,
    fill: 'transparent',
    'stroke-width': strokeWidth,
    'stroke-dasharray': lineDash,
  };
 
  Iif (existingEllipse) {
    _setAttributesIfNecessary(attributes, existingEllipse);
 
    svgDrawingHelper.setNodeTouched(svgNodeHash);
  } else {
    const svgEllipseElement = document.createElementNS(svgns, 'ellipse');
 
    Eif (dataId !== '') {
      svgEllipseElement.setAttribute('data-id', dataId);
    }
 
    _setNewAttributesIfValid(attributes, svgEllipseElement);
 
    svgDrawingHelper.appendNode(svgEllipseElement, svgNodeHash);
  }
}
 
export default drawEllipse;