Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
limits = Object.assign({}, limits, canvasDimensions);
const toolPositionOnCanvas = cornerstone.pixelToCanvas(element, tool);
const renderingInformation = getRenderingInformation(
limits,
toolPositionOnCanvas
);
directions = renderingInformation.directions;
cornerAxis = renderingInformation.cornerAxis;
const position = {
x: directions.x < 0 ? offset.left : offset.left + canvasWidth,
y: directions.y < 0 ? offset.top : offset.top + canvasHeight,
};
const pixelPosition = cornerstone.pageToPixel(
element,
position.x,
position.y
);
cornerAxisPosition = pixelPosition[cornerAxis];
}
const toolAxis = cornerAxis === 'x' ? 'y' : 'x';
const boxSize = getTextBoxSizeInPixels(element, bounds);
textBox[cornerAxis] = cornerAxisPosition;
textBox[toolAxis] = tool[toolAxis];
// Adjust the text box position reducing its size from the corner axis
const textBoxOffset = getTextBoxOffset(config, cornerAxis, toolAxis, boxSize);
textBox[cornerAxis] += textBoxOffset[cornerAxis][directions[cornerAxis]];
// Preventing the text box from partially going outside the canvas area
const topLeft = cornerstone.pixelToCanvas(element, textBox);
const bottomRight = {
x: topLeft.x + bounds.x,
y: topLeft.y + bounds.y,
};
const canvasBorders = {
x0: offset.left,
y0: offset.top,
x1: offset.left + canvasWidth,
y1: offset.top + canvasHeight,
};
if (topLeft[toolAxis] < 0) {
const x = canvasBorders.x0;
const y = canvasBorders.y0;
const pixelPosition = cornerstone.pageToPixel(element, x, y);
textBox[toolAxis] = pixelPosition[toolAxis];
} else if (bottomRight[toolAxis] > canvasDimensions[toolAxis]) {
const x = canvasBorders.x1 - bounds.x;
const y = canvasBorders.y1 - bounds.y;
const pixelPosition = cornerstone.pageToPixel(element, x, y);
textBox[toolAxis] = pixelPosition[toolAxis];
}
}
const getTextBoxSizeInPixels = (element, bounds) => {
const topLeft = cornerstone.pageToPixel(element, 0, 0);
const bottomRight = cornerstone.pageToPixel(element, bounds.x, bounds.y);
return {
x: bottomRight.x - topLeft.x,
y: bottomRight.y - topLeft.y,
};
};
const getTextBoxSizeInPixels = (element, bounds) => {
const topLeft = cornerstone.pageToPixel(element, 0, 0);
const bottomRight = cornerstone.pageToPixel(element, bounds.x, bounds.y);
return {
x: bottomRight.x - topLeft.x,
y: bottomRight.y - topLeft.y,
};
};
};
const canvasBorders = {
x0: offset.left,
y0: offset.top,
x1: offset.left + canvasWidth,
y1: offset.top + canvasHeight,
};
if (topLeft[toolAxis] < 0) {
const x = canvasBorders.x0;
const y = canvasBorders.y0;
const pixelPosition = cornerstone.pageToPixel(element, x, y);
textBox[toolAxis] = pixelPosition[toolAxis];
} else if (bottomRight[toolAxis] > canvasDimensions[toolAxis]) {
const x = canvasBorders.x1 - bounds.x;
const y = canvasBorders.y1 - bounds.y;
const pixelPosition = cornerstone.pageToPixel(element, x, y);
textBox[toolAxis] = pixelPosition[toolAxis];
}
}