Skip to content

Commit

Permalink
feat: 🎸 Dashed rendering options for tools with lines/handles (#1254)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesAPetts authored Jun 24, 2020
1 parent c07c3bf commit aa54b30
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 76 deletions.
7 changes: 6 additions & 1 deletion src/drawing/drawArrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import drawJoinedLines from './drawJoinedLines.js';
* @param {Object} end The end position.
* @param {string} color The color of the arrow.
* @param {number} lineWidth The width of the arrow line.
* @param {number[]|| undefined} [lineDash] The optional lineDash style.
* @returns {undefined}
*/
export default function(context, start, end, color, lineWidth) {
export default function(context, start, end, color, lineWidth, lineDash) {
// Variables to be used when creating the arrow
const headLength = 10;

Expand All @@ -26,6 +27,10 @@ export default function(context, start, end, color, lineWidth) {
lineWidth,
};

if (lineDash) {
options.lineDash = lineDash;
}

drawLine(context, undefined, start, end, options, 'canvas');
options = {
color,
Expand Down
45 changes: 22 additions & 23 deletions src/drawing/drawHandles.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,29 @@ export default function(context, evtDetail, handles, options = {}) {
: toolStyle.getToolWidth();
const fillStyle = options.fill;

path(
context,
{
lineWidth,
fillStyle,
},
context => {
const handleCanvasCoords = external.cornerstone.pixelToCanvas(
element,
handle
);
const pathOptions = { lineWidth, fillStyle };

// Handle's radisu, then tool's radius, then default radius
const handleRadius =
handle.radius || options.handleRadius || state.handleRadius;
if (options.lineDash) {
pathOptions.lineDash = options.lineDash;
}

path(context, pathOptions, context => {
const handleCanvasCoords = external.cornerstone.pixelToCanvas(
element,
handle
);

// Handle's radisu, then tool's radius, then default radius
const handleRadius =
handle.radius || options.handleRadius || state.handleRadius;

context.arc(
handleCanvasCoords.x,
handleCanvasCoords.y,
handleRadius,
0,
2 * Math.PI
);
}
);
context.arc(
handleCanvasCoords.x,
handleCanvasCoords.y,
handleRadius,
0,
2 * Math.PI
);
});
}
}
1 change: 1 addition & 0 deletions src/store/modules/globalConfigurationModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const configuration = {
globalToolSyncEnabled: false,
showSVGCursors: false,
autoResizeViewports: true,
lineDash: [4, 4],
};

export default {
Expand Down
17 changes: 15 additions & 2 deletions src/tools/annotation/AngleTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import triggerEvent from '../../util/triggerEvent.js';
import EVENTS from '../../events.js';
import getPixelSpacing from '../../util/getPixelSpacing';
import throttle from '../../util/throttle';
import { getModule } from '../../store/index';

/**
* @public
Expand All @@ -46,6 +47,7 @@ export default class AngleTool extends BaseAnnotationTool {
svgCursor: angleCursor,
configuration: {
drawHandles: true,
renderDashed: false,
},
};

Expand Down Expand Up @@ -137,9 +139,14 @@ export default class AngleTool extends BaseAnnotationTool {
renderToolData(evt) {
const eventData = evt.detail;
const enabledElement = eventData.enabledElement;
const { handleRadius, drawHandlesOnHover } = this.configuration;
const {
handleRadius,
drawHandlesOnHover,
renderDashed,
} = this.configuration;
// If we have no toolData for this element, return immediately as there is nothing to do
const toolData = getToolState(evt.currentTarget, this.name);
const lineDash = getModule('globalConfiguration').configuration.lineDash;

if (!toolData) {
return;
Expand Down Expand Up @@ -174,12 +181,18 @@ export default class AngleTool extends BaseAnnotationTool {
data.handles.middle
);

const lineOptions = { color };

if (renderDashed) {
lineOptions.lineDash = lineDash;
}

drawJoinedLines(
context,
eventData.element,
data.handles.start,
[data.handles.middle, data.handles.end],
{ color }
lineOptions
);

// Draw the handles
Expand Down
19 changes: 16 additions & 3 deletions src/tools/annotation/ArrowAnnotateTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import drawArrow from './../../drawing/drawArrow.js';
import drawHandles from './../../drawing/drawHandles.js';
import { textBoxWidth } from './../../drawing/drawTextBox.js';
import { arrowAnnotateCursor } from '../cursors/index.js';
import { getModule } from '../../store/index';

/**
* @public
Expand All @@ -41,6 +42,7 @@ export default class ArrowAnnotateTool extends BaseAnnotationTool {
drawHandles: false,
drawHandlesOnHover: true,
arrowFirst: true,
renderDashed: false,
},
svgCursor: arrowAnnotateCursor,
};
Expand Down Expand Up @@ -97,7 +99,11 @@ export default class ArrowAnnotateTool extends BaseAnnotationTool {

renderToolData(evt) {
const { element, enabledElement } = evt.detail;
const { handleRadius, drawHandlesOnHover } = this.configuration;
const {
handleRadius,
drawHandlesOnHover,
renderDashed,
} = this.configuration;

// If we have no toolData for this element, return immediately as there is nothing to do
const toolData = getToolState(element, this.name);
Expand All @@ -112,6 +118,11 @@ export default class ArrowAnnotateTool extends BaseAnnotationTool {

const lineWidth = toolStyle.getToolWidth();

let lineDash;
if (renderDashed) {
lineDash = getModule('globalConfiguration').configuration.lineDash;
}

for (let i = 0; i < toolData.data.length; i++) {
const data = toolData.data[i];

Expand Down Expand Up @@ -141,15 +152,17 @@ export default class ArrowAnnotateTool extends BaseAnnotationTool {
handleEndCanvas,
handleStartCanvas,
color,
lineWidth
lineWidth,
lineDash
);
} else {
drawArrow(
context,
handleStartCanvas,
handleEndCanvas,
color,
lineWidth
lineWidth,
lineDash
);
}

Expand Down
1 change: 1 addition & 0 deletions src/tools/annotation/BidirectionalTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class BidirectionalTool extends BaseAnnotationTool {
shadow: '',
drawHandles: true,
drawHandlesOnHover: true,
renderDashed: false,
additionalData: [],
},
svgCursor: bidirectionalCursor,
Expand Down
21 changes: 17 additions & 4 deletions src/tools/annotation/CircleRoiTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import BaseAnnotationTool from '../base/BaseAnnotationTool.js';
import { getToolState } from './../../stateManagement/toolState.js';
import toolStyle from './../../stateManagement/toolStyle.js';
import toolColors from './../../stateManagement/toolColors.js';
import { getModule } from '../../store/index';

// Drawing
import {
Expand Down Expand Up @@ -43,6 +44,9 @@ export default class CircleRoiTool extends BaseAnnotationTool {
name: 'CircleRoi',
supportedInteractionTypes: ['Mouse', 'Touch'],
svgCursor: circleRoiCursor,
configuration: {
renderDashed: false,
},
};

super(props, defaultProps);
Expand Down Expand Up @@ -164,9 +168,14 @@ export default class CircleRoiTool extends BaseAnnotationTool {
const eventData = evt.detail;
const { image, element, canvasContext } = eventData;
const lineWidth = toolStyle.getToolWidth();
const { handleRadius, drawHandlesOnHover } = this.configuration;
const {
handleRadius,
drawHandlesOnHover,
renderDashed,
} = this.configuration;
const newContext = getNewContext(canvasContext.canvas);
const { rowPixelSpacing, colPixelSpacing } = getPixelSpacing(image);
const lineDash = getModule('globalConfiguration').configuration.lineDash;

// Meta
const seriesModule =
Expand Down Expand Up @@ -209,15 +218,19 @@ export default class CircleRoiTool extends BaseAnnotationTool {
// Calculating the radius where startCanvas is the center of the circle to be drawn
const radius = getDistance(startCanvas, endCanvas);

const circleOptions = { color };

if (renderDashed) {
circleOptions.lineDash = lineDash;
}

// Draw Circle
drawCircle(
context,
element,
data.handles.start,
radius,
{
color,
},
circleOptions,
'pixel'
);

Expand Down
23 changes: 16 additions & 7 deletions src/tools/annotation/CobbAngleTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { cobbAngleCursor } from '../cursors/index.js';
import triggerEvent from '../../util/triggerEvent.js';
import throttle from '../../util/throttle';
import getPixelSpacing from '../../util/getPixelSpacing';
import { getModule } from '../../store/index';

/**
* @public
Expand All @@ -43,6 +44,7 @@ export default class CobbAngleTool extends BaseAnnotationTool {
svgCursor: cobbAngleCursor,
configuration: {
drawHandles: true,
renderDashed: false,
},
};

Expand Down Expand Up @@ -159,7 +161,11 @@ export default class CobbAngleTool extends BaseAnnotationTool {

renderToolData(evt) {
const eventData = evt.detail;
const { handleRadius, drawHandlesOnHover } = this.configuration;
const {
handleRadius,
drawHandlesOnHover,
renderDashed,
} = this.configuration;
// If we have no toolData for this element, return immediately as there is nothing to do
const toolData = getToolState(evt.currentTarget, this.name);

Expand All @@ -171,6 +177,7 @@ export default class CobbAngleTool extends BaseAnnotationTool {
const context = getNewContext(eventData.canvasContext.canvas);

const lineWidth = toolStyle.getToolWidth();
const lineDash = getModule('globalConfiguration').configuration.lineDash;
const font = textStyle.getFont();

for (let i = 0; i < toolData.data.length; i++) {
Expand All @@ -186,14 +193,18 @@ export default class CobbAngleTool extends BaseAnnotationTool {
// Differentiate the color of activation tool
const color = toolColors.getColorIfActive(data);

const lineOptions = { color };

if (renderDashed) {
lineOptions.lineDash = lineDash;
}

drawLine(
context,
eventData.element,
data.handles.start,
data.handles.end,
{
color,
}
lineOptions
);

if (data.complete) {
Expand All @@ -202,9 +213,7 @@ export default class CobbAngleTool extends BaseAnnotationTool {
eventData.element,
data.handles.start2,
data.handles.end2,
{
color,
}
lineOptions
);
}

Expand Down
19 changes: 15 additions & 4 deletions src/tools/annotation/EllipticalRoiTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import throttle from './../../util/throttle.js';
import { ellipticalRoiCursor } from '../cursors/index.js';
import { getLogger } from '../../util/logger.js';
import getPixelSpacing from '../../util/getPixelSpacing';
import { getModule } from '../../store/index';

const logger = getLogger('tools:annotation:EllipticalRoiTool');

Expand All @@ -47,6 +48,7 @@ export default class EllipticalRoiTool extends BaseAnnotationTool {
configuration: {
// showMinMax: false,
// showHounsfieldUnits: true,
renderDashed: false,
},
svgCursor: ellipticalRoiCursor,
};
Expand Down Expand Up @@ -177,7 +179,12 @@ export default class EllipticalRoiTool extends BaseAnnotationTool {
const eventData = evt.detail;
const { image, element } = eventData;
const lineWidth = toolStyle.getToolWidth();
const { handleRadius, drawHandlesOnHover } = this.configuration;
const lineDash = getModule('globalConfiguration').configuration.lineDash;
const {
handleRadius,
drawHandlesOnHover,
renderDashed,
} = this.configuration;
const context = getNewContext(eventData.canvasContext.canvas);
const { rowPixelSpacing, colPixelSpacing } = getPixelSpacing(image);

Expand Down Expand Up @@ -209,15 +216,19 @@ export default class EllipticalRoiTool extends BaseAnnotationTool {

setShadow(context, this.configuration);

const ellipseOptions = { color };

if (renderDashed) {
ellipseOptions.lineDash = lineDash;
}

// Draw
drawEllipse(
context,
element,
data.handles.start,
data.handles.end,
{
color,
},
ellipseOptions,
'pixel',
data.handles.initialRotation
);
Expand Down
Loading

0 comments on commit aa54b30

Please sign in to comment.