Skip to content

Commit 6b881eb

Browse files
yaustarsteveny-scSteven Yau
authored
Remove gl resize (playcanvas#3151)
* Revert "Revert "Remove gl canvas dependency resize (playcanvas#3124)" (playcanvas#3146)" This reverts commit ebcdfc3. * Refactor gl.canvas to use clientWidth/Height instead of relying on external calls to resizeCanvas Fixes playcanvas#3114 * Renamed updateGlCanvas to updateCanvas * Made updateCanvasSize public to be called by the document page JS Planning a little ahead here. This function will need to be called when the resize and orientation events are fired for some older projects to works as they depend on the canvas resolution being correct after a resize event. * Got rid of return statement Co-authored-by: Steven Yau <syau@snapchat.com> Co-authored-by: Steven Yau <social@stevenyau.co.uk>
1 parent 0763d1b commit 6b881eb

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/framework/application.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import { ApplicationStats } from './stats.js';
106106
import { Entity } from './entity.js';
107107
import { SceneRegistry } from './scene-registry.js';
108108
import { SceneDepth } from './scene-depth.js';
109+
import { XRTYPE_VR } from "../xr/constants";
109110

110111
import {
111112
FILLMODE_FILL_WINDOW, FILLMODE_KEEP_ASPECT,
@@ -1394,10 +1395,7 @@ class Application extends EventHandler {
13941395
this.graphicsDevice.canvas.style.width = width + 'px';
13951396
this.graphicsDevice.canvas.style.height = height + 'px';
13961397

1397-
// In AUTO mode the resolution is changed to match the canvas size
1398-
if (this._resolutionMode === RESOLUTION_AUTO) {
1399-
this.setCanvasResolution(RESOLUTION_AUTO);
1400-
}
1398+
this.updateCanvasSize();
14011399

14021400
// return the final values calculated for width and height
14031401
return {
@@ -1406,6 +1404,27 @@ class Application extends EventHandler {
14061404
};
14071405
}
14081406

1407+
/**
1408+
* @function
1409+
* @name Application#updateCanvasSize
1410+
* @description Updates the {@link GraphicsDevice} canvas size to match the canvas size on the document page.
1411+
* It is recommended to call this function when the canvas size changes (e.g on window resize and orientation change
1412+
* events) so that the canvas resolution is immediately updated.
1413+
*/
1414+
updateCanvasSize() {
1415+
// Don't update if we are in VR
1416+
if ((this.vr && this.vr.display) || (this.xr.active && this.xr.type === XRTYPE_VR)) {
1417+
return;
1418+
}
1419+
1420+
// In AUTO mode the resolution is changed to match the canvas size
1421+
if (this._resolutionMode === RESOLUTION_AUTO) {
1422+
// Check if the canvas DOM has changed size
1423+
const canvas = this.graphicsDevice.canvas;
1424+
this.graphicsDevice.resizeCanvas(canvas.clientWidth, canvas.clientHeight);
1425+
}
1426+
}
1427+
14091428
/**
14101429
* @private
14111430
* @name Application#onLibrariesLoaded
@@ -2152,6 +2171,7 @@ var makeTick = function (_app) {
21522171
application.fire("framerender");
21532172

21542173
if (application.autoRender || application.renderNextFrame) {
2174+
application.updateCanvasSize();
21552175
application.render();
21562176
application.renderNextFrame = false;
21572177
}

src/graphics/graphics-device.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,6 +3475,7 @@ class GraphicsDevice extends EventHandler {
34753475
}
34763476

34773477
/**
3478+
* @private
34783479
* @function
34793480
* @name GraphicsDevice#resizeCanvas
34803481
* @description Sets the width and height of the canvas, then fires the 'resizecanvas' event.
@@ -3488,16 +3489,15 @@ class GraphicsDevice extends EventHandler {
34883489
this._width = width;
34893490
this._height = height;
34903491

3491-
const ratio = Math.min(this._maxPixelRatio, window.devicePixelRatio);
3492-
width *= ratio;
3493-
height *= ratio;
3494-
3495-
if (this.canvas.width === width && this.canvas.height === height)
3496-
return;
3492+
var ratio = Math.min(this._maxPixelRatio, window.devicePixelRatio);
3493+
width = Math.floor(width * ratio);
3494+
height = Math.floor(height * ratio);
34973495

3498-
this.canvas.width = width;
3499-
this.canvas.height = height;
3500-
this.fire(EVENT_RESIZE, width, height);
3496+
if (this.canvas.width !== width || this.canvas.height !== height) {
3497+
this.canvas.width = width;
3498+
this.canvas.height = height;
3499+
this.fire(EVENT_RESIZE, width, height);
3500+
}
35013501
}
35023502

35033503
setResolution(width, height) {

0 commit comments

Comments
 (0)