From c9452a034bcf1c6713e5a065e6d19f0f8c983230 Mon Sep 17 00:00:00 2001 From: Bianca Fonseca Date: Sun, 19 May 2024 17:39:05 +0100 Subject: [PATCH] Fix #7049: P5.Graphics.remove() doesn't release all associated resources The proposed fix involves modifying the p5.Graphics.remove() method by setting this._renderer, this.canvas, and this.elt to undefined. Additionally, a boolean flag doubleClickedBool is introduced to prevent the execution of the draw() function after a double-click event ensuring that the graphics object is not displayed again after removal This provides a smoother user experience by preventing the graphics object from reappearing unexpectedly. --- src/core/p5.Graphics.js | 8 +++++++- test/unit/core/p5.Graphics.js | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/core/p5.Graphics.js b/src/core/p5.Graphics.js index 4d8e848ff1..dccaaca75d 100644 --- a/src/core/p5.Graphics.js +++ b/src/core/p5.Graphics.js @@ -331,6 +331,7 @@ p5.Graphics = class extends p5.Element { * // Double-click to remove the p5.Graphics object. * * let pg; + * let doubleClickedBool = false; * * function setup() { * createCanvas(100, 100); @@ -350,7 +351,7 @@ p5.Graphics = class extends p5.Element { * * // Display the p5.Graphics object if * // it's available. - * if (pg) { + * if (!doubleClickedBool) { * image(pg, 20, 20); * } * } @@ -359,6 +360,7 @@ p5.Graphics = class extends p5.Element { * // the user double-clicks. * function doubleClicked() { * pg.remove(); + * doubleClickedBool = true; * } * * @@ -374,6 +376,10 @@ p5.Graphics = class extends p5.Element { for (const elt_ev in this._events) { this.elt.removeEventListener(elt_ev, this._events[elt_ev]); } + + this._renderer = undefined; + this.canvas = undefined; + this.elt = undefined; } diff --git a/test/unit/core/p5.Graphics.js b/test/unit/core/p5.Graphics.js index 634f24e4c7..216f4e9d26 100644 --- a/test/unit/core/p5.Graphics.js +++ b/test/unit/core/p5.Graphics.js @@ -184,4 +184,14 @@ suite('Graphics', function() { assert(graph.height, 100); }); }); + + suite('p5.Graphics.remove()', function() { + test('it sets properties to undefined after removal', function() { + var graph = myp5.createGraphics(10, 17); + graph.remove(); + assert.isUndefined(graph.canvas); + assert.isUndefined(graph._renderer); + assert.isUndefined(graph.elt); + }); + }); });