Skip to content

Commit b467c0d

Browse files
authored
Only use Path2D caching when available (#8464)
* Only use Path2D caching when available * Try to make CC happy
1 parent d63542c commit b467c0d

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

docs/docs/general/performance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Set the [`ticks.sampleSize`](./axes/cartesian/index.mdx#tick-configuration) opti
3535
## Disable Animations
3636

3737
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.
38-
Line charts use Path2D caching when animations are disabled.
38+
Line charts use Path2D caching when animations are disabled and Path2D is available.
3939

4040
To disable animations
4141

src/elements/element.line.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,27 @@ function _getInterpolationMethod(options) {
198198
return _pointInLine;
199199
}
200200

201+
function strokePathWithCache(ctx, line, start, count) {
202+
let path = line._path;
203+
if (!path) {
204+
path = line._path = new Path2D();
205+
if (line.path(path, start, count)) {
206+
path.closePath();
207+
}
208+
}
209+
ctx.stroke(path);
210+
}
211+
function strokePathDirect(ctx, line, start, count) {
212+
ctx.beginPath();
213+
if (line.path(ctx, start, count)) {
214+
ctx.closePath();
215+
}
216+
ctx.stroke();
217+
}
218+
219+
const usePath2D = typeof Path2D === 'function';
220+
const strokePath = usePath2D ? strokePathWithCache : strokePathDirect;
221+
201222
export default class LineElement extends Element {
202223

203224
constructor(cfg) {
@@ -363,15 +384,7 @@ export default class LineElement extends Element {
363384

364385
setStyle(ctx, options);
365386

366-
let path = me._path;
367-
if (!path) {
368-
path = me._path = new Path2D();
369-
if (me.path(path, start, count)) {
370-
path.closePath();
371-
}
372-
}
373-
374-
ctx.stroke(path);
387+
strokePath(ctx, me, start, count);
375388

376389
ctx.restore();
377390

0 commit comments

Comments
 (0)