Skip to content

Commit 5bc1836

Browse files
s-lightdmarcos
authored andcommitted
Fix canvas size calculation on embedded mode (fix #4911)
1 parent 1398123 commit 5bc1836

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

src/core/scene/a-scene.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -804,10 +804,46 @@ module.exports.AScene = registerElement('a-scene', {
804804
})
805805
});
806806

807+
/**
808+
* Return size constrained to maxSize - maintaining aspect ratio.
809+
*
810+
* @param {object} size - size parameters (width and height).
811+
* @param {object} maxSize - Max size parameters (width and height).
812+
* @returns {object} Width and height.
813+
*/
814+
function constrainSizeTo (size, maxSize) {
815+
var aspectRatio;
816+
var pixelRatio = window.devicePixelRatio;
817+
818+
if (!maxSize || (maxSize.width === -1 && maxSize.height === -1)) {
819+
return size;
820+
}
821+
822+
if (size.width * pixelRatio < maxSize.width &&
823+
size.height * pixelRatio < maxSize.height) {
824+
return size;
825+
}
826+
827+
aspectRatio = size.width / size.height;
828+
829+
if ((size.width * pixelRatio) > maxSize.width && maxSize.width !== -1) {
830+
size.width = Math.round(maxSize.width / pixelRatio);
831+
size.height = Math.round(maxSize.width / aspectRatio / pixelRatio);
832+
}
833+
834+
if ((size.height * pixelRatio) > maxSize.height && maxSize.height !== -1) {
835+
size.height = Math.round(maxSize.height / pixelRatio);
836+
size.width = Math.round(maxSize.height * aspectRatio / pixelRatio);
837+
}
838+
839+
return size;
840+
}
841+
807842
/**
808843
* Return the canvas size where the scene will be rendered.
809844
* Will be always the window size except when the scene is embedded.
810-
* The parent size (less than max size) will be returned in that case.
845+
* The parent size will be returned in that case.
846+
* the returned size will be constrained to the maxSize maintaining aspect ratio.
811847
*
812848
* @param {object} canvasEl - the canvas element
813849
* @param {boolean} embedded - Is the scene embedded?
@@ -817,10 +853,12 @@ module.exports.AScene = registerElement('a-scene', {
817853
function getCanvasSize (canvasEl, embedded, maxSize, isVR) {
818854
if (!canvasEl.parentElement) { return {height: 0, width: 0}; }
819855
if (embedded) {
820-
return {
856+
var size;
857+
size = {
821858
height: canvasEl.parentElement.offsetHeight,
822859
width: canvasEl.parentElement.offsetWidth
823860
};
861+
return constrainSizeTo(size, maxSize);
824862
}
825863
return getMaxSize(maxSize, isVR);
826864
}
@@ -835,33 +873,13 @@ function getCanvasSize (canvasEl, embedded, maxSize, isVR) {
835873
* @returns {object} Width and height.
836874
*/
837875
function getMaxSize (maxSize, isVR) {
838-
var aspectRatio;
839876
var size;
840-
var pixelRatio = window.devicePixelRatio;
841-
842877
size = {height: document.body.offsetHeight, width: document.body.offsetWidth};
843-
if (!maxSize || isVR || (maxSize.width === -1 && maxSize.height === -1)) {
878+
if (isVR) {
844879
return size;
880+
} else {
881+
return constrainSizeTo(size, maxSize);
845882
}
846-
847-
if (size.width * pixelRatio < maxSize.width &&
848-
size.height * pixelRatio < maxSize.height) {
849-
return size;
850-
}
851-
852-
aspectRatio = size.width / size.height;
853-
854-
if ((size.width * pixelRatio) > maxSize.width && maxSize.width !== -1) {
855-
size.width = Math.round(maxSize.width / pixelRatio);
856-
size.height = Math.round(maxSize.width / aspectRatio / pixelRatio);
857-
}
858-
859-
if ((size.height * pixelRatio) > maxSize.height && maxSize.height !== -1) {
860-
size.height = Math.round(maxSize.height / pixelRatio);
861-
size.width = Math.round(maxSize.height * aspectRatio / pixelRatio);
862-
}
863-
864-
return size;
865883
}
866884

867885
function requestFullscreen (canvas) {

0 commit comments

Comments
 (0)