Created
July 10, 2019 12:10
-
-
Save tom2strobl/345c198b9e75ecfbf4f3ed3de9cf833e 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 ReactPlayer from 'react-player' | |
import { useSize } from 'react-use' | |
const videoConfig = { | |
vimeo: { | |
playerOptions: { | |
background: true | |
} | |
} | |
} | |
export default function BackgroundVideo({ video }) { | |
const [sized, { width, height }] = useSize(({ width, height }) => { | |
let vw | |
let vh | |
let trans | |
if (width / height > video.width / video.height) { | |
vh = `calc(${width}px / ${video.width} * ${video.height})` | |
vw = `${width}px` | |
trans = `translate(0, calc((${height}px - (${width}px / ${video.width} * ${video.height})) / 2))` | |
} else { | |
vh = `${height}px` | |
vw = `calc(${height}px / ${video.height} * ${video.width})` | |
trans = `translate( calc((${width}px - (${height}px / ${video.height} * ${video.width})) / 2), 0)` | |
} | |
return ( | |
<div style={{ position: 'relative', height: '100%', width: '100%', overflow: 'hidden' }}> | |
<div | |
style={{ | |
position: 'absolute', | |
left: '0', | |
top: '0', | |
height: vh, | |
width: vw, | |
transform: trans | |
}} | |
> | |
<ReactPlayer | |
url={video.embed_url} | |
config={videoConfig} | |
loop={true} | |
muted={false} | |
width="100%" | |
height="100%" | |
/> | |
</div> | |
</div> | |
) | |
}) | |
return sized | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment