import { GameSize , DeviceSize } from "@replay/core" ; import { Dimensions } from "./dimensions" ; export function calculateDeviceSize ( innerWidth , innerHeight , dimensions , size ) { let orientationSize ; if ( "portrait" in size ) { const isPortrait = innerHeight > innerWidth ; orientationSize = isPortrait ? size . portrait : size . landscape ; } else { orientationSize = size ; } const { width , height , maxWidthMargin = 0 , maxHeightMargin = 0 , } = orientationSize ; if ( dimensions === "game-coords" || innerWidth === 0 || innerHeight === 0 ) { return { width , height , widthMargin : 0 , heightMargin : 0 , deviceWidth : width , deviceHeight : height , } ; } const windowWidthHeightRatio = innerWidth / innerHeight ; const gameWidthHeightRatio = width / height ; if ( gameWidthHeightRatio > windowWidthHeightRatio ) { const scaledWidth = innerWidth ; const noMarginScaledHeight = scaledWidth / gameWidthHeightRatio ; const scaledMaxHeightMargin = ( noMarginScaledHeight / height ) * maxHeightMargin ; const scaledHeight = Math . min ( innerHeight , noMarginScaledHeight + scaledMaxHeightMargin * 2 ) ; return { width , height , widthMargin : 0 , heightMargin : ( ( scaledHeight - noMarginScaledHeight ) / 2 ) * ( height / noMarginScaledHeight ) , deviceWidth : scaledWidth , deviceHeight : scaledHeight , } ; } else { const scaledHeight = innerHeight ; const noMarginScaledWidth = scaledHeight * gameWidthHeightRatio ; const scaledMaxWidthMargin = ( noMarginScaledWidth / width ) * maxWidthMargin ; const scaledWidth = Math . min ( innerWidth , noMarginScaledWidth + scaledMaxWidthMargin * 2 ) ; return { width , height , widthMargin : ( ( scaledWidth - noMarginScaledWidth ) / 2 ) * ( width / noMarginScaledWidth ) , heightMargin : 0 , deviceWidth : scaledWidth , deviceHeight : scaledHeight , } ; } }