-
Notifications
You must be signed in to change notification settings - Fork 0
/
useVirtualScroll.ts
200 lines (155 loc) · 4.12 KB
/
useVirtualScroll.ts
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {useEventListener, useRafFn} from '@vueuse/core'
import {scalar} from 'linearly'
import {debounce} from 'lodash'
import {MaybeRef, readonly, ref} from 'vue'
import {DragEvent, useElementDrag} from './useElementDrag'
// 慣性スクロールを無効化するまでの指を動かさないでいる時間 (sec)
const InertiaCancelDuration = 0.2
// 慣性スクロールが効き始める最低速度 (vh/sec)
const MinInertiaScrollSpeedInVh = 10
export interface VirtualScrollEvent {
offset: number
delta: number
}
interface UseVirtualScrollOptions {
onSwipe: (e: VirtualScrollEvent) => void
onPointerdown: (e: VirtualScrollEvent) => void
onPointerup: (e: VirtualScrollEvent) => void
mapScroll: (y: number) => number
}
export function useVirtualScroll(
el: MaybeRef<HTMLElement | null>,
options: UseVirtualScrollOptions
) {
const scroll = ref(0)
let swipeDelta = 0
/** Pepresented in px / second */
let inertiaSpeed = 0
let easeSpeedOptions: null | {
get: () => number
onReach: () => void
} = null
useEventListener(el, 'wheel', onWheel)
// Scroll by wheel
let wheelScrollOrigin: null | number = null
function onWheel(e: WheelEvent) {
swipeDelta = inertiaSpeed = 0
scroll.value = options.mapScroll(scroll.value + e.deltaY)
if (wheelScrollOrigin === null) {
options.onPointerdown({
offset: 0,
delta: 0,
})
}
options.onSwipe({
offset: 0,
delta: e.deltaY,
})
willTriggerOnPointerup()
}
const willTriggerOnPointerup = debounce(() => {
options.onPointerup({
offset: 0,
delta: 0,
})
wheelScrollOrigin = null
}, 100)
// Scroll by swipe
const {dragging} = useElementDrag(el, {
onPointerdown,
onDrag,
onPointerup,
})
function onPointerdown() {
cancelInertia()
options.onPointerdown({
offset: 0,
delta: 0,
})
}
function onDrag(e: DragEvent) {
scroll.value = options.mapScroll(scroll.value - e.movement[1])
swipeDelta += e.movement[1]
onLongPress()
options.onSwipe({
offset: e.offset[1],
delta: -e.movement[1],
})
}
const onLongPress = debounce(
() => {
if (!dragging.value) return
cancelInertia()
},
InertiaCancelDuration * 1000,
{}
)
function onPointerup(e: DragEvent) {
const minInertiaSpeed =
(window.innerHeight * MinInertiaScrollSpeedInVh) / 100
if (Math.abs(inertiaSpeed) < minInertiaSpeed) {
inertiaSpeed = 0
}
swipeDelta = 0
options.onPointerup({
offset: e.offset[1],
delta: 0,
})
}
// Inertial scrolling
useRafFn(({delta: deltaTimeMs}) => {
const deltaTime = deltaTimeMs / 1000
const swipeSpeed = swipeDelta / deltaTime
swipeDelta = 0
// Lerp inertiaSpeed to swipeSpeed
if (deltaTime > 0) {
const directionChanged = swipeSpeed * inertiaSpeed < 0
const swipeSpeedExceeded = Math.abs(swipeSpeed) > Math.abs(inertiaSpeed)
if (directionChanged || swipeSpeedExceeded) {
inertiaSpeed = swipeSpeed
} else {
const halfLife = dragging.value ? 0.05 : 1
const ratio = 1 - 2 ** (-deltaTime / halfLife)
inertiaSpeed = scalar.lerp(inertiaSpeed, swipeSpeed, ratio)
}
// Accelate/decelerate to the target speed
if (easeSpeedOptions !== null) {
const targetSpeed = easeSpeedOptions.get()
const doAccelate = targetSpeed > inertiaSpeed
const diff =
Math.abs(inertiaSpeed - targetSpeed) / Math.abs(targetSpeed)
const rate = scalar.efit(diff, 0, 10, 1000, 5000)
inertiaSpeed += (doAccelate ? 1 : -1) * rate * deltaTime
const reached = doAccelate
? inertiaSpeed >= targetSpeed
: inertiaSpeed <= targetSpeed
if (reached) {
inertiaSpeed = 0
easeSpeedOptions.onReach()
easeSpeedOptions = null
}
}
}
if (!dragging.value && Math.abs(inertiaSpeed) > 1) {
const delta = inertiaSpeed * deltaTime
scroll.value = options.mapScroll(scroll.value - delta)
}
})
function cancelInertia() {
swipeDelta = inertiaSpeed = 0
wheelScrollOrigin = null
easeSpeedOptions = null
}
function scrollTo(y: number) {
scroll.value = options.mapScroll(y)
}
function easeToSpeed(options: typeof easeSpeedOptions) {
easeSpeedOptions = options
}
return {
scroll: readonly(scroll),
scrollTo,
cancelInertia,
easeToSpeed,
}
}