|
2 | 2 | import React, { useEffect, useRef, FunctionComponent, useState } from 'react';
|
3 | 3 | import videojs from 'video.js';
|
4 | 4 | import Player from 'video.js/dist/types/player';
|
| 5 | +import Hammer from 'hammerjs'; |
5 | 6 | import 'video.js/dist/video-js.css';
|
6 | 7 | import 'videojs-contrib-eme';
|
7 | 8 | import 'videojs-mobile-ui/dist/videojs-mobile-ui.css';
|
@@ -30,6 +31,19 @@ interface VideoPlayerProps {
|
30 | 31 | onVideoEnd: () => void;
|
31 | 32 | }
|
32 | 33 |
|
| 34 | +interface TransformState { |
| 35 | + scale: number; |
| 36 | + lastScale: number; |
| 37 | + translateX: number; |
| 38 | + translateY: number; |
| 39 | + lastPanX: number; |
| 40 | + lastPanY: number; |
| 41 | +} |
| 42 | + |
| 43 | +interface ZoomIndicator extends HTMLDivElement { |
| 44 | + timeoutId?: ReturnType<typeof setTimeout>; |
| 45 | +} |
| 46 | + |
33 | 47 | const PLAYBACK_RATES: number[] = [0.5, 1, 1.25, 1.5, 1.75, 2];
|
34 | 48 | const VOLUME_LEVELS: number[] = [0, 0.2, 0.4, 0.6, 0.8, 1.0];
|
35 | 49 |
|
@@ -96,6 +110,207 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
|
96 | 110 | return pipButtonContainer;
|
97 | 111 | };
|
98 | 112 |
|
| 113 | + const setupZoomFeatures = (player: any) => { |
| 114 | + |
| 115 | + if (typeof window === 'undefined' || typeof document === 'undefined') return; |
| 116 | + |
| 117 | + const videoEl = player.el().querySelector('video'); |
| 118 | + const container = player.el(); |
| 119 | + |
| 120 | + const transformState: TransformState = { |
| 121 | + scale: 1, |
| 122 | + lastScale: 1, |
| 123 | + translateX: 0, |
| 124 | + translateY: 0, |
| 125 | + lastPanX: 0, |
| 126 | + lastPanY: 0 |
| 127 | + }; |
| 128 | + |
| 129 | + // Zoom indicator |
| 130 | + const zoomIndicator = document.createElement('div') as ZoomIndicator; |
| 131 | + zoomIndicator.className = 'vjs-zoom-level'; |
| 132 | + container.appendChild(zoomIndicator); |
| 133 | + |
| 134 | + // Optimized boundary calculation with memoization |
| 135 | + const calculateBoundaries = (() => { |
| 136 | + let lastDimensions: { width: number; height: number }; |
| 137 | + |
| 138 | + return () => { |
| 139 | + const containerRect = container.getBoundingClientRect(); |
| 140 | + const videoAspect = videoEl.videoWidth / videoEl.videoHeight; |
| 141 | + |
| 142 | + // returning cached values if dimensions haven't changed |
| 143 | + if (lastDimensions?.width === containerRect.width && |
| 144 | + lastDimensions?.height === containerRect.height) { |
| 145 | + return lastDimensions; |
| 146 | + } |
| 147 | + |
| 148 | + const containerAspect = containerRect.width / containerRect.height; |
| 149 | + let actualWidth = containerRect.width; |
| 150 | + let actualHeight = containerRect.height; |
| 151 | + |
| 152 | + actualWidth = containerAspect > videoAspect |
| 153 | + ? actualHeight * videoAspect |
| 154 | + : actualWidth; |
| 155 | + actualHeight = containerAspect > videoAspect |
| 156 | + ? actualHeight |
| 157 | + : actualWidth / videoAspect; |
| 158 | + |
| 159 | + lastDimensions = { |
| 160 | + width: actualWidth, |
| 161 | + height: actualHeight |
| 162 | + }; |
| 163 | + |
| 164 | + return lastDimensions; |
| 165 | + }; |
| 166 | + })(); |
| 167 | + |
| 168 | + // Unified gesture handler |
| 169 | + const handleGestureControl = (e: HammerInput) => { |
| 170 | + const target = e.srcEvent.target as HTMLElement; |
| 171 | + const isControlBar = target.closest('.vjs-control-bar'); |
| 172 | + |
| 173 | + if (!isControlBar && player.isFullscreen()) { |
| 174 | + e.srcEvent.preventDefault(); |
| 175 | + e.srcEvent.stopPropagation(); |
| 176 | + } |
| 177 | + }; |
| 178 | + |
| 179 | + // Configuring Hammer with proper types |
| 180 | + const hammer = new Hammer.Manager(container, { |
| 181 | + touchAction: 'none', |
| 182 | + inputClass: Hammer.TouchInput |
| 183 | + }); |
| 184 | + |
| 185 | + hammer.add(new Hammer.Pinch()); |
| 186 | + hammer.add(new Hammer.Pan({ |
| 187 | + threshold: 0, |
| 188 | + direction: Hammer.DIRECTION_ALL |
| 189 | + })); |
| 190 | + |
| 191 | + // Optimized transform update with boundary enforcement |
| 192 | + const updateTransform = () => { |
| 193 | + const boundaries = calculateBoundaries(); |
| 194 | + const maxX = (boundaries.width * (transformState.scale - 1)) / 2; |
| 195 | + const maxY = (boundaries.height * (transformState.scale - 1)) / 2; |
| 196 | + |
| 197 | + transformState.translateX = Math.min(Math.max( |
| 198 | + transformState.translateX, |
| 199 | + -maxX |
| 200 | + ), maxX); |
| 201 | + |
| 202 | + transformState.translateY = Math.min(Math.max( |
| 203 | + transformState.translateY, |
| 204 | + -maxY |
| 205 | + ), maxY); |
| 206 | + |
| 207 | + videoEl.style.transform = ` |
| 208 | + scale(${transformState.scale}) |
| 209 | + translate3d( |
| 210 | + ${transformState.translateX / transformState.scale}px, |
| 211 | + ${transformState.translateY / transformState.scale}px, |
| 212 | + 0 |
| 213 | + )`; |
| 214 | + }; |
| 215 | + |
| 216 | + // Unified pinch handler |
| 217 | + hammer.on('pinchstart pinchmove', (e) => { |
| 218 | + handleGestureControl(e); |
| 219 | + |
| 220 | + if (!player.isFullscreen()) return; |
| 221 | + |
| 222 | + if (e.type === 'pinchstart') { |
| 223 | + transformState.lastScale = transformState.scale; |
| 224 | + videoEl.classList.add('zoomed'); |
| 225 | + return; |
| 226 | + } |
| 227 | + |
| 228 | + transformState.scale = Math.min( |
| 229 | + Math.max(transformState.lastScale * e.scale, 1), |
| 230 | + 3 |
| 231 | + ); |
| 232 | + |
| 233 | + updateTransform(); |
| 234 | + showZoomLevel(); |
| 235 | + }); |
| 236 | + |
| 237 | + // Unified pan handler |
| 238 | + hammer.on('panstart panmove', (e) => { |
| 239 | + handleGestureControl(e); |
| 240 | + |
| 241 | + if (transformState.scale <= 1) return; |
| 242 | + |
| 243 | + if (e.type === 'panstart') { |
| 244 | + transformState.lastPanX = e.center.x; |
| 245 | + transformState.lastPanY = e.center.y; |
| 246 | + videoEl.style.transition = 'none'; |
| 247 | + return; |
| 248 | + } |
| 249 | + |
| 250 | + const deltaX = e.center.x - transformState.lastPanX; |
| 251 | + const deltaY = e.center.y - transformState.lastPanY; |
| 252 | + |
| 253 | + transformState.translateX += deltaX; |
| 254 | + transformState.translateY += deltaY; |
| 255 | + |
| 256 | + transformState.lastPanX = e.center.x; |
| 257 | + transformState.lastPanY = e.center.y; |
| 258 | + |
| 259 | + updateTransform(); |
| 260 | + }); |
| 261 | + |
| 262 | + // Optimized zoom indicator |
| 263 | + const showZoomLevel = () => { |
| 264 | + zoomIndicator.textContent = `${transformState.scale.toFixed(1)}x`; |
| 265 | + zoomIndicator.style.opacity = '1'; |
| 266 | + |
| 267 | + if (zoomIndicator.timeoutId) clearTimeout(zoomIndicator.timeoutId); |
| 268 | + |
| 269 | + zoomIndicator.timeoutId = setTimeout(() => { |
| 270 | + zoomIndicator.style.opacity = '0'; |
| 271 | + }, 1000); |
| 272 | + }; |
| 273 | + |
| 274 | + // Reset handler with animation frame |
| 275 | + const resetZoom = () => { |
| 276 | + transformState.scale = 1; |
| 277 | + transformState.translateX = 0; |
| 278 | + transformState.translateY = 0; |
| 279 | + |
| 280 | + requestAnimationFrame(() => { |
| 281 | + videoEl.style.transition = 'transform 0.3s ease-out'; |
| 282 | + updateTransform(); |
| 283 | + videoEl.classList.remove('zoomed'); |
| 284 | + }); |
| 285 | + }; |
| 286 | + |
| 287 | + // Adding resize observer for responsive boundaries |
| 288 | + const resizeObserver = new ResizeObserver(() => { |
| 289 | + if (player.isFullscreen()) updateTransform(); |
| 290 | + }); |
| 291 | + |
| 292 | + resizeObserver.observe(container); |
| 293 | + |
| 294 | + // Reset zoom when exiting fullscreen |
| 295 | + player.on('fullscreenchange', () => { |
| 296 | + if (!player.isFullscreen()) { |
| 297 | + resetZoom(); |
| 298 | + } |
| 299 | + }); |
| 300 | + |
| 301 | + // Cleanup function |
| 302 | + const cleanup = () => { |
| 303 | + resizeObserver.disconnect(); |
| 304 | + hammer.destroy(); |
| 305 | + if (zoomIndicator.timeoutId) clearTimeout(zoomIndicator.timeoutId); |
| 306 | + container.removeChild(zoomIndicator); |
| 307 | + resetZoom(); |
| 308 | + }; |
| 309 | + |
| 310 | + player.on('dispose', cleanup); |
| 311 | + return cleanup; |
| 312 | + }; |
| 313 | + |
99 | 314 | useEffect(() => {
|
100 | 315 | if (!player) return;
|
101 | 316 |
|
@@ -420,6 +635,7 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({
|
420 | 635 | () => {
|
421 | 636 | player.mobileUi(); // mobile ui #https://github.com/mister-ben/videojs-mobile-ui
|
422 | 637 | player.eme(); // Initialize EME
|
| 638 | + setupZoomFeatures(player); |
423 | 639 | player.seekButtons({
|
424 | 640 | forward: 15,
|
425 | 641 | back: 15,
|
|
0 commit comments