Created
February 6, 2020 14:16
-
-
Save KrustyC/d480218ad7ac26aea6b93da8403874e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PHONE_SIZE } from '../constants'; | |
type Dimensions = { | |
width: number; | |
height: number; | |
}; | |
type DimensionData = { | |
mediaDimensions: Dimensions; | |
containerDimensions: Dimensions; | |
windowDimensions: Dimensions; | |
}; | |
const calculateDimensions = ({ | |
mediaDimensions, | |
containerDimensions, | |
windowDimensions, | |
}: DimensionData): Dimensions => { | |
if (windowDimensions.width > PHONE_SIZE) { | |
return containerDimensions; | |
} | |
const mediaAspectRatio = mediaDimensions.width / mediaDimensions.height; | |
return { | |
height: containerDimensions.width / mediaAspectRatio, | |
width: containerDimensions.width, | |
}; | |
}; | |
const getNewDimensions = (playerInstance: jwplayer.JWPlayer | null): Dimensions => { | |
const [, video] = playerInstance.getPlaylistItem().allSources; | |
const container = playerInstance.getContainer().parentElement; | |
const data = { | |
mediaDimensions: { | |
width: video.width, | |
height: video.height, | |
}, | |
windowDimensions: { | |
width: Math.min(window.innerWidth, window.outerWidth), | |
height: window.innerHeight, | |
}, | |
containerDimensions: { | |
width: container.offsetWidth, | |
height: container.offsetHeight, | |
}, | |
}; | |
return calculateDimensions(data); | |
}; | |
export default getNewDimensions; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment