All files / tools/src/drawingSvg drawLine.ts

94.44% Statements 17/18
80.76% Branches 21/26
100% Functions 1/1
94.44% Lines 17/18

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                                  1762x                         1762x                       1762x   1762x 1762x 1762x 1762x 1762x   1762x                           1762x   1514x   1514x   248x   248x 24x     248x   248x      
import type { Types } from '@cornerstonejs/core';
 
import _getHash from './_getHash';
import setNewAttributesIfValid from './setNewAttributesIfValid';
import setAttributesIfNecessary from './setAttributesIfNecessary';
import type { SVGDrawingHelper } from '../types';
 
export default function drawLine(
  svgDrawingHelper: SVGDrawingHelper,
  annotationUID: string,
  lineUID: string,
  start: Types.Point2,
  end: Types.Point2,
  options = {},
  dataId = ''
): void {
  // if length is NaN return
  Iif (isNaN(start[0]) || isNaN(start[1]) || isNaN(end[0]) || isNaN(end[1])) {
    return;
  }
 
  const {
    color = 'rgb(0, 255, 0)',
    width = 10,
    lineWidth,
    lineDash,
    markerStartId = null,
    markerEndId = null,
    shadow = false,
    strokeOpacity = 1,
  } = options as {
    color?: string;
    width?: string;
    lineWidth?: string;
    lineDash?: string;
    markerStartId?: string;
    markerEndId?: string;
    shadow?: boolean;
    strokeOpacity?: number;
  };
 
  // for supporting both lineWidth and width options
  const strokeWidth = lineWidth || width;
 
  const svgns = 'http://www.w3.org/2000/svg';
  const svgNodeHash = _getHash(annotationUID, 'line', lineUID);
  const existingLine = svgDrawingHelper.getSvgNode(svgNodeHash);
  const layerId = svgDrawingHelper.svgLayerElement.id;
  const dropShadowStyle = shadow ? `filter:url(#shadow-${layerId});` : '';
 
  const attributes = {
    x1: `${start[0]}`,
    y1: `${start[1]}`,
    x2: `${end[0]}`,
    y2: `${end[1]}`,
    stroke: color,
    style: dropShadowStyle,
    'stroke-width': strokeWidth,
    'stroke-dasharray': lineDash,
    'marker-start': markerStartId ? `url(#${markerStartId})` : '',
    'marker-end': markerEndId ? `url(#${markerEndId})` : '',
    'stroke-opacity': strokeOpacity,
  };
 
  if (existingLine) {
    // This is run to avoid re-rendering annotations that actually haven't changed
    setAttributesIfNecessary(attributes, existingLine);
 
    svgDrawingHelper.setNodeTouched(svgNodeHash);
  } else {
    const newLine = document.createElementNS(svgns, 'line');
 
    if (dataId !== '') {
      newLine.setAttribute('data-id', dataId);
    }
 
    setNewAttributesIfValid(attributes, newLine);
 
    svgDrawingHelper.appendNode(newLine, svgNodeHash);
  }
}