Skip to content

Commit 48dfe18

Browse files
Merge branch 'chore/upgrade-eslint' of https://github.com/error-four-o-four/forked-p5.js into chore/upgrade-eslint
2 parents 39ced90 + 82177f2 commit 48dfe18

File tree

8 files changed

+57
-26
lines changed

8 files changed

+57
-26
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/dom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1932,7 +1932,7 @@ function dom(p5, fn){
19321932
* let y = i * 20;
19331933
*
19341934
* // Draw the image.
1935-
* image(img, 0, y, 100, 100);
1935+
* image(images[i], 0, y, 100, 100);
19361936
* }
19371937
*
19381938
* describe('A gray square with a file input beneath it. If the user selects multiple image files to load, they are displayed on the square.');

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) {

src/dom/p5.MediaElement.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,15 +1335,6 @@ function media(p5, fn){
13351335
elt.appendChild(sourceEl);
13361336
}
13371337

1338-
// If callback is provided, attach to element
1339-
if (typeof callback === 'function') {
1340-
const callbackHandler = () => {
1341-
callback();
1342-
elt.removeEventListener('canplaythrough', callbackHandler);
1343-
};
1344-
elt.addEventListener('canplaythrough', callbackHandler);
1345-
}
1346-
13471338
const mediaEl = addElement(elt, pInst, true);
13481339
mediaEl.loadedmetadata = false;
13491340

@@ -1362,6 +1353,15 @@ function media(p5, fn){
13621353
mediaEl.loadedmetadata = true;
13631354
});
13641355

1356+
// If callback is provided, attach to element
1357+
if (typeof callback === 'function') {
1358+
const callbackHandler = () => {
1359+
callback(mediaEl);
1360+
elt.removeEventListener('canplaythrough', callbackHandler);
1361+
};
1362+
elt.addEventListener('canplaythrough', callbackHandler);
1363+
}
1364+
13651365
return mediaEl;
13661366
}
13671367

src/type/p5.Font.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,9 @@ async function create(pInst, name, path, descriptors, rawFont) {
950950
// add it to the document
951951
document.fonts.add(face);
952952

953+
// ensure the font is ready to be rendered
954+
await document.fonts.ready;
955+
953956
// return a new p5.Font
954957
return new Font(pInst, face, name, path, rawFont);
955958
}

0 commit comments

Comments
 (0)