Skip to content

Commit

Permalink
Fix clipping + modify fixture to catch
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Jan 17, 2022
1 parent f9d5494 commit 3195893
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
23 changes: 13 additions & 10 deletions src/types/box.js
Expand Up @@ -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();
}

Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/box/label.js
Expand Up @@ -38,12 +38,12 @@ module.exports = {
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'
}
},
Expand Down
Binary file modified test/fixtures/box/label.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3195893

Please sign in to comment.