From f223d9ec8426cc0ccff5c25b14c4c478b5768d6a Mon Sep 17 00:00:00 2001 From: capGoblin Date: Sat, 23 Dec 2023 22:10:32 +0530 Subject: [PATCH 1/7] limit textures in p5.Graphics --- src/core/p5.Graphics.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/core/p5.Graphics.js b/src/core/p5.Graphics.js index 2de8fe011b..9d5a003077 100644 --- a/src/core/p5.Graphics.js +++ b/src/core/p5.Graphics.js @@ -60,6 +60,13 @@ p5.Graphics = class extends p5.Element { if (r === constants.WEBGL) { this._renderer = new p5.RendererGL(this.canvas, this, false); + const gl = this._renderer.GL; + const maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); + const maxAllowedPixelDimensions = Math.floor( + maxTextureSize / this._pixelDensity + ); + w = Math.min(w, maxAllowedPixelDimensions); + h = Math.min(h, maxAllowedPixelDimensions); } else { this._renderer = new p5.Renderer2D(this.canvas, this, false); } From 7c92d81fa8fa2fb66b251542f233fe80299bc503 Mon Sep 17 00:00:00 2001 From: capGoblin Date: Sun, 24 Dec 2023 22:13:49 +0530 Subject: [PATCH 2/7] limit textures in p5.Framebuffer --- src/webgl/p5.Framebuffer.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/webgl/p5.Framebuffer.js b/src/webgl/p5.Framebuffer.js index 7003a12fc1..9eb89d8ec4 100644 --- a/src/webgl/p5.Framebuffer.js +++ b/src/webgl/p5.Framebuffer.js @@ -131,9 +131,19 @@ class Framebuffer { console.warn('Antialiasing is unsupported in a WebGL 1 context'); this.antialias = false; } + this.density = settings.density || target.pixelDensity(); + const gl = target._renderer.GL; + this.gl = gl; if (settings.width && settings.height) { - this.width = settings.width; - this.height = settings.height; + const maxTextureSize = this.gl.getParameter( + this.gl.MAX_TEXTURE_SIZE); + const maxAllowedPixelDimensions = Math.floor( + maxTextureSize / this.density + ); + const width = Math.min(settings.width, maxAllowedPixelDimensions); + const height = Math.min(settings.height, maxAllowedPixelDimensions); + this.width = width; + this.height = height; this.autoSized = false; } else { if ((settings.width === undefined) !== (settings.height === undefined)) { @@ -147,11 +157,6 @@ class Framebuffer { this.height = target.height; this.autoSized = true; } - this.density = settings.density || target.pixelDensity(); - - const gl = target._renderer.GL; - this.gl = gl; - this._checkIfFormatsAvailable(); if (settings.stencil && !this.useDepth) { From e755c68c6819db25ad1c164de1473b2ac9663001 Mon Sep 17 00:00:00 2001 From: capGoblin Date: Sat, 30 Dec 2023 21:15:18 +0530 Subject: [PATCH 3/7] refactor code --- src/core/environment.js | 2 +- src/core/main.js | 1 + src/core/p5.Graphics.js | 11 ++++------- src/core/rendering.js | 7 ++++++- src/webgl/p5.Framebuffer.js | 13 ++++--------- src/webgl/p5.RendererGL.js | 28 ++++++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/core/environment.js b/src/core/environment.js index 4b84fb8c2e..7acb8c35d5 100644 --- a/src/core/environment.js +++ b/src/core/environment.js @@ -997,7 +997,7 @@ p5.prototype.pixelDensity = function(val) { let returnValue; if (typeof val === 'number') { if (val !== this._pixelDensity) { - this._pixelDensity = val; + this._pixelDensity = this._maxAllowedPixelDimensions = val; } returnValue = this; this.resizeCanvas(this.width, this.height, true); // as a side effect, it will clear the canvas diff --git a/src/core/main.js b/src/core/main.js index dd61bb6862..bb81cb7ab4 100644 --- a/src/core/main.js +++ b/src/core/main.js @@ -169,6 +169,7 @@ class p5 { this._preloadDone = false; // for handling hidpi this._pixelDensity = Math.ceil(window.devicePixelRatio) || 1; + this._maxAllowedPixelDimensions = 0; this._userNode = node; this._curElement = null; this._elements = []; diff --git a/src/core/p5.Graphics.js b/src/core/p5.Graphics.js index 9d5a003077..90c1d8edba 100644 --- a/src/core/p5.Graphics.js +++ b/src/core/p5.Graphics.js @@ -60,13 +60,10 @@ p5.Graphics = class extends p5.Element { if (r === constants.WEBGL) { this._renderer = new p5.RendererGL(this.canvas, this, false); - const gl = this._renderer.GL; - const maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); - const maxAllowedPixelDimensions = Math.floor( - maxTextureSize / this._pixelDensity - ); - w = Math.min(w, maxAllowedPixelDimensions); - h = Math.min(h, maxAllowedPixelDimensions); + const { adjustedWidth, adjustedHeight } = + this._renderer._adjustDimensions(w, h); + w = adjustedWidth; + h = adjustedHeight; } else { this._renderer = new p5.Renderer2D(this.canvas, this, false); } diff --git a/src/core/rendering.js b/src/core/rendering.js index 89590a9c9f..ebbec545ef 100644 --- a/src/core/rendering.js +++ b/src/core/rendering.js @@ -143,9 +143,14 @@ p5.prototype.createCanvas = function(w, h, renderer, canvas) { // Init our graphics renderer //webgl mode + let adjustedWidth, adjustedHeight; if (r === constants.WEBGL) { this._setProperty('_renderer', new p5.RendererGL(c, this, true)); this._elements.push(this._renderer); + const dimensions = + this._renderer._adjustDimensions(w, h); + adjustedWidth = dimensions.adjustedWidth; + adjustedHeight = dimensions.adjustedHeight; } else { //P2D mode if (!this._defaultGraphicsCreated) { @@ -154,7 +159,7 @@ p5.prototype.createCanvas = function(w, h, renderer, canvas) { this._elements.push(this._renderer); } } - this._renderer.resize(w, h); + this._renderer.resize(adjustedWidth, adjustedHeight); this._renderer._applyDefaults(); return this._renderer; }; diff --git a/src/webgl/p5.Framebuffer.js b/src/webgl/p5.Framebuffer.js index 9a85ce4c90..74f094eebd 100644 --- a/src/webgl/p5.Framebuffer.js +++ b/src/webgl/p5.Framebuffer.js @@ -135,15 +135,10 @@ class Framebuffer { const gl = target._renderer.GL; this.gl = gl; if (settings.width && settings.height) { - const maxTextureSize = this.gl.getParameter( - this.gl.MAX_TEXTURE_SIZE); - const maxAllowedPixelDimensions = Math.floor( - maxTextureSize / this.density - ); - const width = Math.min(settings.width, maxAllowedPixelDimensions); - const height = Math.min(settings.height, maxAllowedPixelDimensions); - this.width = width; - this.height = height; + const dimensions = + target._renderer._adjustDimensions(settings.width, settings.height); + this.width = dimensions.adjustedWidth; + this.height = dimensions.adjustedHeight; this.autoSized = false; } else { if ((settings.width === undefined) !== (settings.height === undefined)) { diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 097c9ab8c1..1e66831020 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -787,6 +787,34 @@ p5.RendererGL = class RendererGL extends p5.Renderer { } } + _adjustDimensions(width, height) { + const gl = this.drawingContext; + if (!this._maxTextureSize) { + this._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); + } + let maxTextureSize = this._maxTextureSize; + let maxAllowedPixleDimensions = p5.prototype._maxAllowedPixelDimensions; + + maxAllowedPixleDimensions = Math.floor( + maxTextureSize / this.pixelDensity() + ); + let adjustedWidth = Math.min( + width, maxAllowedPixleDimensions + ); + let adjustedHeight = Math.min( + height, maxAllowedPixleDimensions + ); + + if (adjustedWidth !== width || adjustedHeight !== height) { + console.warn( + 'Warning: The requested width/height exceeds hardware limits. ' + + `Adjusting dimensions to width: ${adjustedWidth}, height: ${adjustedHeight}.` + ); + } + + return { adjustedWidth, adjustedHeight }; + } + //This is helper function to reset the context anytime the attributes //are changed with setAttributes() From d537edbfb58efa3cb2d153361dd4aac81d34aced Mon Sep 17 00:00:00 2001 From: capGoblin Date: Sun, 31 Dec 2023 01:20:24 +0530 Subject: [PATCH 4/7] fix w and h set to zero --- src/core/rendering.js | 7 +++---- src/webgl/p5.RendererGL.js | 8 ++++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/core/rendering.js b/src/core/rendering.js index ebbec545ef..09e9b004c8 100644 --- a/src/core/rendering.js +++ b/src/core/rendering.js @@ -143,14 +143,13 @@ p5.prototype.createCanvas = function(w, h, renderer, canvas) { // Init our graphics renderer //webgl mode - let adjustedWidth, adjustedHeight; if (r === constants.WEBGL) { this._setProperty('_renderer', new p5.RendererGL(c, this, true)); this._elements.push(this._renderer); const dimensions = this._renderer._adjustDimensions(w, h); - adjustedWidth = dimensions.adjustedWidth; - adjustedHeight = dimensions.adjustedHeight; + w = dimensions.adjustedWidth; + h = dimensions.adjustedHeight; } else { //P2D mode if (!this._defaultGraphicsCreated) { @@ -159,7 +158,7 @@ p5.prototype.createCanvas = function(w, h, renderer, canvas) { this._elements.push(this._renderer); } } - this._renderer.resize(adjustedWidth, adjustedHeight); + this._renderer.resize(w, h); this._renderer._applyDefaults(); return this._renderer; }; diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 1e66831020..5048645258 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -793,16 +793,16 @@ p5.RendererGL = class RendererGL extends p5.Renderer { this._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); } let maxTextureSize = this._maxTextureSize; - let maxAllowedPixleDimensions = p5.prototype._maxAllowedPixelDimensions; + let maxAllowedPixelDimensions = p5.prototype._maxAllowedPixelDimensions; - maxAllowedPixleDimensions = Math.floor( + maxAllowedPixelDimensions = Math.floor( maxTextureSize / this.pixelDensity() ); let adjustedWidth = Math.min( - width, maxAllowedPixleDimensions + width, maxAllowedPixelDimensions ); let adjustedHeight = Math.min( - height, maxAllowedPixleDimensions + height, maxAllowedPixelDimensions ); if (adjustedWidth !== width || adjustedHeight !== height) { From d7d794ec335720f343c6891f6ce197452ba74e62 Mon Sep 17 00:00:00 2001 From: capGoblin Date: Mon, 15 Jan 2024 00:41:55 +0530 Subject: [PATCH 5/7] add resize for framebuffer & resizeCanvas w/ unit tests --- src/core/rendering.js | 6 ++++++ src/webgl/p5.Framebuffer.js | 4 ++++ src/webgl/p5.RendererGL.js | 8 ++++++-- test/unit/core/p5.Graphics.js | 13 +++++++++++++ test/unit/core/rendering.js | 26 ++++++++++++++++++++++++++ test/unit/webgl/p5.Framebuffer.js | 30 ++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/core/rendering.js b/src/core/rendering.js index 09e9b004c8..e84221c767 100644 --- a/src/core/rendering.js +++ b/src/core/rendering.js @@ -202,6 +202,12 @@ p5.prototype.resizeCanvas = function(w, h, noRedraw) { } this.width = w; this.height = h; + if (!(this._renderer instanceof p5.Renderer2D)) { + const dimensions = + this._renderer._adjustDimensions(w, h); + w = dimensions.adjustedWidth; + h = dimensions.adjustedHeight; + } // Make sure width and height are updated before the renderer resizes so // that framebuffers updated from the resize read the correct size this._renderer.resize(w, h); diff --git a/src/webgl/p5.Framebuffer.js b/src/webgl/p5.Framebuffer.js index 74f094eebd..28b0c6ec61 100644 --- a/src/webgl/p5.Framebuffer.js +++ b/src/webgl/p5.Framebuffer.js @@ -227,6 +227,10 @@ class Framebuffer { */ resize(width, height) { this.autoSized = false; + const dimensions = + this.target._renderer._adjustDimensions(width, height); + width = dimensions.adjustedWidth; + height = dimensions.adjustedHeight; this.width = width; this.height = height; this._handleResize(); diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 5048645258..65ee80137e 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -787,10 +787,14 @@ p5.RendererGL = class RendererGL extends p5.Renderer { } } - _adjustDimensions(width, height) { + _getParam() { const gl = this.drawingContext; + return gl.getParameter(gl.MAX_TEXTURE_SIZE); + } + + _adjustDimensions(width, height) { if (!this._maxTextureSize) { - this._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE); + this._maxTextureSize = this._getParam(); } let maxTextureSize = this._maxTextureSize; let maxAllowedPixelDimensions = p5.prototype._maxAllowedPixelDimensions; diff --git a/test/unit/core/p5.Graphics.js b/test/unit/core/p5.Graphics.js index d722382d95..dded0413b1 100644 --- a/test/unit/core/p5.Graphics.js +++ b/test/unit/core/p5.Graphics.js @@ -164,5 +164,18 @@ suite('Graphics', function() { graph.resizeCanvas(19, 16); assertValidPixels(graph, 19, 16, 2); }); + + test('it resizes the canvas and the graphics based on max texture size', function() { + let glStub; + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + + var graph = myp5.createGraphics(200, 200, myp5.WEBGL); + graph.pixelDensity(1); + assertValidGraphSizes(graph, 100, 100, 1); + + glStub.restore(); + }); }); }); diff --git a/test/unit/core/rendering.js b/test/unit/core/rendering.js index 0772f77350..fafd011396 100644 --- a/test/unit/core/rendering.js +++ b/test/unit/core/rendering.js @@ -89,6 +89,32 @@ suite('Rendering', function() { assert.equal(fbo.width, 5); assert.equal(fbo.height, 15); }); + + test('should resize the dimensions of canvas based on max texture size', function() { + let glStub; + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + myp5.createCanvas(200, 200, myp5.WEBGL); + assert.equal(myp5.canvas.width, 100); + assert.equal(myp5.canvas.height, 100); + + glStub.restore(); + }); + + test('should resize the dimensions of canvas by resizeCanvas based on max texture size', function() { + + let glStub; + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + myp5.createCanvas(10, 10, myp5.WEBGL); + myp5.resizeCanvas(200, 200); + assert.equal(myp5.canvas.width, 100); + assert.equal(myp5.canvas.height, 100); + + glStub.restore(); + }); }); suite('p5.prototype.blendMode', function() { diff --git a/test/unit/webgl/p5.Framebuffer.js b/test/unit/webgl/p5.Framebuffer.js index e87e6240e7..9b1a6afbb4 100644 --- a/test/unit/webgl/p5.Framebuffer.js +++ b/test/unit/webgl/p5.Framebuffer.js @@ -162,6 +162,36 @@ suite('p5.Framebuffer', function() { // The texture should not be recreated expect(fbo.color.rawTexture()).to.equal(oldTexture); }); + + test('resizes the framebuffer using its constructor based on max texture size', function() { + let glStub; + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + + myp5.createCanvas(10, 10, myp5.WEBGL); + const fbo = myp5.createFramebuffer({ width: 200, height: 200 }); + expect(fbo.width).to.equal(100); + expect(fbo.height).to.equal(100); + + glStub.restore(); + }); + + test('resizes the framebuffer using resize method based on max texture size', function() { + let glStub; + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + + myp5.createCanvas(10, 10, myp5.WEBGL); + const fbo = myp5.createFramebuffer({ width: 10, height: 10 }); + myp5.resize(200, 200); + expect(fbo.width).to.equal(100); + expect(fbo.height).to.equal(100); + + + glStub.restore(); + }); }); }); From fa2e6156a02b23ffb1730e92153f225bf2f4b39f Mon Sep 17 00:00:00 2001 From: capGoblin Date: Thu, 18 Jan 2024 23:42:38 +0530 Subject: [PATCH 6/7] fix unit tests --- src/core/rendering.js | 6 +++--- test/unit/core/p5.Graphics.js | 22 ++++++++++------------ test/unit/core/rendering.js | 31 +++++++++++++------------------ test/unit/webgl/p5.Framebuffer.js | 15 ++++----------- 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/src/core/rendering.js b/src/core/rendering.js index e84221c767..6df27af7f9 100644 --- a/src/core/rendering.js +++ b/src/core/rendering.js @@ -200,14 +200,14 @@ p5.prototype.resizeCanvas = function(w, h, noRedraw) { props[key] = val; } } - this.width = w; - this.height = h; - if (!(this._renderer instanceof p5.Renderer2D)) { + if (this._renderer instanceof p5.RendererGL) { const dimensions = this._renderer._adjustDimensions(w, h); w = dimensions.adjustedWidth; h = dimensions.adjustedHeight; } + this.width = w; + this.height = h; // Make sure width and height are updated before the renderer resizes so // that framebuffers updated from the resize read the correct size this._renderer.resize(w, h); diff --git a/test/unit/core/p5.Graphics.js b/test/unit/core/p5.Graphics.js index dded0413b1..76492ad6e3 100644 --- a/test/unit/core/p5.Graphics.js +++ b/test/unit/core/p5.Graphics.js @@ -47,6 +47,16 @@ suite('Graphics', function() { var graph = myp5.createGraphics(10, 17); assert.isObject(graph); }); + + test('it resizes the graphics based on max texture size', function() { + let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + const graph = myp5.createGraphics(200, 200, myp5.WEBGL); + assert(graph.width, 100); + assert(graph.height, 100); + glStub.restore(); + }); }); suite('p5.Graphics', function() { @@ -165,17 +175,5 @@ suite('Graphics', function() { assertValidPixels(graph, 19, 16, 2); }); - test('it resizes the canvas and the graphics based on max texture size', function() { - let glStub; - glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); - const fakeMaxTextureSize = 100; - glStub.returns(fakeMaxTextureSize); - - var graph = myp5.createGraphics(200, 200, myp5.WEBGL); - graph.pixelDensity(1); - assertValidGraphSizes(graph, 100, 100, 1); - - glStub.restore(); - }); }); }); diff --git a/test/unit/core/rendering.js b/test/unit/core/rendering.js index fafd011396..70b68c1331 100644 --- a/test/unit/core/rendering.js +++ b/test/unit/core/rendering.js @@ -41,6 +41,16 @@ suite('Rendering', function() { black ); }); + + test('should resize the dimensions of canvas based on max texture size', function() { + let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + myp5.createCanvas(200, 200, myp5.WEBGL); + assert.equal(myp5.width, 100); + assert.equal(myp5.height, 100); + glStub.restore(); + }); }); suite('p5.prototype.resizeCanvas', function() { @@ -91,28 +101,13 @@ suite('Rendering', function() { }); test('should resize the dimensions of canvas based on max texture size', function() { - let glStub; - glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); - const fakeMaxTextureSize = 100; - glStub.returns(fakeMaxTextureSize); - myp5.createCanvas(200, 200, myp5.WEBGL); - assert.equal(myp5.canvas.width, 100); - assert.equal(myp5.canvas.height, 100); - - glStub.restore(); - }); - - test('should resize the dimensions of canvas by resizeCanvas based on max texture size', function() { - - let glStub; - glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam').restore(); const fakeMaxTextureSize = 100; glStub.returns(fakeMaxTextureSize); myp5.createCanvas(10, 10, myp5.WEBGL); myp5.resizeCanvas(200, 200); - assert.equal(myp5.canvas.width, 100); - assert.equal(myp5.canvas.height, 100); - + assert.equal(myp5.width, 100); + assert.equal(myp5.height, 100); glStub.restore(); }); }); diff --git a/test/unit/webgl/p5.Framebuffer.js b/test/unit/webgl/p5.Framebuffer.js index 9b1a6afbb4..c654eba933 100644 --- a/test/unit/webgl/p5.Framebuffer.js +++ b/test/unit/webgl/p5.Framebuffer.js @@ -163,33 +163,26 @@ suite('p5.Framebuffer', function() { expect(fbo.color.rawTexture()).to.equal(oldTexture); }); - test('resizes the framebuffer using its constructor based on max texture size', function() { - let glStub; - glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + test('resizes the framebuffer by createFramebuffer based on max texture size', function() { + let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); const fakeMaxTextureSize = 100; glStub.returns(fakeMaxTextureSize); - myp5.createCanvas(10, 10, myp5.WEBGL); const fbo = myp5.createFramebuffer({ width: 200, height: 200 }); expect(fbo.width).to.equal(100); expect(fbo.height).to.equal(100); - glStub.restore(); }); - test('resizes the framebuffer using resize method based on max texture size', function() { - let glStub; - glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + test('resizes the framebuffer by resize method based on max texture size', function() { + let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); const fakeMaxTextureSize = 100; glStub.returns(fakeMaxTextureSize); - myp5.createCanvas(10, 10, myp5.WEBGL); const fbo = myp5.createFramebuffer({ width: 10, height: 10 }); myp5.resize(200, 200); expect(fbo.width).to.equal(100); expect(fbo.height).to.equal(100); - - glStub.restore(); }); }); From 6d8919f0e0160a7d6978617ac29a5ea4b1ff96e6 Mon Sep 17 00:00:00 2001 From: capGoblin Date: Sat, 20 Jan 2024 03:23:06 +0530 Subject: [PATCH 7/7] fix unit tests --- test/unit/core/p5.Graphics.js | 26 +++++++++++++++++--------- test/unit/core/rendering.js | 31 +++++++++++++++++++------------ test/unit/webgl/p5.Framebuffer.js | 15 +++++++++++---- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/test/unit/core/p5.Graphics.js b/test/unit/core/p5.Graphics.js index 76492ad6e3..634f24e4c7 100644 --- a/test/unit/core/p5.Graphics.js +++ b/test/unit/core/p5.Graphics.js @@ -48,15 +48,6 @@ suite('Graphics', function() { assert.isObject(graph); }); - test('it resizes the graphics based on max texture size', function() { - let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); - const fakeMaxTextureSize = 100; - glStub.returns(fakeMaxTextureSize); - const graph = myp5.createGraphics(200, 200, myp5.WEBGL); - assert(graph.width, 100); - assert(graph.height, 100); - glStub.restore(); - }); }); suite('p5.Graphics', function() { @@ -125,6 +116,15 @@ suite('Graphics', function() { }); suite('p5.Graphics.resizeCanvas', function() { + let glStub; + + afterEach(() => { + if (glStub) { + glStub.restore(); + glStub = null; + } + }); + test('it can call resizeCanvas', function() { var graph = myp5.createGraphics(10, 17); var resize = function() { @@ -175,5 +175,13 @@ suite('Graphics', function() { assertValidPixels(graph, 19, 16, 2); }); + test('it resizes the graphics based on max texture size', function() { + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + const graph = myp5.createGraphics(200, 200, myp5.WEBGL); + assert(graph.width, 100); + assert(graph.height, 100); + }); }); }); diff --git a/test/unit/core/rendering.js b/test/unit/core/rendering.js index 70b68c1331..8495b3aea3 100644 --- a/test/unit/core/rendering.js +++ b/test/unit/core/rendering.js @@ -41,19 +41,18 @@ suite('Rendering', function() { black ); }); - - test('should resize the dimensions of canvas based on max texture size', function() { - let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); - const fakeMaxTextureSize = 100; - glStub.returns(fakeMaxTextureSize); - myp5.createCanvas(200, 200, myp5.WEBGL); - assert.equal(myp5.width, 100); - assert.equal(myp5.height, 100); - glStub.restore(); - }); }); suite('p5.prototype.resizeCanvas', function() { + let glStub; + + afterEach(() => { + if (glStub) { + glStub.restore(); + glStub = null; + } + }); + test('should resize canvas', function() { myp5.resizeCanvas(10, 20); assert.equal(myp5.canvas.width, 10 * myp5.pixelDensity()); @@ -101,14 +100,22 @@ suite('Rendering', function() { }); test('should resize the dimensions of canvas based on max texture size', function() { - let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam').restore(); + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam').restore(); const fakeMaxTextureSize = 100; glStub.returns(fakeMaxTextureSize); myp5.createCanvas(10, 10, myp5.WEBGL); myp5.resizeCanvas(200, 200); assert.equal(myp5.width, 100); assert.equal(myp5.height, 100); - glStub.restore(); + }); + + test('should resize the dimensions of canvas based on max texture size', function() { + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + const fakeMaxTextureSize = 100; + glStub.returns(fakeMaxTextureSize); + myp5.createCanvas(200, 200, myp5.WEBGL); + assert.equal(myp5.width, 100); + assert.equal(myp5.height, 100); }); }); diff --git a/test/unit/webgl/p5.Framebuffer.js b/test/unit/webgl/p5.Framebuffer.js index c654eba933..71d52f19c9 100644 --- a/test/unit/webgl/p5.Framebuffer.js +++ b/test/unit/webgl/p5.Framebuffer.js @@ -89,6 +89,15 @@ suite('p5.Framebuffer', function() { }); suite('sizing', function() { + let glStub; + + afterEach(() => { + if (glStub) { + glStub.restore(); + glStub = null; + } + }); + test('auto-sized framebuffers change size with their canvas', function() { myp5.createCanvas(10, 10, myp5.WEBGL); myp5.pixelDensity(1); @@ -164,18 +173,17 @@ suite('p5.Framebuffer', function() { }); test('resizes the framebuffer by createFramebuffer based on max texture size', function() { - let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); const fakeMaxTextureSize = 100; glStub.returns(fakeMaxTextureSize); myp5.createCanvas(10, 10, myp5.WEBGL); const fbo = myp5.createFramebuffer({ width: 200, height: 200 }); expect(fbo.width).to.equal(100); expect(fbo.height).to.equal(100); - glStub.restore(); }); test('resizes the framebuffer by resize method based on max texture size', function() { - let glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); + glStub = sinon.stub(p5.RendererGL.prototype, '_getParam'); const fakeMaxTextureSize = 100; glStub.returns(fakeMaxTextureSize); myp5.createCanvas(10, 10, myp5.WEBGL); @@ -183,7 +191,6 @@ suite('p5.Framebuffer', function() { myp5.resize(200, 200); expect(fbo.width).to.equal(100); expect(fbo.height).to.equal(100); - glStub.restore(); }); }); });