Skip to content

Commit ae508c7

Browse files
committed
Selectively attach renderer properties to p5.Graphics
1 parent ae81cdd commit ae508c7

File tree

4 files changed

+27
-49
lines changed

4 files changed

+27
-49
lines changed

preview/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import p5 from '../src/app.js';
2121

2222
const sketch = function (p) {
23+
let g, f;
24+
2325
p.setup = function () {
2426
p.createCanvas(200, 200);
27+
g = p.createGraphics(200, 200);
28+
f = p.createGraphics(200, 200, p.WEBGL);
2529
};
2630

2731
p.draw = function () {
@@ -31,6 +35,10 @@
3135
p.fill('white');
3236
p.textSize(30);
3337
p.text('hello', 10, 30);
38+
39+
// f.fill('red');
40+
f.sphere();
41+
p.image(f, 0, 0);
3442
};
3543
};
3644

src/core/p5.Graphics.js

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,6 @@ class Graphics {
3030
this._pInst = pInst;
3131
this._renderer = new renderers[r](this._pInst, w, h, false, canvas);
3232

33-
// Attach renderer methods
34-
// for(const p of Object.getOwnPropertyNames(p5.renderers[r].prototype)) {
35-
// if(
36-
// p !== 'constructor' &&
37-
// p[0] !== '_' &&
38-
// !(p in this) &&
39-
// typeof this._renderer[p] === 'function'
40-
// ){
41-
// this[p] = this._renderer[p].bind(this._renderer);
42-
// }
43-
// }
44-
45-
// Attach renderer properties
46-
for (const p in this._renderer) {
47-
if(p[0] === '_' || typeof this._renderer[p] === 'function') continue;
48-
Object.defineProperty(this, p, {
49-
get(){
50-
return this._renderer?.[p];
51-
}
52-
})
53-
}
54-
55-
// bind methods and props of p5 to the new object
56-
// for (const p in p5.prototype) {
57-
// if (!this[p]) {
58-
// // console.log(p);
59-
// if (typeof p5.prototype[p] === 'function') {
60-
// this[p] = p5.prototype[p].bind(this);
61-
// } else if(p !== 'deltaTime') {
62-
// this[p] = p5.prototype[p];
63-
// }
64-
// }
65-
// }
6633
p5.prototype._initializeInstanceVariables.apply(this);
6734

6835
this._renderer._applyDefaults();
@@ -73,21 +40,25 @@ class Graphics {
7340
return this._pInst.deltaTime;
7441
}
7542

76-
// get canvas(){
77-
// return this._renderer.canvas;
78-
// }
43+
get canvas(){
44+
return this._renderer?.canvas;
45+
}
7946

80-
// get drawingContext(){
81-
// return this._renderer.drawingContext;
82-
// }
47+
get drawingContext(){
48+
return this._renderer.drawingContext;
49+
}
8350

84-
// get width(){
85-
// return this._renderer.width;
86-
// }
51+
get width(){
52+
return this._renderer?.width;
53+
}
54+
55+
get height(){
56+
return this._renderer?.height;
57+
}
8758

88-
// get height(){
89-
// return this._renderer.height;
90-
// }
59+
get pixels(){
60+
return this._renderer?.pixels;
61+
}
9162

9263
pixelDensity(val){
9364
let returnValue;

src/io/files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ function files(p5, fn){
19051905
// otherwise, parse the arguments
19061906

19071907
// if first param is a p5Graphics, then saveCanvas
1908-
fn.saveCanvas(args[0].elt, args[1], args[2]);
1908+
fn.saveCanvas(args[0].canvas, args[1], args[2]);
19091909
return;
19101910
} else if (args.length === 1 && typeof args[0] === 'string') {
19111911
// if 1st param is String and only one arg, assume it is canvas filename

src/webgl/p5.Texture.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ class Texture {
9696
textureData = this.src.canvas;
9797
} else if (
9898
this.isSrcMediaElement ||
99-
this.isSrcP5Graphics ||
100-
this.isSrcHTMLElement
99+
this.isSrcHTMLElement
101100
) {
102101
// if param is a video HTML element
103102
textureData = this.src.elt;
104-
} else if (this.isSrcP5Renderer) {
103+
} else if (this.isSrcP5Graphics || this.isSrcP5Renderer) {
105104
textureData = this.src.canvas;
106105
} else if (this.isImageData) {
107106
textureData = this.src;

0 commit comments

Comments
 (0)