add mouse flicks to the gif filmstrip
This commit is contained in:
@@ -0,0 +1,264 @@
|
||||
import { gsap } from "gsap"
|
||||
import { bound, duration } from "./motion"
|
||||
|
||||
export type FilmstripView = { first: number; last: number; before: boolean; after: boolean }
|
||||
|
||||
const coastRate = -1000 * Math.log(.998)
|
||||
const stopSpeed = 35
|
||||
const sampleWindow = 90
|
||||
const dragThreshold = 5
|
||||
const rubberCoefficient = .55
|
||||
const springRate = 15
|
||||
|
||||
export function kineticFilmstrip(
|
||||
host: HTMLElement, viewport: HTMLElement, track: HTMLElement,
|
||||
{ stride, gap, inset, onView, onScrub }: {
|
||||
stride: number; gap: number; inset: number
|
||||
onView: (view: FilmstripView) => void; onScrub: (frame: number) => void
|
||||
},
|
||||
) {
|
||||
let count = 0
|
||||
let width = 0
|
||||
let limit = 0
|
||||
let position = 0
|
||||
let enabled = false
|
||||
let tween: gsap.core.Tween | undefined
|
||||
let wheelTarget: number | undefined
|
||||
let gestureOrigin: number | undefined
|
||||
let scrubFrame: number | undefined
|
||||
let drag: {
|
||||
id: number; x: number; y: number; origin: number; started: boolean
|
||||
samples: { x: number; time: number }[]
|
||||
} | undefined
|
||||
let suppressClick = false
|
||||
const translate = gsap.quickSetter(track, "x", "px")
|
||||
const events = new AbortController()
|
||||
const { signal } = events
|
||||
let view: FilmstripView | undefined
|
||||
|
||||
function frameAt(x: number) {
|
||||
return bound(Math.floor((x - inset) / stride), 0, Math.max(0, count - 1))
|
||||
}
|
||||
|
||||
function paint(next: number) {
|
||||
position = next
|
||||
const offset = bound(next, 0, limit)
|
||||
const excess = (next - offset) * rubberCoefficient
|
||||
const distance = Math.min(74, width * .22)
|
||||
const stretch = distance ? excess / (1 + Math.abs(excess) / distance) : 0
|
||||
translate(-offset - stretch)
|
||||
|
||||
if (limit && gestureOrigin !== undefined && (scrubFrame !== undefined || Math.abs(offset - gestureOrigin) >= 1)) {
|
||||
// Follow the clamped offset so the playhead stays inside the trim.
|
||||
const frame = Math.round(offset / limit * (count - 1))
|
||||
if (frame !== scrubFrame) {
|
||||
scrubFrame = frame
|
||||
onScrub(frame)
|
||||
}
|
||||
}
|
||||
|
||||
const first = frameAt(offset + gap)
|
||||
const last = frameAt(offset + width - .5)
|
||||
const before = limit > 0 && offset > inset + .5
|
||||
const after = limit > 0 && offset < limit - inset - .5
|
||||
if (view?.first === first && view.last === last && view.before === before && view.after === after) return
|
||||
view = { first, last, before, after }
|
||||
onView(view)
|
||||
}
|
||||
|
||||
function interrupt() {
|
||||
tween?.kill()
|
||||
tween = undefined
|
||||
wheelTarget = undefined
|
||||
gestureOrigin = undefined
|
||||
scrubFrame = undefined
|
||||
}
|
||||
|
||||
function simulate(seconds: number, at: (elapsed: number) => number, complete: () => void) {
|
||||
const clock = { elapsed: 0 }
|
||||
tween = gsap.to(clock, {
|
||||
elapsed: seconds, duration: seconds, ease: "none",
|
||||
onUpdate: () => paint(at(clock.elapsed)), onComplete: complete,
|
||||
})
|
||||
}
|
||||
|
||||
function bounce(velocity = 0) {
|
||||
const edge = bound(position, 0, limit)
|
||||
const excess = position - edge
|
||||
if (!limit || !duration() || (!excess && !velocity)) {
|
||||
paint(edge)
|
||||
interrupt()
|
||||
return
|
||||
}
|
||||
// Critically damped return. Keep the spring on the overscrolled side of the edge.
|
||||
const outward = Math.sign(excess || velocity)
|
||||
const speed = outward * bound(outward * velocity, -Math.abs(excess) * springRate, 3800)
|
||||
simulate(.56, time => edge + (excess + (speed + springRate * excess) * time) * Math.exp(-springRate * time), () => {
|
||||
paint(edge)
|
||||
interrupt()
|
||||
})
|
||||
}
|
||||
|
||||
function coast(velocity: number) {
|
||||
if (position < 0 || position > limit) return bounce(velocity)
|
||||
if (!duration() || Math.abs(velocity) < stopSpeed) return interrupt()
|
||||
const start = position
|
||||
const edge = velocity < 0 ? 0 : limit
|
||||
const remaining = (edge - start) * coastRate / velocity
|
||||
const hit = remaining < 1 ? -Math.log1p(-remaining) / coastRate : Infinity
|
||||
const end = bound(Math.log(Math.abs(velocity) / stopSpeed) / coastRate, .5, 2)
|
||||
const time = Math.min(hit, end)
|
||||
const hitEdge = hit <= end
|
||||
const finish = () => {
|
||||
if (!hitEdge) return interrupt()
|
||||
paint(edge)
|
||||
bounce(velocity * Math.exp(-coastRate * hit) / rubberCoefficient)
|
||||
}
|
||||
if (time <= .001) return finish()
|
||||
simulate(time, elapsed => start - velocity * Math.expm1(-coastRate * elapsed) / coastRate, finish)
|
||||
}
|
||||
|
||||
function endDrag() {
|
||||
const previous = drag
|
||||
drag = undefined
|
||||
delete host.dataset.dragging
|
||||
if (previous && host.hasPointerCapture(previous.id)) host.releasePointerCapture(previous.id)
|
||||
return previous
|
||||
}
|
||||
|
||||
host.addEventListener("pointerdown", event => {
|
||||
if (event.button !== 0 || event.pointerType !== "mouse") return
|
||||
suppressClick = false
|
||||
if (!enabled || !limit || drag) return
|
||||
interrupt()
|
||||
if (event.shiftKey || event.ctrlKey || event.metaKey) {
|
||||
paint(bound(position, 0, limit))
|
||||
return
|
||||
}
|
||||
drag = { id: event.pointerId, x: event.clientX, y: event.clientY, origin: position, started: false,
|
||||
samples: [{ x: event.clientX, time: event.timeStamp }] }
|
||||
}, { signal })
|
||||
|
||||
window.addEventListener("pointermove", event => {
|
||||
if (!drag || drag.id !== event.pointerId) return
|
||||
const x = event.clientX
|
||||
const delta = x - drag.x
|
||||
if (!drag.started) {
|
||||
if (Math.abs(delta) < dragThreshold || Math.abs(delta) < Math.abs(event.clientY - drag.y)) return
|
||||
drag.started = true
|
||||
suppressClick = true
|
||||
gestureOrigin = bound(drag.origin, 0, limit)
|
||||
host.setPointerCapture(event.pointerId)
|
||||
host.dataset.dragging = ""
|
||||
}
|
||||
event.preventDefault()
|
||||
const time = event.timeStamp
|
||||
drag.samples.push({ x, time })
|
||||
while (drag.samples.length > 2 && drag.samples[0].time < time - sampleWindow) drag.samples.shift()
|
||||
const next = drag.origin - delta
|
||||
paint(duration() ? next : bound(next, 0, limit))
|
||||
}, { signal })
|
||||
|
||||
window.addEventListener("pointerup", event => {
|
||||
if (drag?.id !== event.pointerId) return
|
||||
const previous = endDrag()
|
||||
if (!previous?.started) {
|
||||
if (position !== bound(position, 0, limit)) bounce()
|
||||
return
|
||||
}
|
||||
const first = previous.samples[0]
|
||||
const last = previous.samples[previous.samples.length - 1]
|
||||
const velocity = event.timeStamp - last.time < sampleWindow
|
||||
? bound((first.x - last.x) * 1000 / Math.max(16, event.timeStamp - first.time), -3000, 3000) : 0
|
||||
coast(velocity)
|
||||
}, { signal })
|
||||
|
||||
function cancelDrag(event: PointerEvent) {
|
||||
if (drag?.id !== event.pointerId) return
|
||||
endDrag()
|
||||
bounce()
|
||||
}
|
||||
window.addEventListener("pointercancel", cancelDrag, { signal })
|
||||
host.addEventListener("lostpointercapture", cancelDrag, { signal })
|
||||
host.addEventListener("click", event => {
|
||||
if (!suppressClick || event.detail === 0) return
|
||||
suppressClick = false
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
}, { capture: true, signal })
|
||||
|
||||
host.addEventListener("wheel", event => {
|
||||
if (!enabled || !limit || !event.cancelable || event.ctrlKey || event.metaKey) return
|
||||
const delta = Math.abs(event.deltaX) >= Math.abs(event.deltaY) ? event.deltaX : event.deltaY
|
||||
if (!delta) return
|
||||
event.preventDefault()
|
||||
if (drag) return
|
||||
const unit = event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? width : 1
|
||||
const allowance = Math.min(125, width * .22)
|
||||
gestureOrigin ??= bound(position, 0, limit)
|
||||
const target = bound((wheelTarget ?? position) + delta * unit, -allowance, limit + allowance)
|
||||
if (target === wheelTarget) return
|
||||
tween?.kill()
|
||||
wheelTarget = target
|
||||
if (!duration()) {
|
||||
paint(bound(target, 0, limit))
|
||||
interrupt()
|
||||
return
|
||||
}
|
||||
const offset = { x: position }
|
||||
tween = gsap.to(offset, { x: target, duration: .5, ease: "power2.out",
|
||||
onUpdate: () => paint(offset.x), onComplete: () => {
|
||||
wheelTarget = undefined
|
||||
bounce()
|
||||
},
|
||||
})
|
||||
}, { passive: false, signal })
|
||||
|
||||
function measure() {
|
||||
const next = viewport.clientWidth
|
||||
if (!next) return
|
||||
width = next
|
||||
limit = Math.max(0, 2 * inset + count * stride - gap - width)
|
||||
host.dataset.scrollable = String(limit > 0)
|
||||
}
|
||||
|
||||
const resize = new ResizeObserver(() => {
|
||||
if (!viewport.clientWidth || viewport.clientWidth === width) return
|
||||
interrupt()
|
||||
endDrag()
|
||||
measure()
|
||||
paint(bound(position, 0, limit))
|
||||
})
|
||||
resize.observe(viewport)
|
||||
|
||||
return {
|
||||
setRange(frames: number, center: number) {
|
||||
interrupt()
|
||||
endDrag()
|
||||
count = frames
|
||||
measure()
|
||||
paint(bound(center * stride + inset + (stride - gap) / 2 - width / 2, 0, limit))
|
||||
},
|
||||
setEnabled(value: boolean) {
|
||||
enabled = value
|
||||
if (value) return
|
||||
interrupt()
|
||||
endDrag()
|
||||
paint(bound(position, 0, limit))
|
||||
},
|
||||
reveal(index: number) {
|
||||
const left = inset + index * stride
|
||||
const right = left + stride - gap
|
||||
if (!width || (left >= position && right <= position + width)) return
|
||||
interrupt()
|
||||
endDrag()
|
||||
paint(bound(left < position ? left - inset : right + inset - width, 0, limit))
|
||||
},
|
||||
destroy() {
|
||||
interrupt()
|
||||
endDrag()
|
||||
resize.disconnect()
|
||||
events.abort()
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user