Skip to content

Commit

Permalink
Bar: inflate rects by 0.33px to avoid artifacts (#9399)
Browse files Browse the repository at this point in the history
* Bar: inflate rects by 0.33px to avoid artifacts
* Update fixture
* YAGNI
  • Loading branch information
kurkle committed Jul 13, 2021
1 parent f315daf commit 58999ea
Show file tree
Hide file tree
Showing 24 changed files with 95 additions and 14 deletions.
25 changes: 20 additions & 5 deletions src/elements/element.bar.js
@@ -1,5 +1,5 @@
import Element from '../core/core.element';
import {isObject} from '../helpers';
import {isObject, _limitValue} from '../helpers';
import {addRoundedRectPath} from '../helpers/helpers.canvas';
import {toTRBL, toTRBLCorners} from '../helpers/helpers.options';

Expand Down Expand Up @@ -67,7 +67,7 @@ function startEnd(v, start, end) {
}

function skipOrLimit(skip, value, min, max) {
return skip ? 0 : Math.max(Math.min(value, max), min);
return skip ? 0 : _limitValue(value, min, max);
}

function parseBorderWidth(bar, maxW, maxH) {
Expand Down Expand Up @@ -156,6 +156,20 @@ function addNormalRectPath(ctx, rect) {
ctx.rect(rect.x, rect.y, rect.w, rect.h);
}

function inflateRect(rect, amount, refRect = {}) {
const x = rect.x !== refRect.x ? -amount : 0;
const y = rect.y !== refRect.y ? -amount : 0;
const w = (rect.x + rect.w !== refRect.x + refRect.w ? amount : 0) - x;
const h = (rect.y + rect.h !== refRect.y + refRect.h ? amount : 0) - y;
return {
x: rect.x + x,
y: rect.y + y,
w: rect.w + w,
h: rect.h + h,
radius: rect.radius
};
}

export default class BarElement extends Element {

constructor(cfg) {
Expand All @@ -176,20 +190,21 @@ export default class BarElement extends Element {
const options = this.options;
const {inner, outer} = boundingRects(this);
const addRectPath = hasRadius(outer.radius) ? addRoundedRectPath : addNormalRectPath;
const inflateAmount = 0.33;

ctx.save();

if (outer.w !== inner.w || outer.h !== inner.h) {
ctx.beginPath();
addRectPath(ctx, outer);
addRectPath(ctx, inflateRect(outer, inflateAmount, inner));
ctx.clip();
addRectPath(ctx, inner);
addRectPath(ctx, inflateRect(inner, -inflateAmount, outer));
ctx.fillStyle = options.borderColor;
ctx.fill('evenodd');
}

ctx.beginPath();
addRectPath(ctx, inner);
addRectPath(ctx, inflateRect(inner, inflateAmount, outer));
ctx.fillStyle = options.backgroundColor;
ctx.fill();

Expand Down
Binary file modified test/fixtures/controller.bar/bar-base-value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/bottom.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/left.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/mid-x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/mid-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/right.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/top.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/value-x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/baseLine/value-y.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions test/fixtures/controller.bar/borderColor/border+dpr.js
@@ -0,0 +1,35 @@
module.exports = {
threshold: 0,
tolerance: 0,
config: {
type: 'bar',
data: {
labels: [0, 1, 2, 3, 4, 5, 6],
datasets: [
{
// option in dataset
data: [5, 4, 3, 2, 3, 4, 5],
},
]
},
options: {
events: [],
devicePixelRatio: 1.5,
barPercentage: 1,
categoryPercentage: 1,
backgroundColor: 'black',
borderColor: 'black',
borderWidth: 8,
scales: {
x: {display: false},
y: {display: false}
}
}
},
options: {
canvas: {
height: 256,
width: 501
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderRadius/border-radius.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions test/fixtures/controller.bar/borderRadius/no-spacing.js
@@ -0,0 +1,33 @@
module.exports = {
threshold: 0.01,
config: {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
datasets: [
{
data: [9, 25, 13, 17, 12, 21, 20, 19, 6, 12, 14, 20],
categoryPercentage: 1,
barPercentage: 1,
backgroundColor: '#2E5C76',
borderWidth: 2,
borderColor: '#377395',
borderRadius: 5,
},
]
},
options: {
devicePixelRatio: 1.25,
scales: {
x: {display: false},
y: {display: false}
}
}
},
options: {
canvas: {
height: 256,
width: 512
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderWidth/indexable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderWidth/object.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/borderWidth/value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/controller.bar/horizontal-borders.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 7 additions & 9 deletions test/fixtures/scale.category/ticks-from-data.js
Expand Up @@ -4,26 +4,24 @@ module.exports = {
type: 'bar',
data: {
datasets: [{
data: [10, 5, 0, 25, 78]
data: [10, 5, 0, 25, 78],
backgroundColor: 'transparent'
}],
labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5']
},
options: {
indexAxis: 'y',
elements: {
bar: {
backgroundColor: '#AAAAAA80',
borderColor: '#80808080',
borderWidth: {bottom: 6, left: 15, top: 6, right: 15}
}
},
scales: {
x: {display: false},
y: {display: true}
}
}
},
options: {
spriteText: true
spriteText: true,
canvas: {
width: 128,
height: 256
}
}
};
Binary file modified test/fixtures/scale.category/ticks-from-data.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 58999ea

Please sign in to comment.