Skip to content

Commit 3a356d5

Browse files
committed
Move DOM initialization from p5.Renderer to individual renderers
1 parent 282cabb commit 3a356d5

File tree

5 files changed

+63
-38
lines changed

5 files changed

+63
-38
lines changed

src/core/p5.Renderer.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,9 @@ p5.Renderer = class Renderer {
2121
constructor(elt, pInst, isMainCanvas) {
2222
this._pInst = this._pixelsState = pInst;
2323
this._events = {};
24-
this.canvas = elt;
25-
this.width = this.canvas.offsetWidth;
26-
this.height = this.canvas.offsetHeight;
2724

2825
if (isMainCanvas) {
2926
this._isMainCanvas = true;
30-
// for pixel method sharing with pimage
31-
this._pInst._curElement = this;
32-
this._pInst.canvas = this.canvas;
33-
this._pInst.width = this.width;
34-
this._pInst.height = this.height;
35-
} else {
36-
// hide if offscreen buffer by default
37-
this.canvas.style.display = 'none';
38-
this._styles = []; // non-main elt styles stored in p5.Renderer
3927
}
4028

4129
// Renderer state machine
@@ -66,6 +54,13 @@ p5.Renderer = class Renderer {
6654
this._curveTightness = 0;
6755
}
6856

57+
createCanvas(w, h) {
58+
this.width = w;
59+
this.height = h;
60+
this._pInst.width = this.width;
61+
this._pInst.height = this.height;
62+
}
63+
6964
// Makes a shallow copy of the current states
7065
// and push it into the push pop stack
7166
push() {
@@ -108,20 +103,16 @@ p5.Renderer = class Renderer {
108103
/**
109104
* Resize our canvas element.
110105
*/
111-
resize (w, h) {
106+
resize(w, h) {
112107
this.width = w;
113108
this.height = h;
114-
this.canvas.width = w * this._pInst._pixelDensity;
115-
this.canvas.height = h * this._pInst._pixelDensity;
116-
this.canvas.style.width = `${w}px`;
117-
this.canvas.style.height = `${h}px`;
118109
if (this._isMainCanvas) {
119110
this._pInst.width = this.width;
120111
this._pInst.height = this.height;
121112
}
122113
}
123114

124-
get (x, y, w, h) {
115+
get(x, y, w, h) {
125116
const pixelsState = this._pixelsState;
126117
const pd = pixelsState._pixelDensity;
127118
const canvas = this.canvas;

src/core/p5.Renderer2D.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ const styleEmpty = 'rgba(0,0,0,0)';
1515
class Renderer2D extends Renderer {
1616
constructor(elt, pInst, isMainCanvas) {
1717
super(elt, pInst, isMainCanvas);
18+
this.elt = elt;
19+
this.canvas = elt;
1820
this.drawingContext = this.canvas.getContext('2d');
1921
this._pInst.drawingContext = this.drawingContext;
20-
this.elt = elt;
22+
23+
if (isMainCanvas) {
24+
// for pixel method sharing with pimage
25+
this._pInst._curElement = this;
26+
this._pInst.canvas = this.canvas;
27+
} else {
28+
// hide if offscreen buffer by default
29+
this.canvas.style.display = 'none';
30+
}
2131

2232
// Extend renderer with methods of p5.Element with getters
2333
this.wrappedElt = new p5.Element(elt, pInst);
@@ -32,6 +42,18 @@ class Renderer2D extends Renderer {
3242
}
3343
}
3444

45+
// NOTE: renderer won't be created until instance createCanvas was called
46+
// This createCanvas should handle the HTML stuff while other createCanvas
47+
// be generic
48+
createCanvas(w, h, canvas) {
49+
super.createCanvas(w, h);
50+
// this.canvas = this.elt = canvas || document.createElement('canvas');
51+
// this.drawingContext = this.canvas.getContext('2d');
52+
// this._pInst.drawingContext = this.drawingContext;
53+
54+
return this.wrappedElt;
55+
}
56+
3557
getFilterGraphicsLayer() {
3658
// create hidden webgl renderer if it doesn't exist
3759
if (!this.filterGraphicsLayer) {
@@ -76,6 +98,10 @@ class Renderer2D extends Renderer {
7698

7799
resize(w, h) {
78100
super.resize(w, h);
101+
this.canvas.width = w * this._pInst._pixelDensity;
102+
this.canvas.height = h * this._pInst._pixelDensity;
103+
this.canvas.style.width = `${w}px`;
104+
this.canvas.style.height = `${h}px`;
79105
this.drawingContext.scale(
80106
this._pInst._pixelDensity,
81107
this._pInst._pixelDensity

src/core/rendering.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ p5.prototype.createCanvas = function (w, h, renderer, canvas) {
133133
p5._validateParameters('createCanvas', arguments);
134134
//optional: renderer, otherwise defaults to p2d
135135

136+
let selectedRenderer = constants.P2D
137+
// Check third argument whether it is renderer constants
138+
if(Reflect.ownKeys(renderers).includes(renderer)){
139+
selectedRenderer = renderer;
140+
}
141+
142+
143+
136144
let r;
137145
if (arguments[2] instanceof HTMLCanvasElement) {
138146
renderer = constants.P2D;
@@ -217,29 +225,13 @@ p5.prototype.createCanvas = function (w, h, renderer, canvas) {
217225
}
218226

219227
// Init our graphics renderer
220-
//webgl mode
221-
// if (r === constants.WEBGL) {
222-
// this._setProperty('_renderer', new p5.RendererGL(c, this, true));
223-
// this._elements.push(this._renderer);
224-
// NOTE: these needs to be taken cared of below
225-
// const dimensions =
226-
// this._renderer._adjustDimensions(w, h);
227-
// w = dimensions.adjustedWidth;
228-
// h = dimensions.adjustedHeight;
229-
// } else {
230-
// //P2D mode
231-
// if (!this._defaultGraphicsCreated) {
232-
// this._setProperty('_renderer', new p5.Renderer2D(c, this, true));
233-
// this._defaultGraphicsCreated = true;
234-
// this._elements.push(this._renderer);
235-
// }
236-
// }
237228
this._renderer = new renderers[r](c, this, true);
238229
this._defaultGraphicsCreated = true;
239230
this._elements.push(this._renderer);
240-
// Instead of resize, just create with the right size in the first place
241231
this._renderer.resize(w, h);
242232
this._renderer._applyDefaults();
233+
this._renderer.createCanvas(w, h, canvas);
234+
// return this._renderer.createCanvas(w, h, canvas);
243235
return this._renderer;
244236
};
245237

src/webgl/p5.RendererGL.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,8 @@ export function readPixelWebGL(
435435
p5.RendererGL = class RendererGL extends Renderer {
436436
constructor(elt, pInst, isMainCanvas, attr) {
437437
super(elt, pInst, isMainCanvas);
438+
this.elt = elt;
439+
this.canvas = elt;
438440
this._setAttributeDefaults(pInst);
439441
this._initContext();
440442
this.isP3D = true; //lets us know we're in 3d mode
@@ -447,6 +449,15 @@ p5.RendererGL = class RendererGL extends Renderer {
447449
this.GL = this.drawingContext;
448450
this._pInst.drawingContext = this.drawingContext;
449451

452+
if (isMainCanvas) {
453+
// for pixel method sharing with pimage
454+
this._pInst._curElement = this;
455+
this._pInst.canvas = this.canvas;
456+
} else {
457+
// hide if offscreen buffer by default
458+
this.canvas.style.display = 'none';
459+
}
460+
450461
// Push/pop state
451462
this.states.uModelMatrix = new p5.Matrix();
452463
this.states.uViewMatrix = new p5.Matrix();
@@ -1432,6 +1443,10 @@ p5.RendererGL = class RendererGL extends Renderer {
14321443
*/
14331444
resize(w, h) {
14341445
Renderer.prototype.resize.call(this, w, h);
1446+
this.canvas.width = w * this._pInst._pixelDensity;
1447+
this.canvas.height = h * this._pInst._pixelDensity;
1448+
this.canvas.style.width = `${w}px`;
1449+
this.canvas.style.height = `${h}px`;
14351450
this._origViewport = {
14361451
width: this.GL.drawingBufferWidth,
14371452
height: this.GL.drawingBufferHeight

utils/sample-linter.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Object.keys(dataDoc.consts).forEach(c => {
2121
});
2222

2323
dataDoc.classitems
24-
.find(ci => ci.name === 'keyCode' && ci.class === 'p5')
24+
.find(ci => ci.name === 'keyCode')
2525
.description.match(/[A-Z\r\n, _]{10,}/m)[0]
2626
.match(/[A-Z_]+/gm)
2727
.forEach(c => {
@@ -248,6 +248,7 @@ function splitLines(text) {
248248

249249
eslintFiles(null, process.argv.slice(2))
250250
.then(result => {
251+
if(result === true) process.exit(0);
251252
console.log(result.output);
252253
process.exit(result.report.errorCount === 0 ? 0 : 1);
253254
});

0 commit comments

Comments
 (0)