Skip to content

Commit

Permalink
Add interpolation to AutoScroll.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjerleke committed Jul 18, 2024
1 parent 6ed3a51 commit 2c0c528
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
17 changes: 10 additions & 7 deletions packages/embla-carousel-auto-scroll/src/components/AutoScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ function AutoScroll(userOptions: AutoScrollOptionsType = {}): AutoScrollType {
function createAutoScrollBehaviour(engine: EngineType): ScrollBodyType {
const {
location,
previousLocation,
offsetLocation,
target,
scrollTarget,
index,
Expand All @@ -158,20 +160,21 @@ function AutoScroll(userOptions: AutoScrollOptionsType = {}): AutoScrollType {
const directionSign = options.direction === 'forward' ? -1 : 1
const noop = (): ScrollBodyType => self

// TODO: Make it work with interpolation

let bodyVelocity = 0
let scrollDirection = 0
let rawLocation = location.get()
let rawLocationPrevious = 0
let hasSettled = false

function seek(): ScrollBodyType {
function seek(timeStep: number): ScrollBodyType {
const fixedDeltaTimeSeconds = timeStep / 1000
let directionDiff = 0

bodyVelocity = directionSign * options.speed
previousLocation.set(location)

bodyVelocity = directionSign * options.speed * 55
rawLocation += bodyVelocity
location.add(bodyVelocity)
location.add(bodyVelocity * fixedDeltaTimeSeconds)
target.set(location)

directionDiff = rawLocation - rawLocationPrevious
Expand All @@ -188,8 +191,8 @@ function AutoScroll(userOptions: AutoScrollOptionsType = {}): AutoScrollType {

const reachedEnd =
options.direction === 'forward'
? reachedMin(location.get())
: reachedMax(location.get())
? reachedMin(offsetLocation.get())
: reachedMax(offsetLocation.get())

if (!loop && reachedEnd) {
hasSettled = true
Expand Down
10 changes: 6 additions & 4 deletions packages/embla-carousel/src/components/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type EngineType = {
limit: LimitType
location: Vector1DType
offsetLocation: Vector1DType
previousLocation: Vector1DType
options: OptionsType
percentOfView: PercentOfViewType
scrollBody: ScrollBodyType
Expand Down Expand Up @@ -190,7 +191,7 @@ export function Engine(
if (!hasSettled) eventHandler.emit('scroll')

const interpolatedLocation =
location.get() * lagOffset + prevLocation.get() * (1 - lagOffset)
location.get() * lagOffset + previousLocation.get() * (1 - lagOffset)

offsetLocation.set(interpolatedLocation)

Expand All @@ -212,13 +213,13 @@ export function Engine(
const friction = 0.68
const startLocation = scrollSnaps[index.get()]
const location = Vector1D(startLocation)
const prevLocation = Vector1D(startLocation)
const previousLocation = Vector1D(startLocation)
const offsetLocation = Vector1D(startLocation)
const target = Vector1D(startLocation)
const scrollBody = ScrollBody(
location,
offsetLocation,
prevLocation,
previousLocation,
target,
duration,
friction
Expand Down Expand Up @@ -302,6 +303,7 @@ export function Engine(
limit,
location,
offsetLocation,
previousLocation,
options,
resizeHandler: ResizeHandler(
container,
Expand All @@ -323,7 +325,7 @@ export function Engine(
scrollLooper: ScrollLooper(contentSize, limit, offsetLocation, [
location,
offsetLocation,
prevLocation,
previousLocation,
target
]),
scrollProgress,
Expand Down
8 changes: 4 additions & 4 deletions packages/embla-carousel/src/components/ScrollBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type ScrollBodyType = {
export function ScrollBody(
location: Vector1DType,
offsetLocation: Vector1DType,
prevLocation: Vector1DType,
previousLocation: Vector1DType,
target: Vector1DType,
baseDuration: number,
baseFriction: number
Expand All @@ -30,19 +30,19 @@ export function ScrollBody(

function seek(timeStep: number): ScrollBodyType {
const fixedDeltaTimeSeconds = timeStep / 1000
const duration = scrollDuration * 0.016
const duration = scrollDuration * fixedDeltaTimeSeconds
const diff = target.get() - location.get()
const isInstant = !scrollDuration
let directionDiff = 0

if (isInstant) {
bodyVelocity = 0
prevLocation.set(target)
previousLocation.set(target)
location.set(target)

directionDiff = diff
} else {
prevLocation.set(location)
previousLocation.set(location)

bodyVelocity += diff / duration
bodyVelocity *= scrollFriction
Expand Down

0 comments on commit 2c0c528

Please sign in to comment.