-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollScrubber.tsx
More file actions
180 lines (166 loc) · 5.36 KB
/
ScrollScrubber.tsx
File metadata and controls
180 lines (166 loc) · 5.36 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { useEffect, useRef, useState } from "react"
import { useViewportScroll, useTransform, motion } from "framer-motion"
import { addPropertyControls, ControlType } from "framer"
/**
* @framerSupportedLayoutWidth any-prefer-fixed
* @framerIntrinsicWidth 600
* @framerSupportedLayoutHeight any-prefer-fixed
* @framerIntrinsicHeight 400
*/
export default function ScrollVideo({
videoSourceType,
videoSrcURL,
videoSrcFile,
scrollLength,
videoAlignment,
startOn,
videoFit,
}) {
const videoRef = useRef(null)
const containerRef = useRef(null)
const { scrollY } = useViewportScroll()
const [containerTop, setContainerTop] = useState(0)
const [containerHeight, setContainerHeight] = useState(0)
useEffect(() => {
if (containerRef.current) {
const rect = containerRef.current.getBoundingClientRect()
setContainerTop(window.pageYOffset + rect.top)
setContainerHeight(rect.height)
}
}, [])
const videoProgress = useTransform(
scrollY,
[containerTop, containerTop + containerHeight],
[0, 1]
)
useEffect(() => {
let observer
let options = {}
if (startOn === "top") {
options = { threshold: 0 }
} else if (startOn === "center") {
options = { threshold: 0.5 }
} else if (startOn === "bottom") {
options = { threshold: 1 }
}
observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting && videoRef.current) {
const scrollPosition =
window.pageYOffset + entry.boundingClientRect.top
const progress =
(scrollPosition - containerTop) / containerHeight
videoRef.current.currentTime =
progress * videoRef.current.duration
}
}, options)
if (videoRef.current) {
observer.observe(videoRef.current)
}
return () => {
if (videoRef.current) {
observer.unobserve(videoRef.current)
}
}
}, [startOn, containerTop, containerHeight])
useEffect(() => {
const updateVideoTime = () => {
if (videoRef.current) {
const scrollPosition = window.pageYOffset
const progress =
(scrollPosition - containerTop) / containerHeight
videoRef.current.currentTime =
Math.max(0, Math.min(1, progress)) *
videoRef.current.duration
}
}
window.addEventListener("scroll", updateVideoTime)
return () => window.removeEventListener("scroll", updateVideoTime)
}, [containerTop, containerHeight])
const videoSrc = videoSourceType === "url" ? videoSrcURL : videoSrcFile
const alignmentStyles = {
center: { top: "50%", transform: "translateY(-50%)" },
top: { top: 0 },
bottom: { bottom: 0 },
}
return (
<motion.div
ref={containerRef}
style={{ height: scrollLength, position: "relative" }}
>
<video
ref={videoRef}
src={videoSrc}
style={{
width: "100%",
height: "100vh",
position: "sticky",
objectFit: videoFit,
...alignmentStyles[videoAlignment],
}}
muted
playsInline
/>
</motion.div>
)
}
ScrollVideo.defaultProps = {
videoSourceType: "url",
videoSrcURL:
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
scrollLength: 500,
videoAlignment: "center",
startOn: "top",
videoFit: "cover",
}
addPropertyControls(ScrollVideo, {
videoSourceType: {
type: ControlType.Enum,
title: "Source Type",
options: ["url", "file"],
optionTitles: ["URL", "File"],
defaultValue: "url",
},
videoSrcURL: {
type: ControlType.String,
title: "Video URL",
defaultValue:
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
hidden: ({ videoSourceType }) => videoSourceType !== "url",
},
videoSrcFile: {
type: ControlType.File,
title: "Video File",
allowedFileTypes: ["mp4", "mov", "webm"],
hidden: ({ videoSourceType }) => videoSourceType !== "file",
},
scrollLength: {
type: ControlType.Number,
title: "Scroll Length",
defaultValue: 500,
min: 100,
max: 20000,
step: 30,
},
videoAlignment: {
type: ControlType.Enum,
title: "Video Alignment",
options: ["center", "top", "bottom"],
optionTitles: ["Center", "Top", "Bottom"],
defaultValue: "center",
},
videoFit: {
type: ControlType.Enum,
title: "Video Fit",
options: ["cover", "contain", "fill"],
optionTitles: ["Cover", "Contain", "Fill"],
defaultValue: "cover",
},
startOn: {
type: ControlType.Enum,
title: "Start On",
options: ["top", "center", "bottom"],
optionTitles: ["Top", "Center", "Bottom"],
defaultValue: "top",
description: "v1.0 \n[via SegmentUI](https://www.segmentUI.com)",
},
})