Skip to content

Commit d5ada75

Browse files
committed
Handle "Canvas exceeds max size"
Get occasional error logs for this. No idea when this happens; I can only reproduce it by setting the DPR to ridiculous values such as ~40. Just render at DPR 1, which may be wrong but is better than not rendering anything at all.
1 parent 33b7de4 commit d5ada75

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

public/charty.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@
77
// example when zooming (either in browser or OS, e.g. for high-DPI
88
// displays), so account for that with devicePixelRatio.
99
let dpr = Math.max(1, window.devicePixelRatio || 1)
10-
ctx.canvas.width = ctx.canvas.clientWidth * dpr
11-
ctx.canvas.height = ctx.canvas.clientHeight * dpr
12-
ctx.scale(dpr, dpr)
10+
try {
11+
ctx.canvas.width = ctx.canvas.clientWidth * dpr
12+
ctx.canvas.height = ctx.canvas.clientHeight * dpr
13+
ctx.scale(dpr, dpr)
14+
} catch (err) {
15+
// No idea when this happens, but it seems that it does on occasion.
16+
// I can only reproduce it by setting the DPR to ridiculous values
17+
// such as ~40. Just render at dpr 1, which may be wrong but is
18+
// better than not rendering anything at all.
19+
if (!(err instanceof DOMException) || err.message.indexOf('Canvas exceeds max size') === -1)
20+
throw err
21+
dpr = 1
22+
ctx.canvas.width = ctx.canvas.clientWidth * dpr
23+
ctx.canvas.height = ctx.canvas.clientHeight * dpr
24+
ctx.scale(dpr, dpr)
25+
}
1326

1427
opt.line = Object.assign({width: 2, color: '#f00', fill: '#fdd'}, opt.line)
1528
opt.bar = Object.assign({color: '#f00'}, opt.bar)

0 commit comments

Comments
 (0)