From 80df1f837ee324302b11f79e5799e44604af6549 Mon Sep 17 00:00:00 2001
From: Arun Philip <dacodedbeat@gmail.com>
Date: Tue, 7 May 2024 18:04:05 -0400
Subject: [PATCH 1/4] do not attempt to clear canvas if one does not exist

---
 src/helpers/helpers.canvas.ts      |  6 +++++-
 test/specs/helpers.canvas.tests.js | 10 ++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/helpers/helpers.canvas.ts b/src/helpers/helpers.canvas.ts
index a959d1dea1d..f37504c0097 100644
--- a/src/helpers/helpers.canvas.ts
+++ b/src/helpers/helpers.canvas.ts
@@ -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();
diff --git a/test/specs/helpers.canvas.tests.js b/test/specs/helpers.canvas.tests.js
index ec7a539042a..60f1b1f8700 100644
--- a/test/specs/helpers.canvas.tests.js
+++ b/test/specs/helpers.canvas.tests.js
@@ -21,6 +21,16 @@ 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() {

From e1d670dbbff8efa41772677cdcb9e448d78f73c0 Mon Sep 17 00:00:00 2001
From: Arun Philip <dacodedbeat@gmail.com>
Date: Mon, 13 May 2024 10:32:56 -0400
Subject: [PATCH 2/4] update test to explicitly run clearCanvas method to
 ensure it doesn't throw an error

---
 test/specs/helpers.canvas.tests.js | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/test/specs/helpers.canvas.tests.js b/test/specs/helpers.canvas.tests.js
index 60f1b1f8700..32ed3752ad2 100644
--- a/test/specs/helpers.canvas.tests.js
+++ b/test/specs/helpers.canvas.tests.js
@@ -23,13 +23,15 @@ describe('Chart.helpers.canvas', function() {
     });
 
     it('should not throw error when chart is null', function() {
-      function createChart() {
-        return acquireChart({}, {
+      function createAndClearChart() {
+        var chart = acquireChart({}, {
           canvas: null
         });
+
+        helpers.clearCanvas(chart.canvas, chart.ctx);
       }
 
-      expect(createChart).not.toThrow();
+      expect(createAndClearChart).not.toThrow();
     });
   });
 

From 6e1e356b4c60f27fdb3af8aba6dfd46bf14bfc69 Mon Sep 17 00:00:00 2001
From: Arun Philip <dacodedbeat@gmail.com>
Date: Thu, 16 May 2024 10:32:34 -0400
Subject: [PATCH 3/4] explicitly set canvas and ctx to null in test since the
 helper in test code didn't

---
 test/specs/helpers.canvas.tests.js | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/test/specs/helpers.canvas.tests.js b/test/specs/helpers.canvas.tests.js
index 32ed3752ad2..73ab68c55e0 100644
--- a/test/specs/helpers.canvas.tests.js
+++ b/test/specs/helpers.canvas.tests.js
@@ -27,6 +27,9 @@ describe('Chart.helpers.canvas', function() {
         var chart = acquireChart({}, {
           canvas: null
         });
+        // explicitly set canvas and ctx to null since setting it in acquireChart doesn't do anything
+        chart.canvas = null
+        chart.ctx = null
 
         helpers.clearCanvas(chart.canvas, chart.ctx);
       }

From c4363ec40fba28c3171a64ea61b45a7440d6c59f Mon Sep 17 00:00:00 2001
From: Jacco van den Berg <jaccoberg2281@gmail.com>
Date: Thu, 16 May 2024 20:51:25 +0200
Subject: [PATCH 4/4] Update test/specs/helpers.canvas.tests.js

---
 test/specs/helpers.canvas.tests.js | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test/specs/helpers.canvas.tests.js b/test/specs/helpers.canvas.tests.js
index 73ab68c55e0..ba28e3f78d9 100644
--- a/test/specs/helpers.canvas.tests.js
+++ b/test/specs/helpers.canvas.tests.js
@@ -28,8 +28,8 @@ describe('Chart.helpers.canvas', function() {
           canvas: null
         });
         // explicitly set canvas and ctx to null since setting it in acquireChart doesn't do anything
-        chart.canvas = null
-        chart.ctx = null
+        chart.canvas = null;
+        chart.ctx = null;
 
         helpers.clearCanvas(chart.canvas, chart.ctx);
       }