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

Only use Path2D caching when available #8464

Merged
merged 2 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/general/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Set the [`ticks.sampleSize`](./axes/cartesian/index.mdx#tick-configuration) opti
## Disable Animations

If your charts have long render times, it is a good idea to disable animations. Doing so will mean that the chart needs to only be rendered once during an update instead of multiple times. This will have the effect of reducing CPU usage and improving general page performance.
Line charts use Path2D caching when animations are disabled.
Line charts use Path2D caching when animations are disabled and Path2D is available.

To disable animations

Expand Down
31 changes: 22 additions & 9 deletions src/elements/element.line.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,27 @@ function _getInterpolationMethod(options) {
return _pointInLine;
}

function strokePathWithCache(ctx, line, start, count) {
let path = line._path;
if (!path) {
path = line._path = new Path2D();
if (line.path(path, start, count)) {
path.closePath();
}
}
ctx.stroke(path);
}
function strokePathDirect(ctx, line, start, count) {
ctx.beginPath();
if (line.path(ctx, start, count)) {
ctx.closePath();
}
ctx.stroke();
}

const usePath2D = typeof Path2D === 'function';
const strokePath = usePath2D ? strokePathWithCache : strokePathDirect;

export default class LineElement extends Element {

constructor(cfg) {
Expand Down Expand Up @@ -363,15 +384,7 @@ export default class LineElement extends Element {

setStyle(ctx, options);

let path = me._path;
if (!path) {
path = me._path = new Path2D();
if (me.path(path, start, count)) {
path.closePath();
}
}

ctx.stroke(path);
strokePath(ctx, me, start, count);

ctx.restore();

Expand Down