-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-cinematic-video-loop.ts
More file actions
134 lines (107 loc) · 3.34 KB
/
Copy pathuse-cinematic-video-loop.ts
File metadata and controls
134 lines (107 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { useEffect, useMemo, useRef, useState } from 'react'
const FADE_DURATION_SECONDS = 0.5
const RESTART_DELAY_MS = 100
function clampOpacity(value: number) {
return Math.min(1, Math.max(0, value))
}
export function useCinematicVideoLoop() {
const videoRef = useRef<HTMLVideoElement>(null)
const mediaQuery = useMemo(
() => window.matchMedia('(prefers-reduced-motion: reduce)'),
[],
)
const [hasVideoError, setHasVideoError] = useState(false)
const [prefersReducedMotion, setPrefersReducedMotion] = useState(() => mediaQuery.matches)
useEffect(() => {
const handlePreferenceChange = (event: MediaQueryListEvent) => {
setPrefersReducedMotion(event.matches)
}
mediaQuery.addEventListener('change', handlePreferenceChange)
return () => mediaQuery.removeEventListener('change', handlePreferenceChange)
}, [mediaQuery])
useEffect(() => {
const video = videoRef.current
if (!video) {
return
}
let animationFrameId: number | undefined
let restartTimeoutId: number | undefined
let isDisposed = false
let hasFailed = false
const setVideoOpacity = (opacity: number) => {
video.style.opacity = String(clampOpacity(opacity))
}
const handlePlaybackFailure = () => {
if (isDisposed) {
return
}
hasFailed = true
setVideoOpacity(0)
setHasVideoError(true)
if (animationFrameId !== undefined) {
window.cancelAnimationFrame(animationFrameId)
}
}
const playVideo = () => {
void video.play().catch(handlePlaybackFailure)
}
const updateOpacity = () => {
if (isDisposed || hasFailed) {
return
}
const { currentTime, duration } = video
let opacity = 1
if (currentTime < FADE_DURATION_SECONDS) {
opacity = currentTime / FADE_DURATION_SECONDS
} else if (
Number.isFinite(duration) &&
duration > 0 &&
duration - currentTime < FADE_DURATION_SECONDS
) {
opacity = (duration - currentTime) / FADE_DURATION_SECONDS
}
setVideoOpacity(opacity)
animationFrameId = window.requestAnimationFrame(updateOpacity)
}
const handleEnded = () => {
setVideoOpacity(0)
if (restartTimeoutId !== undefined) {
window.clearTimeout(restartTimeoutId)
}
restartTimeoutId = window.setTimeout(() => {
if (isDisposed || hasFailed) {
return
}
video.currentTime = 0
playVideo()
}, RESTART_DELAY_MS)
}
const handleError = () => {
handlePlaybackFailure()
}
video.addEventListener('ended', handleEnded)
video.addEventListener('error', handleError)
setVideoOpacity(0)
if (prefersReducedMotion) {
video.pause()
video.currentTime = 0
setVideoOpacity(1)
} else {
playVideo()
animationFrameId = window.requestAnimationFrame(updateOpacity)
}
return () => {
isDisposed = true
video.removeEventListener('ended', handleEnded)
video.removeEventListener('error', handleError)
video.pause()
if (animationFrameId !== undefined) {
window.cancelAnimationFrame(animationFrameId)
}
if (restartTimeoutId !== undefined) {
window.clearTimeout(restartTimeoutId)
}
}
}, [prefersReducedMotion])
return { videoRef, hasVideoError, prefersReducedMotion }
}