diff --git a/src/types/box.js b/src/types/box.js index 45c791abf..328e4b64b 100644 --- a/src/types/box.js +++ b/src/types/box.js @@ -18,22 +18,25 @@ export default class BoxAnnotation extends Element { } drawLabel(ctx) { - const {x, y, width, height, options: {label: labelOpts}} = this; - const halfBorder = labelOpts.borderWidth / 2; - ctx.save(); - ctx.beginPath(); - ctx.rect(x + halfBorder, y + halfBorder, width - halfBorder, height - halfBorder); - ctx.clip(); - const position = toPosition(labelOpts.position); - const padding = toPadding(labelOpts.padding); - const labelSize = measureLabelSize(ctx, labelOpts); + const {x, y, width, height, options} = this; + const {label, borderWidth} = options; + const halfBorder = borderWidth / 2; + const position = toPosition(label.position); + const padding = toPadding(label.padding); + const labelSize = measureLabelSize(ctx, label); const labelRect = { x: calculateX(this, labelSize, position, padding), y: calculateY(this, labelSize, position, padding), width: labelSize.width, height: labelSize.height }; - drawLabel(ctx, labelRect, labelOpts); + + ctx.save(); + ctx.beginPath(); + ctx.rect(x + halfBorder + padding.left, y + halfBorder + padding.top, + width - borderWidth - padding.width, height - borderWidth - padding.height); + ctx.clip(); + drawLabel(ctx, labelRect, label); ctx.restore(); } diff --git a/test/fixtures/box/label.js b/test/fixtures/box/label.js index 144c470e1..612d2d1a2 100644 --- a/test/fixtures/box/label.js +++ b/test/fixtures/box/label.js @@ -18,8 +18,6 @@ module.exports = { annotations: { box1: { type: 'box', - xScaleID: 'x', - yScaleID: 'y', xMin: 1.5, xMax: 3.5, yMin: 5, @@ -35,26 +33,22 @@ module.exports = { }, box2: { type: 'box', - xScaleID: 'x', - yScaleID: 'y', xMin: 'May', xMax: 'July', yMin: 11, yMax: 15, backgroundColor: 'rgba(255, 99, 132, 0.5)', - borderColor: 'rgba(255, 99, 132)', - borderDash: [6, 6], - borderWidth: 5, + borderColor: 'black', + borderWidth: 15, label: { enabled: true, - content: 'This is a label', + borderColor: 'green', + content: 'This label tests clipping', position: 'start' } }, box3: { type: 'box', - xScaleID: 'x', - yScaleID: 'y', xMin: -0.5, xMax: 'May', yMin: 16, @@ -71,8 +65,6 @@ module.exports = { }, box4: { type: 'box', - xScaleID: 'x', - yScaleID: 'y', xMin: 'June', xMax: 'July', yMin: 5, diff --git a/test/fixtures/box/label.png b/test/fixtures/box/label.png index c3ca1ad5f..7408bbf86 100644 Binary files a/test/fixtures/box/label.png and b/test/fixtures/box/label.png differ