Skip to content

Commit

Permalink
do not attempt to clear canvas if one does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
DAcodedBEAT committed May 7, 2024
1 parent 334fdf7 commit 02d4151
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/helpers/helpers.canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ export function _alignPixel(chart: Chart, pixel: number, width: number) {
/**
* Clears the entire canvas.
*/
export function clearCanvas(canvas: HTMLCanvasElement, ctx?: CanvasRenderingContext2D) {
export function clearCanvas(canvas?: HTMLCanvasElement, ctx?: CanvasRenderingContext2D) {
if (!ctx && !canvas) {
return;
}

ctx = ctx || canvas.getContext('2d');

ctx.save();
Expand Down
9 changes: 9 additions & 0 deletions test/specs/helpers.canvas.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ describe('Chart.helpers.canvas', function() {
expect(chart.ctx.clearRect.calls.first().object).toBe(chart.ctx);
expect(chart.ctx.clearRect.calls.first().args).toEqual([0, 0, 150, 245]);
});

it('should not throw error when chart is null', function() {
function createChart() {
return acquireChart({}, {
canvas: null
})}

expect(createChart).not.toThrow();
});
});

describe('isPointInArea', function() {
Expand Down

0 comments on commit 02d4151

Please sign in to comment.