Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ArrowAnnotate): use svg marker to draw the arrow #1732

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 2 additions & 48 deletions packages/tools/src/drawingSvg/drawArrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function drawArrow(
const { color, width, lineWidth, lineDash } = Object.assign(
{
color: 'rgb(0, 255, 0)',
width: '2',
width: 2,
lineWidth: undefined,
lineDash: undefined,
},
Expand All @@ -31,52 +31,6 @@ export default function drawArrow(
width,
lineWidth,
lineDash,
markerEndId: 'arrow',
});

// Drawing the head arrow with two lines
// Variables to be used when creating the arrow
const headLength = 10;
const angle = Math.atan2(end[1] - start[1], end[0] - start[0]);

const firstLine = {
start: [
end[0] - headLength * Math.cos(angle - Math.PI / 7),
end[1] - headLength * Math.sin(angle - Math.PI / 7),
] as Types.Point2,
end: end,
};

const secondLine = {
start: [
end[0] - headLength * Math.cos(angle + Math.PI / 7),
end[1] - headLength * Math.sin(angle + Math.PI / 7),
] as Types.Point2,
end: end,
};

drawLine(
svgDrawingHelper,
annotationUID,
'2',
firstLine.start,
firstLine.end,
{
color,
width,
lineWidth,
}
);

drawLine(
svgDrawingHelper,
annotationUID,
'3',
secondLine.start,
secondLine.end,
{
color,
width,
lineWidth,
}
);
}
46 changes: 32 additions & 14 deletions packages/tools/src/drawingSvg/drawLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,50 @@ export default function drawLine(
lineUID: string,
start: Types.Point2,
end: Types.Point2,
options = {},
options: {
color?: string;
width?: number;
lineWidth?: number;
lineDash?: string;
markerStartId?: string;
markerEndId?: string;
shadow?: boolean;
},
dataId = ''
): void {
// if length is NaN return
if (isNaN(start[0]) || isNaN(start[1]) || isNaN(end[0]) || isNaN(end[1])) {
return;
}

const { color, width, lineWidth, lineDash, shadow } = Object.assign(
{
color: 'rgb(0, 255, 0)',
width: '2',
lineWidth: undefined,
lineDash: undefined,
shadow: undefined,
},
options
);
const {
color = 'rgb(0, 255, 0)',
width = 10,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we changing the width here? Could this break people's tools since we're changing it from 2 to 10?

lineWidth,
lineDash,
markerStartId = null,
markerEndId = null,
shadow = false,
} = options;

// 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 dropShadowStyle = shadow
? `filter:url(#shadow-${svgDrawingHelper.svgLayerElement.id});`
: '';
const layerId = svgDrawingHelper.svgLayerElement.id;
const dropShadowStyle = shadow ? `filter:url(#shadow-${layerId});` : '';
const arrow = svgDrawingHelper.svgLayerElement.querySelector(
`#${markerEndId}-${layerId}`
);

if (arrow) {
arrow.setAttribute('fill', color);
const newstrokeWidth = strokeWidth >= 4 ? 3 : 6;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is strokeWidth >= 4 ? 3 : 6; used here? It seems arbitrary.

arrow.setAttribute('markerWidth', `${newstrokeWidth}`);
arrow.setAttribute('markerHeight', `${newstrokeWidth}`);
}

const attributes = {
x1: `${start[0]}`,
Expand All @@ -49,6 +65,8 @@ export default function drawLine(
style: dropShadowStyle,
'stroke-width': strokeWidth,
'stroke-dasharray': lineDash,
'marker-start': markerStartId ? `url(#${markerStartId}-${layerId})` : '',
'marker-end': markerEndId ? `url(#${markerEndId}-${layerId})` : '',
};

if (existingLine) {
Expand Down
6 changes: 6 additions & 0 deletions packages/tools/src/drawingSvg/drawPolyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default function drawPolyline(
lineWidth?: number;
lineDash?: string;
closePath?: boolean;
markerStartId?: string;
markerEndId?: string;
}
): void {
if (points.length < 2) {
Expand All @@ -37,6 +39,8 @@ export default function drawPolyline(
lineWidth,
lineDash,
closePath = false,
markerStartId = null,
markerEndId = null,
} = options;

// for supporting both lineWidth and width options
Expand Down Expand Up @@ -65,6 +69,8 @@ export default function drawPolyline(
'fill-opacity': fillOpacity,
'stroke-width': strokeWidth,
'stroke-dasharray': lineDash,
'marker-start': markerStartId ? `url(#${markerStartId})` : '',
'marker-end': markerEndId ? `url(#${markerEndId})` : '',
};

if (existingPolyLine) {
Expand Down
15 changes: 15 additions & 0 deletions packages/tools/src/store/addEnabledElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,25 @@ function _createSvgAnnotationLayer(viewportId: string): SVGElement {
feBlend.setAttribute('in2', 'matrixOut');
feBlend.setAttribute('mode', 'normal');

// arrow marker
const arrowMarker = document.createElementNS(svgns, 'marker');
arrowMarker.setAttribute('id', `arrow-${svgLayerId}`);
arrowMarker.setAttribute('viewBox', '0 0 10 10');
arrowMarker.setAttribute('refX', '8');
arrowMarker.setAttribute('refY', '5');
arrowMarker.setAttribute('markerWidth', '5');
arrowMarker.setAttribute('markerHeight', '5');
arrowMarker.setAttribute('orient', 'auto');
arrowMarker.setAttribute('stroke', 'none');
const arrowPath = document.createElementNS(svgns, 'path');
arrowPath.setAttribute('d', 'M 0 0 L 10 5 L 0 10 z');
arrowMarker.appendChild(arrowPath);

filter.appendChild(feOffset);
filter.appendChild(feColorMatrix);
filter.appendChild(feBlend);
defs.appendChild(filter);
defs.appendChild(arrowMarker);
svgLayer.appendChild(defs);

return svgLayer;
Expand Down
Loading