diff --git a/src/helpers/helpers.canvas.ts b/src/helpers/helpers.canvas.ts index f37504c0097..c3bfd0b8c7f 100644 --- a/src/helpers/helpers.canvas.ts +++ b/src/helpers/helpers.canvas.ts @@ -326,6 +326,7 @@ export function _isPointInArea( } export function clipArea(ctx: CanvasRenderingContext2D, area: TRBL) { + if (!ctx) return; ctx.save(); ctx.beginPath(); ctx.rect(area.left, area.top, area.right - area.left, area.bottom - area.top); @@ -333,6 +334,7 @@ export function clipArea(ctx: CanvasRenderingContext2D, area: TRBL) { } export function unclipArea(ctx: CanvasRenderingContext2D) { + if (!ctx) return; ctx.restore(); } diff --git a/src/helpers/helpers.dom.ts b/src/helpers/helpers.dom.ts index b0c41eec0c0..cbb6341cc3b 100644 --- a/src/helpers/helpers.dom.ts +++ b/src/helpers/helpers.dom.ts @@ -111,6 +111,11 @@ export function getRelativePosition( } const {canvas, currentDevicePixelRatio} = chart; + // Guard against null canvas to prevent ownerDocument crash + if (!canvas) { + return {x: 0, y: 0}; + } + const style = getComputedStyle(canvas); const borderBox = style.boxSizing === 'border-box'; const paddings = getPositionedStyle(style, 'padding'); @@ -166,6 +171,11 @@ export function getMaximumSize( bbHeight?: number, aspectRatio?: number ): { width: number; height: number } { + // Guard against null canvas to prevent ownerDocument crash + if (!canvas) { + return {width: 0, height: 0}; + } + const style = getComputedStyle(canvas); const margins = getPositionedStyle(style, 'margin'); const maxWidth = parseMaxStyle(style.maxWidth, canvas, 'clientWidth') || INFINITY;