@@ -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', {
817853function 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 */
837875function 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
867885function requestFullscreen ( canvas ) {
0 commit comments