Skip to content

Commit 3eae276

Browse files
authored
Merge pull request #7829 from perminder-17/createGraphics
Create Graphics fixing in dev-2.0 branch.
2 parents aab1b0b + 1f61661 commit 3eae276

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

lib/empty-example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
background-color: #1b1b1b;
1313
}
1414
</style>
15-
<script src="../p5.rollup.min.js"></script>
15+
<script src="../p5.min.js"></script>
1616
<!-- <script src="../addons/p5.sound.js"></script> -->
1717
<script src="sketch.js"></script>
1818
</head>

src/core/p5.Graphics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Graphics {
3030
const r = renderer || constants.P2D;
3131

3232
this._pInst = pInst;
33-
this._renderer = new renderers[r](this._pInst, w, h, false, canvas);
33+
this._renderer = new renderers[r](this, w, h, false, canvas);
3434

3535
this._initializeInstanceVariables(this);
3636

src/core/p5.Renderer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
* @for p5
55
*/
66

7+
/**
8+
* `pInst` may be:
9+
*
10+
* The main sketch-wide `p5` instance (global canvas), or
11+
* an off-screen `p5.Graphics` wrapper.
12+
*
13+
* Therefore a renderer must only call properties / methods that exist
14+
* on both objects.
15+
*/
16+
717
import { Color } from '../color/p5.Color';
818
import * as constants from '../core/constants';
919
import { Image } from '../image/p5.Image';

src/core/p5.Renderer2D.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class Renderer2D extends Renderer {
7575
}
7676
// Set and return p5.Element
7777
this.wrappedElt = new Element(this.elt, this._pInst);
78-
7978
this.clipPath = null;
8079
}
8180

@@ -178,9 +177,10 @@ class Renderer2D extends Renderer {
178177
// create background rect
179178
const color = this._pInst.color(...args);
180179

181-
//accessible Outputs
182-
if (this._pInst._addAccsOutput()) {
183-
this._pInst._accsBackground(color._getRGBA([255, 255, 255, 255]));
180+
// Add accessible outputs if the method exists; on success,
181+
// set the accessible output background to white.
182+
if (this._pInst._addAccsOutput?.()) {
183+
this._pInst._accsBackground?.(color._getRGBA([255, 255, 255, 255]));
184184
}
185185

186186
const newFill = color.toString();
@@ -211,9 +211,10 @@ class Renderer2D extends Renderer {
211211
const color = this.states.fillColor;
212212
this._setFill(color.toString());
213213

214-
//accessible Outputs
215-
if (this._pInst._addAccsOutput()) {
216-
this._pInst._accsCanvasColors('fill', color._getRGBA([255, 255, 255, 255]));
214+
// Add accessible outputs if the method exists; on success,
215+
// set the accessible output background to white.
216+
if (this._pInst._addAccsOutput?.()) {
217+
this._pInst._accsCanvasColors?.('fill', color._getRGBA([255, 255, 255, 255]));
217218
}
218219
}
219220

@@ -222,9 +223,10 @@ class Renderer2D extends Renderer {
222223
const color = this.states.strokeColor;
223224
this._setStroke(color.toString());
224225

225-
//accessible Outputs
226-
if (this._pInst._addAccsOutput()) {
227-
this._pInst._accsCanvasColors('stroke', color._getRGBA([255, 255, 255, 255]));
226+
// Add accessible outputs if the method exists; on success,
227+
// set the accessible output background to white.
228+
if (this._pInst._addAccsOutput?.()) {
229+
this._pInst._accsCanvasColors?.('stroke', color._getRGBA([255, 255, 255, 255]));
228230
}
229231
}
230232

src/dom/p5.Element.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,27 @@ class Element {
6363
}
6464
}
6565

66-
// delete the reference in this._pInst._elements
67-
const index = this._pInst._elements.indexOf(this);
68-
if (index !== -1) {
69-
this._pInst._elements.splice(index, 1);
66+
// `this._pInst` is usually the p5 “sketch” object that owns the global
67+
// `_elements` array. But when an element lives inside an off-screen
68+
// `p5.Graphics` layer, `this._pInst` is that wrapper Graphics object
69+
// instead. The wrapper keeps a back–pointer (`_pInst`) to the real
70+
// sketch but has no `_elements` array of its own.
71+
72+
let sketch = this._pInst;
73+
74+
// If `sketch` doesn’t own an `_elements` array it means
75+
// we’re still at the graphics-layer “wrapper”.
76+
// Jump one level up to the real p5 sketch stored in sketch._pInst.
77+
78+
if (sketch && !sketch._elements && sketch._pInst) {
79+
sketch = sketch._pInst; // climb one level up
7080
}
81+
82+
if (sketch && sketch._elements) { // only if the array exists
83+
const i = sketch._elements.indexOf(this);
84+
if (i !== -1) sketch._elements.splice(i, 1);
85+
}
86+
7187

7288
// deregister events
7389
for (let ev in this._events) {

0 commit comments

Comments
 (0)