Skip to content

Instantly share code, notes, and snippets.

@KrustyC
Last active February 6, 2020 13:32
Show Gist options
  • Save KrustyC/f1a1313044a0588779bce0a62392605a to your computer and use it in GitHub Desktop.
Save KrustyC/f1a1313044a0588779bce0a62392605a to your computer and use it in GitHub Desktop.
const getNewDimensions = (playerInstance: jwplayer.JWPlayer | null): Dimension => {
const container = playerInstance.getContainer();
const parentContainerWidth = container && container.parentElement.offsetWidth;
let parentContainerHeight = container && container.parentElement.offsetHeight;
// get the window size, overcoming simulator bug of misplacing the actual screen size property value
const windowWidth = Math.min(window.innerWidth, window.outerWidth);
console.log(windowWidth)
// only tweak height for mobile phone widths
if (windowWidth <= PHONE_SIZE) {
const playlistItem = playerInstance.getPlaylistItem();
// calculate the actual video media aspect ratio
const currentVideoWidth = playlistItem.allSources[1].width;
const currentVideoHeight = playlistItem.allSources[1].height;
const currentVideoRatio = currentVideoWidth / currentVideoHeight;
// calculate the new desired player height
parentContainerHeight = parentContainerWidth / currentVideoRatio;
}
return {
height: parentContainerHeight,
width: parentContainerWidth,
};
};
import { PHONE_SIZE } from '../constants';
type Dimension = {
width: number;
height: number;
};
const calculateRatio = (width: number, height: number): number => width / height;
const calculateHeight = (containerWidth: number, videoWidth: number, videoHeight: number) =>
containerWidth / calculateRatio(videoWidth, videoHeight);
const getNewDimensions = (playerInstance: jwplayer.JWPlayer | null): Dimension => {
const container = playerInstance.getContainer();
const width = container && container.parentElement.offsetWidth;
const height = container && container.parentElement.offsetHeight;
// get the window size, overcoming simulator bug of misplacing the actual screen size property value
const windowWidth = Math.min(window.innerWidth, window.outerWidth);
if (windowWidth > PHONE_SIZE) {
return { height, width };
}
const [, video] = playerInstance.getPlaylistItem().allSources;
return {
height: calculateHeight(width, video.width, video.height),
width,
};
};
export default getNewDimensions;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment