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

65% Statements 13/20
55.55% Branches 5/9
100% Functions 1/1
65% Lines 13/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 89                                17x                           17x     17x 17x             17x 17x                                                           17x   17x 2x   2x   15x   15x   15x          
import type { Types } from '@cornerstonejs/core';
 
import _getHash from './_getHash';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import { SVGDrawingHelper } from '../types';
 
function drawHandle(
  svgDrawingHelper: SVGDrawingHelper,
  annotationUID: string,
  handleGroupUID: string,
  handle: Types.Point2,
  options = {},
  uniqueIndex
): void {
  const { color, handleRadius, width, lineWidth, fill, type, opacity } =
    Object.assign(
      {
        color: 'rgb(0, 255, 0)',
        handleRadius: '6',
        width: '2',
        lineWidth: undefined,
        fill: 'transparent',
        type: 'circle',
        opacity: 1,
      },
      options
    );
 
  // for supporting both lineWidth and width options
  const strokeWidth = lineWidth || width;
 
  // variable for the namespace
  const svgns = 'http://www.w3.org/2000/svg';
  const svgNodeHash = _getHash(
    annotationUID,
    'handle',
    `hg-${handleGroupUID}-index-${uniqueIndex}`
  );
 
  let attributes;
  if (type === 'circle') {
    attributes = {
      cx: `${handle[0]}`,
      cy: `${handle[1]}`,
      r: handleRadius,
      stroke: color,
      fill,
      'stroke-width': strokeWidth,
      opacity: opacity,
    };
  } else Eif (type === 'rect') {
    const handleRadiusFloat = parseFloat(handleRadius);
    const side = handleRadiusFloat * 1.5;
    const x = handle[0] - side * 0.5;
    const y = handle[1] - side * 0.5;
 
    attributes = {
      x: `${x}`,
      y: `${y}`,
      width: `${side}`,
      height: `${side}`,
      stroke: color,
      fill,
      'stroke-width': strokeWidth,
      rx: `${side * 0.1}`,
      opacity: opacity,
    };
  } else {
    throw new Error(`Unsupported handle type: ${type}`);
  }
 
  const existingHandleElement = svgDrawingHelper.getSvgNode(svgNodeHash);
 
  if (existingHandleElement) {
    setAttributesIfNecessary(attributes, existingHandleElement);
 
    svgDrawingHelper.setNodeTouched(svgNodeHash);
  } else {
    const newHandleElement = document.createElementNS(svgns, type);
 
    setNewAttributesIfValid(attributes, newHandleElement);
 
    svgDrawingHelper.appendNode(newHandleElement, svgNodeHash);
  }
}
 
export default drawHandle;