-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Create Graphics fixing in dev-2.0 branch. #7829
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
Changes from all commits
b78818b
178d2cb
50d6530
78cb39d
3d7d1c5
c91eb44
3933911
63cc5a0
4fe8021
1f61661
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,11 +63,27 @@ 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); | ||
// `this._pInst` is usually the p5 “sketch” object that owns the global | ||
// `_elements` array. But when an element lives inside an off-screen | ||
// `p5.Graphics` layer, `this._pInst` is that wrapper Graphics object | ||
// instead. The wrapper keeps a back–pointer (`_pInst`) to the real | ||
// sketch but has no `_elements` array of its own. | ||
|
||
let sketch = this._pInst; | ||
|
||
// If `sketch` doesn’t own an `_elements` array it means | ||
// we’re still at the graphics-layer “wrapper”. | ||
// Jump one level up to the real p5 sketch stored in sketch._pInst. | ||
|
||
if (sketch && !sketch._elements && sketch._pInst) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When is this not true / should that path also be handled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi kit,
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) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also minor: I don't think I've seen the
?.
pattern in this codebase before, maybe it's more easy to read to explicitly check existance and then '&&'?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Kit, I think here in dev-2.0 branch, we uses chaining as well.
p5.js/src/webgl/p5.Shader.js
Line 711 in d51184b
I think, if I choose optional chaining operator that will keep our code a bit cleaner than repeating the property name with
&&
. If you just confirm me to choose&&
pattern, I can quickly change it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: No worries, my oversight! Please keep the chaining use but please add a more descriptive incline comment to explain? To make it more easy to understand for new contributors.
Rather than "accessible Outputs" something more like "add accessible outputs if possible; on success...", what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sure. I'll do it now.