|
| 1 | +// Select elements here |
| 2 | +const video = document.getElementById('video'); |
| 3 | +const videoControls = document.getElementById('video-controls'); |
| 4 | +const playButton = document.getElementById('play'); |
| 5 | +const playbackIcons = document.querySelectorAll('.playback-icons use'); |
| 6 | +const timeElapsed = document.getElementById('time-elapsed'); |
| 7 | +const duration = document.getElementById('duration'); |
| 8 | +const progressBar = document.getElementById('progress-bar'); |
| 9 | +const seek = document.getElementById('seek'); |
| 10 | +const seekTooltip = document.getElementById('seek-tooltip'); |
| 11 | +const volumeButton = document.getElementById('volume-button'); |
| 12 | +const volumeIcons = document.querySelectorAll('.volume-button use'); |
| 13 | +const volumeMute = document.querySelector('use[href="#volume-mute"]'); |
| 14 | +const volumeLow = document.querySelector('use[href="#volume-low"]'); |
| 15 | +const volumeHigh = document.querySelector('use[href="#volume-high"]'); |
| 16 | +const volume = document.getElementById('volume'); |
| 17 | +const playbackAnimation = document.getElementById('playback-animation'); |
| 18 | +const fullscreenButton = document.getElementById('fullscreen-button'); |
| 19 | +const videoContainer = document.getElementById('video-container'); |
| 20 | +const fullscreenIcons = fullscreenButton.querySelectorAll('use'); |
| 21 | + |
| 22 | +const videoWorks = !!document.createElement('video').canPlayType; |
| 23 | +if (videoWorks) { |
| 24 | + video.controls = false; |
| 25 | + videoControls.classList.remove('hidden'); |
| 26 | +} |
| 27 | + |
| 28 | +// Add functions here |
| 29 | + |
| 30 | +// togglePlay toggles the playback state of the video. |
| 31 | +// If the video playback is paused or ended, the video is played |
| 32 | +// otherwise, the video is paused |
| 33 | +function togglePlay() { |
| 34 | + if (video.paused || video.ended) { |
| 35 | + video.play(); |
| 36 | + } else { |
| 37 | + video.pause(); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// updatePlayButton updates the playback icon and tooltip |
| 42 | +// depending on the playback state |
| 43 | +function updatePlayButton() { |
| 44 | + playbackIcons.forEach((icon) => icon.classList.toggle('hidden')); |
| 45 | + |
| 46 | + if (video.paused) { |
| 47 | + playButton.setAttribute('data-title', 'Play (k)'); |
| 48 | + } else { |
| 49 | + playButton.setAttribute('data-title', 'Pause (k)'); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// formatTime takes a time length in seconds and returns the time in |
| 54 | +// minutes and seconds |
| 55 | +function formatTime(timeInSeconds) { |
| 56 | + const result = new Date(timeInSeconds * 1000).toISOString().substr(11, 8); |
| 57 | + |
| 58 | + return { |
| 59 | + minutes: result.substr(3, 2), |
| 60 | + seconds: result.substr(6, 2), |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +// initializeVideo sets the video duration, and maximum value of the |
| 65 | +// progressBar |
| 66 | +function initializeVideo() { |
| 67 | + const videoDuration = Math.round(video.duration); |
| 68 | + seek.setAttribute('max', videoDuration); |
| 69 | + progressBar.setAttribute('max', videoDuration); |
| 70 | + const time = formatTime(videoDuration); |
| 71 | + duration.innerText = `${time.minutes}:${time.seconds}`; |
| 72 | + duration.setAttribute('datetime', `${time.minutes}m ${time.seconds}s`); |
| 73 | +} |
| 74 | + |
| 75 | +// updateTimeElapsed indicates how far through the video |
| 76 | +// the current playback is by updating the timeElapsed element |
| 77 | +function updateTimeElapsed() { |
| 78 | + const time = formatTime(Math.round(video.currentTime)); |
| 79 | + timeElapsed.innerText = `${time.minutes}:${time.seconds}`; |
| 80 | + timeElapsed.setAttribute('datetime', `${time.minutes}m ${time.seconds}s`); |
| 81 | +} |
| 82 | + |
| 83 | +// updateProgress indicates how far through the video |
| 84 | +// the current playback is by updating the progress bar |
| 85 | +function updateProgress() { |
| 86 | + seek.value = Math.floor(video.currentTime); |
| 87 | + progressBar.value = Math.floor(video.currentTime); |
| 88 | +} |
| 89 | + |
| 90 | +// updateSeekTooltip uses the position of the mouse on the progress bar to |
| 91 | +// roughly work out what point in the video the user will skip to if |
| 92 | +// the progress bar is clicked at that point |
| 93 | +function updateSeekTooltip(event) { |
| 94 | + const skipTo = Math.round( |
| 95 | + (event.offsetX / event.target.clientWidth) * |
| 96 | + parseInt(event.target.getAttribute('max'), 10) |
| 97 | + ); |
| 98 | + seek.setAttribute('data-seek', skipTo); |
| 99 | + const t = formatTime(skipTo); |
| 100 | + seekTooltip.textContent = `${t.minutes}:${t.seconds}`; |
| 101 | + const rect = video.getBoundingClientRect(); |
| 102 | + seekTooltip.style.left = `${event.pageX - rect.left}px`; |
| 103 | +} |
| 104 | + |
| 105 | +// skipAhead jumps to a different point in the video when the progress bar |
| 106 | +// is clicked |
| 107 | +function skipAhead(event) { |
| 108 | + const skipTo = event.target.dataset.seek |
| 109 | + ? event.target.dataset.seek |
| 110 | + : event.target.value; |
| 111 | + video.currentTime = skipTo; |
| 112 | + progressBar.value = skipTo; |
| 113 | + seek.value = skipTo; |
| 114 | +} |
| 115 | + |
| 116 | +// updateVolume updates the video's volume |
| 117 | +// and disables the muted state if active |
| 118 | +function updateVolume() { |
| 119 | + if (video.muted) { |
| 120 | + video.muted = false; |
| 121 | + } |
| 122 | + |
| 123 | + video.volume = volume.value; |
| 124 | +} |
| 125 | + |
| 126 | +// updateVolumeIcon updates the volume icon so that it correctly reflects |
| 127 | +// the volume of the video |
| 128 | +function updateVolumeIcon() { |
| 129 | + volumeIcons.forEach((icon) => { |
| 130 | + icon.classList.add('hidden'); |
| 131 | + }); |
| 132 | + |
| 133 | + volumeButton.setAttribute('data-title', 'Mute (m)'); |
| 134 | + |
| 135 | + if (video.muted || video.volume === 0) { |
| 136 | + volumeMute.classList.remove('hidden'); |
| 137 | + volumeButton.setAttribute('data-title', 'Unmute (m)'); |
| 138 | + } else if (video.volume > 0 && video.volume <= 0.5) { |
| 139 | + volumeLow.classList.remove('hidden'); |
| 140 | + } else { |
| 141 | + volumeHigh.classList.remove('hidden'); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// toggleMute mutes or unmutes the video when executed |
| 146 | +// When the video is unmuted, the volume is returned to the value |
| 147 | +// it was set to before the video was muted |
| 148 | +function toggleMute() { |
| 149 | + video.muted = !video.muted; |
| 150 | + |
| 151 | + if (video.muted) { |
| 152 | + volume.setAttribute('data-volume', volume.value); |
| 153 | + volume.value = 0; |
| 154 | + } else { |
| 155 | + volume.value = volume.dataset.volume; |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +// animatePlayback displays an animation when |
| 160 | +// the video is played or paused |
| 161 | +function animatePlayback() { |
| 162 | + playbackAnimation.animate( |
| 163 | + [ |
| 164 | + { |
| 165 | + opacity: 1, |
| 166 | + transform: 'scale(1)', |
| 167 | + }, |
| 168 | + { |
| 169 | + opacity: 0, |
| 170 | + transform: 'scale(1.3)', |
| 171 | + }, |
| 172 | + ], |
| 173 | + { |
| 174 | + duration: 500, |
| 175 | + } |
| 176 | + ); |
| 177 | +} |
| 178 | + |
| 179 | +// toggleFullScreen toggles the full screen state of the video |
| 180 | +// If the browser is currently in fullscreen mode, |
| 181 | +// then it should exit and vice versa. |
| 182 | +function toggleFullScreen() { |
| 183 | + if (document.fullscreenElement) { |
| 184 | + document.exitFullscreen(); |
| 185 | + } else if (document.webkitFullscreenElement) { |
| 186 | + // Need this to support Safari |
| 187 | + document.webkitExitFullscreen(); |
| 188 | + } else if (videoContainer.webkitRequestFullscreen) { |
| 189 | + // Need this to support Safari |
| 190 | + videoContainer.webkitRequestFullscreen(); |
| 191 | + } else { |
| 192 | + videoContainer.requestFullscreen(); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +// updateFullscreenButton changes the icon of the full screen button |
| 197 | +// and tooltip to reflect the current full screen state of the video |
| 198 | +function updateFullscreenButton() { |
| 199 | + fullscreenIcons.forEach((icon) => icon.classList.toggle('hidden')); |
| 200 | + |
| 201 | + if (document.fullscreenElement) { |
| 202 | + fullscreenButton.setAttribute('data-title', 'Exit full screen (f)'); |
| 203 | + } else { |
| 204 | + fullscreenButton.setAttribute('data-title', 'Full screen (f)'); |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +// togglePip toggles Picture-in-Picture mode on the video |
| 209 | +async function togglePip() { |
| 210 | + try { |
| 211 | + if (video !== document.pictureInPictureElement) { |
| 212 | + pipButton.disabled = true; |
| 213 | + await video.requestPictureInPicture(); |
| 214 | + } else { |
| 215 | + await document.exitPictureInPicture(); |
| 216 | + } |
| 217 | + } catch (error) { |
| 218 | + console.error(error); |
| 219 | + } finally { |
| 220 | + pipButton.disabled = false; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +// hideControls hides the video controls when not in use |
| 225 | +// if the video is paused, the controls must remain visible |
| 226 | +function hideControls() { |
| 227 | + if (video.paused) { |
| 228 | + return; |
| 229 | + } |
| 230 | + |
| 231 | + videoControls.classList.add('hide'); |
| 232 | +} |
| 233 | + |
| 234 | +// showControls displays the video controls |
| 235 | +function showControls() { |
| 236 | + videoControls.classList.remove('hide'); |
| 237 | +} |
| 238 | + |
| 239 | +// keyboardShortcuts executes the relevant functions for |
| 240 | +// each supported shortcut key |
| 241 | +function keyboardShortcuts(event) { |
| 242 | + const { key } = event; |
| 243 | + switch (key) { |
| 244 | + case 'k': |
| 245 | + togglePlay(); |
| 246 | + animatePlayback(); |
| 247 | + if (video.paused) { |
| 248 | + showControls(); |
| 249 | + } else { |
| 250 | + setTimeout(() => { |
| 251 | + hideControls(); |
| 252 | + }, 2000); |
| 253 | + } |
| 254 | + break; |
| 255 | + case 'm': |
| 256 | + toggleMute(); |
| 257 | + break; |
| 258 | + case 'f': |
| 259 | + toggleFullScreen(); |
| 260 | + break; |
| 261 | + } |
| 262 | +} |
| 263 | + |
| 264 | +// Add eventlisteners here |
| 265 | +playButton.addEventListener('click', togglePlay); |
| 266 | +video.addEventListener('play', updatePlayButton); |
| 267 | +video.addEventListener('pause', updatePlayButton); |
| 268 | +video.addEventListener('loadedmetadata', initializeVideo); |
| 269 | +video.addEventListener('timeupdate', updateTimeElapsed); |
| 270 | +video.addEventListener('timeupdate', updateProgress); |
| 271 | +video.addEventListener('volumechange', updateVolumeIcon); |
| 272 | +video.addEventListener('click', togglePlay); |
| 273 | +video.addEventListener('click', animatePlayback); |
| 274 | +video.addEventListener('mouseenter', showControls); |
| 275 | +video.addEventListener('mouseleave', hideControls); |
| 276 | +videoControls.addEventListener('mouseenter', showControls); |
| 277 | +videoControls.addEventListener('mouseleave', hideControls); |
| 278 | +seek.addEventListener('mousemove', updateSeekTooltip); |
| 279 | +seek.addEventListener('input', skipAhead); |
| 280 | +volume.addEventListener('input', updateVolume); |
| 281 | +volumeButton.addEventListener('click', toggleMute); |
| 282 | +fullscreenButton.addEventListener('click', toggleFullScreen); |
| 283 | +videoContainer.addEventListener('fullscreenchange', updateFullscreenButton); |
| 284 | + |
| 285 | +document.addEventListener('DOMContentLoaded', () => { |
| 286 | + if (!('pictureInPictureEnabled' in document)) { |
| 287 | + pipButton.classList.add('hidden'); |
| 288 | + } |
| 289 | +}); |
| 290 | +document.addEventListener('keyup', keyboardShortcuts); |
0 commit comments