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

do not attempt to clear canvas if one does not exist #11764

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
12 changes: 12 additions & 0 deletions test/specs/helpers.canvas.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ 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 createAndClearChart() {
var chart = acquireChart({}, {
canvas: null
});

helpers.clearCanvas(chart.canvas, chart.ctx);
}

expect(createAndClearChart).not.toThrow();
});
LeeLenaleee marked this conversation as resolved.
Show resolved Hide resolved
});

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