Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixing-tests
  • Loading branch information
perminder-17 committed May 18, 2025
commit 78cb39dbd6e67b5187f538e5d4341362d9c7d59f
22 changes: 16 additions & 6 deletions lib/empty-example/sketch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
let g;

function setup() {
// put setup code here
}

function draw() {
// put drawing code here
}
createCanvas(400, 400);

g = createGraphics(200, 200);
}

function draw() {
g.background(0);
g.stroke(255, 0, 0);
g.strokeWeight(5);
g.noFill();
g.bezier(0, 0, 100, 0, 0, 100, 200, 200);
//Comment out this line and for some reason the bezier gets drawn (to the canvas instead of the graphics object)
image(g, 0, 0, 400, 400);
}
12 changes: 9 additions & 3 deletions src/dom/p5.Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ class Element {
}

// delete the reference in this._pInst._elements
const index = this._pInst._elements.indexOf(this);
if (index !== -1) {
this._pInst._elements.splice(index, 1);
let sketch = this._pInst;
if (sketch && !sketch._elements && sketch._pInst) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a little more inline docs could be useful here to explain the logic

sketch = sketch._pInst; // climb one level up
}

if (sketch && sketch._elements) { // only if the array exists
const i = sketch._elements.indexOf(this);
if (i !== -1) sketch._elements.splice(i, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is this not true / should that path also be handled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi kit,
That condition is false in two cases:-

  1. Sketch is null / undefined, so there isn’t any p5 sketch to talk to.

  2. Sketch exists but it never set up its _elements list.

In both sutiotions there is no list we can remove ourselves from, so the safest action is to do nothing and just reutrn. Trying to touch a list that isn’t there would crash, so skipping the code is exactly what we want.

}


// deregister events
for (let ev in this._events) {
Expand Down