Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bar-race): fix lines glitch with sub-pixel optimization in animations #14679 #17426

Merged
merged 11 commits into from
Aug 18, 2022
38 changes: 22 additions & 16 deletions src/component/axis/AxisBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,17 @@ const builders: Record<'axisLine' | 'axisTickLabel' | 'axisName', AxisElementsBu
);

const line = new graphic.Line({
// Id for animation
subPixelOptimize: true,
shape: {
x1: pt1[0],
y1: pt1[1],
x2: pt2[0],
y2: pt2[1]
},
shape: graphic.subPixelOptimizeLine({
shape: {
x1: pt1[0],
y1: pt1[1],
x2: pt2[0],
y2: pt2[1]
},
style: {
lineWidth: lineStyle.lineWidth
}
}).shape,
style: lineStyle,
strokeContainThreshold: opt.strokeContainThreshold || 5,
silent: true,
Expand Down Expand Up @@ -343,7 +346,6 @@ const builders: Record<'axisLine' | 'axisTickLabel' | 'axisName', AxisElementsBu
},

axisTickLabel(opt, axisModel, group, transformGroup) {

const ticksEls = buildAxisMajorTicks(group, transformGroup, axisModel, opt);
const labelEls = buildAxisLabel(group, transformGroup, axisModel, opt);

Expand Down Expand Up @@ -630,13 +632,17 @@ function createTicks(
}
// Tick line, Not use group transform to have better line draw
const tickEl = new graphic.Line({
subPixelOptimize: true,
shape: {
x1: pt1[0],
y1: pt1[1],
x2: pt2[0],
y2: pt2[1]
},
shape: graphic.subPixelOptimizeLine({
shape: {
x1: pt1[0],
y1: pt1[1],
x2: pt2[0],
y2: pt2[1]
},
style: {
lineWidth: tickLineStyle.lineWidth
}
}).shape,
style: tickLineStyle,
z2: 2,
autoBatch: true,
Expand Down
49 changes: 30 additions & 19 deletions src/component/axis/CartesianAxisView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class CartesianAxisView extends AxisView {

const layout = cartesianAxisHelper.layout(gridModel, axisModel);


const axisBuilder = new AxisBuilder(axisModel, zrUtil.extend({
handleAutoShown(elementType) {
const cartesians = gridModel.coordinateSystem.getCartesians();
Expand Down Expand Up @@ -139,6 +140,7 @@ const axisElementBuilders: Record<typeof selfBuilderAttrs[number], AxisElementBu
const p2 = [];

const lineStyle = lineStyleModel.getLineStyle();

for (let i = 0; i < ticksCoords.length; i++) {
const tickCoord = axis.toGlobalCoord(ticksCoords[i].coord);

Expand All @@ -157,21 +159,26 @@ const axisElementBuilders: Record<typeof selfBuilderAttrs[number], AxisElementBu

const colorIndex = (lineCount++) % lineColors.length;
const tickValue = ticksCoords[i].tickValue;
axisGroup.add(new graphic.Line({
const line = new graphic.Line({
Ovilia marked this conversation as resolved.
Show resolved Hide resolved
anid: tickValue != null ? 'line_' + ticksCoords[i].tickValue : null,
subPixelOptimize: true,
autoBatch: true,
shape: {
x1: p1[0],
y1: p1[1],
x2: p2[0],
y2: p2[1]
},
shape: graphic.subPixelOptimizeLine({
shape: {
x1: p1[0],
y1: p1[1],
x2: p2[0],
y2: p2[1]
},
style: {
lineWidth: lineStyle.lineWidth
}
}).shape,
style: zrUtil.defaults({
stroke: lineColors[colorIndex]
}, lineStyle),
silent: true
}));
});
axisGroup.add(line);
}
},

Expand All @@ -193,7 +200,6 @@ const axisElementBuilders: Record<typeof selfBuilderAttrs[number], AxisElementBu

const lineStyle = lineStyleModel.getLineStyle();


for (let i = 0; i < minorTicksCoords.length; i++) {
for (let k = 0; k < minorTicksCoords[i].length; k++) {
const tickCoord = axis.toGlobalCoord(minorTicksCoords[i][k].coord);
Expand All @@ -211,19 +217,24 @@ const axisElementBuilders: Record<typeof selfBuilderAttrs[number], AxisElementBu
p2[1] = tickCoord;
}

axisGroup.add(new graphic.Line({
const line = new graphic.Line({
Ovilia marked this conversation as resolved.
Show resolved Hide resolved
anid: 'minor_line_' + minorTicksCoords[i][k].tickValue,
subPixelOptimize: true,
autoBatch: true,
shape: {
x1: p1[0],
y1: p1[1],
x2: p2[0],
y2: p2[1]
},
shape: graphic.subPixelOptimizeLine({
shape: {
x1: p1[0],
y1: p1[1],
x2: p2[0],
y2: p2[1]
},
style: {
lineWidth: lineStyle.lineWidth
}
}).shape,
style: lineStyle,
silent: true
}));
});
axisGroup.add(line);
}
}
},
Expand Down
29 changes: 18 additions & 11 deletions src/component/axis/SingleAxisView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ const axisElementBuilders: Record<typeof selfBuilderAttrs[number], AxisElementBu
const splitLineModel = axisModel.getModel('splitLine');
const lineStyleModel = splitLineModel.getModel('lineStyle');
let lineColors = lineStyleModel.get('color');

lineColors = lineColors instanceof Array ? lineColors : [lineColors];
const lineWidth = lineStyleModel.get('width');

const gridRect = axisModel.coordinateSystem.getRect();
const isHorizontal = axis.isHorizontal();
Expand Down Expand Up @@ -123,18 +123,25 @@ const axisElementBuilders: Record<typeof selfBuilderAttrs[number], AxisElementBu
p2[0] = gridRect.x + gridRect.width;
p2[1] = tickCoord;
}

const line = new graphic.Line({
shape: graphic.subPixelOptimizeLine({
shape: {
x1: p1[0],
y1: p1[1],
x2: p2[0],
y2: p2[1]
},
style: {
lineWidth: lineWidth
}
}).shape,
silent: true
});

const colorIndex = (lineCount++) % lineColors.length;
splitLines[colorIndex] = splitLines[colorIndex] || [];
splitLines[colorIndex].push(new graphic.Line({
subPixelOptimize: true,
shape: {
x1: p1[0],
y1: p1[1],
x2: p2[0],
y2: p2[1]
},
silent: true
}));
splitLines[colorIndex].push(line);
}

const lineStyle = lineStyleModel.getLineStyle(['color']);
Expand Down
3 changes: 2 additions & 1 deletion src/util/graphic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ import {
keys,
each,
hasOwn,
isArray
isArray,
retrieve2
Ovilia marked this conversation as resolved.
Show resolved Hide resolved
} from 'zrender/src/core/util';
import { getECData } from './innerStore';
import ComponentModel from '../model/Component';
Expand Down
5 changes: 5 additions & 0 deletions test/bar-race.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.