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

96.66% Statements 29/30
83.33% Branches 10/12
100% Functions 1/1
95.83% Lines 23/24

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                          21x 21x                         21x   21x 29x     29x 29x           29x 29x 21x               8x 8x 8x 8x 8x   8x                           29x   29x 2x   2x   27x   27x   27x            
import type { Types } from '@cornerstonejs/core';
 
import _getHash from './_getHash';
import _setNewAttributesIfValid from './_setNewAttributesIfValid';
import _setAttributesIfNecessary from './_setAttributesIfNecessary';
import { SVGDrawingHelper } from '../types';
 
function drawHandles(
  svgDrawingHelper: SVGDrawingHelper,
  annotationUID: string,
  handleGroupUID: string,
  handlePoints: Array<Types.Point2>,
  options = {}
): void {
  const { color, handleRadius, width, lineWidth, fill, type } = Object.assign(
    {
      color: 'dodgerblue',
      handleRadius: '6',
      width: '2',
      lineWidth: undefined,
      fill: 'transparent',
      type: 'circle',
    },
    options
  );
 
  // for supporting both lineWidth and width options
  const strokeWidth = lineWidth || width;
 
  for (let i = 0; i < handlePoints.length; i++) {
    const handle = handlePoints[i];
 
    // variable for the namespace
    const svgns = 'http://www.w3.org/2000/svg';
    const svgNodeHash = _getHash(
      annotationUID,
      'handle',
      `hg-${handleGroupUID}-index-${i}`
    );
 
    let attributes;
    if (type === 'circle') {
      attributes = {
        cx: `${handle[0]}`,
        cy: `${handle[1]}`,
        r: handleRadius,
        stroke: color,
        fill,
        'stroke-width': strokeWidth,
      };
    } 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}`,
      };
    } 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 drawHandles;