PiP js Update video size based on Picture-in-Picture window size changes code example
Example: PiP js Update video size based on Picture-in-Picture window size changes
<video id="video" src="https://example.com/file.mp4"></video>
<button id="pipButton"></button>
<script>
const video = document.getElementById("video");
const pipButton = document.getElementById("pipButton");
pipButton.addEventListener("click", async () => {
try {
await video.requestPictureInPicture();
} catch (error) {
}
});
video.addEventListener("enterpictureinpicture", (event) => {
const pipWindow = event.pictureInPictureWindow;
updateVideoSize(pipWindow.width, pipWindow.height);
pipWindow.addEventListener("resize", onPipWindowResize);
});
video.addEventListener("leavepictureinpicture", (event) => {
const pipWindow = event.pictureInPictureWindow;
pipWindow.removeEventListener("resize", onPipWindowResize);
});
function onPipWindowResize(event) {
const { width, height } = event.target;
updateVideoSize(width, height);
}
function updateVideoSize(width, height) {
}
</script>