Skip to content

Commit

Permalink
fix frame sync on complete
Browse files Browse the repository at this point in the history
  • Loading branch information
clementroche committed Nov 21, 2023
1 parent 94994ae commit 51f8228
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
20 changes: 12 additions & 8 deletions bundled/lenis.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
}

// Call the onUpdate callback with the current value and completed status

(_this$onUpdate = this.onUpdate) == null ? void 0 : _this$onUpdate.call(this, this.value, completed);
if (completed) {
this.stop();
Expand Down Expand Up @@ -492,7 +491,8 @@
lerp: hasTouchInertia ? _this.syncTouchLerp : _this.options.__iosNoInertiaSyncTouchLerp
}));
};
this.onScroll = function () {
this.onNativeScroll = function () {
if (_this.__preventNextScrollEvent) return;
if (!_this.isScrolling) {
var lastScroll = _this.animatedScroll;
_this.animatedScroll = _this.targetScroll = _this.actualScroll;
Expand Down Expand Up @@ -543,7 +543,7 @@
this.isSmooth = _syncTouch || smoothWheel || smoothTouch;
this.isScrolling = false;
this.targetScroll = this.animatedScroll = this.actualScroll;
this.options.wrapper.addEventListener('scroll', this.onScroll, {
this.options.wrapper.addEventListener('scroll', this.onNativeScroll, {
passive: false
});
this.virtualScroll = new VirtualScroll(eventsTarget, {
Expand All @@ -556,7 +556,7 @@
var _proto = Lenis.prototype;
_proto.destroy = function destroy() {
this.emitter.destroy();
this.options.wrapper.removeEventListener('scroll', this.onScroll, {
this.options.wrapper.removeEventListener('scroll', this.onNativeScroll, {
passive: false
});
this.virtualScroll.destroy();
Expand Down Expand Up @@ -590,6 +590,7 @@
_proto.reset = function reset() {
this.isLocked = false;
this.isScrolling = false;
this.animatedScroll = this.targetScroll = this.actualScroll;
this.velocity = 0;
this.animate.stop();
};
Expand Down Expand Up @@ -699,11 +700,14 @@
}
if (!completed) _this2.emit();
if (completed) {
// avoid emitting twice (onScroll)
_this2.reset();
_this2.emit();
onComplete == null ? void 0 : onComplete(_this2);

// avoid emitting event twice
_this2.__preventNextScrollEvent = true;
requestAnimationFrame(function () {
_this2.reset();
_this2.emit();
onComplete == null ? void 0 : onComplete(_this2);
delete _this2.__preventNextScrollEvent;
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default class Lenis {
}) => void;
resize(): void;
emit(): void;
onScroll: () => void;
onNativeScroll: () => void;
direction: number;
reset(): void;
start(): void;
Expand All @@ -116,6 +116,7 @@ export default class Lenis {
force?: boolean;
programmatic?: boolean;
}): void;
__preventNextScrollEvent: boolean;
get rootElement(): Window | HTMLElement;
get limit(): number;
get isHorizontal(): boolean;
Expand Down
1 change: 0 additions & 1 deletion src/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class Animate {
}

// Call the onUpdate callback with the current value and completed status

this.onUpdate?.(this.value, completed)

if (completed) {
Expand Down
20 changes: 13 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default class Lenis {
this.isScrolling = false
this.targetScroll = this.animatedScroll = this.actualScroll

this.options.wrapper.addEventListener('scroll', this.onScroll, {
this.options.wrapper.addEventListener('scroll', this.onNativeScroll, {
passive: false,
})

Expand All @@ -127,7 +127,7 @@ export default class Lenis {
destroy() {
this.emitter.destroy()

this.options.wrapper.removeEventListener('scroll', this.onScroll, {
this.options.wrapper.removeEventListener('scroll', this.onNativeScroll, {
passive: false,
})

Expand Down Expand Up @@ -243,7 +243,9 @@ export default class Lenis {
this.emitter.emit('scroll', this)
}

onScroll = () => {
onNativeScroll = () => {
if (this.__preventNextScrollEvent) return

if (!this.isScrolling) {
const lastScroll = this.animatedScroll
this.animatedScroll = this.targetScroll = this.actualScroll
Expand All @@ -256,6 +258,7 @@ export default class Lenis {
reset() {
this.isLocked = false
this.isScrolling = false
this.animatedScroll = this.targetScroll = this.actualScroll
this.velocity = 0
this.animate.stop()
}
Expand Down Expand Up @@ -380,11 +383,14 @@ export default class Lenis {
if (!completed) this.emit()

if (completed) {
// avoid emitting twice (onScroll)
this.reset()
this.emit()
onComplete?.(this)

// avoid emitting event twice
this.__preventNextScrollEvent = true
requestAnimationFrame(() => {
this.reset()
this.emit()
onComplete?.(this)
delete this.__preventNextScrollEvent
})
}
},
Expand Down
4 changes: 2 additions & 2 deletions website/layouts/default/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function Layout({
window.scrollTo(0, 0)
const lenis = new Lenis({
// gestureOrientation: 'both',
// smoothWheel: false,
smoothWheel: true,
// smoothTouch: true,
syncTouch: true,
})
Expand Down Expand Up @@ -96,7 +96,7 @@ export function Layout({

useFrame((time) => {
lenis?.raf(time)
}, [])
}, 0)

return (
<>
Expand Down
7 changes: 5 additions & 2 deletions website/pages/home/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRect } from '@studio-freight/hamo'
import { useFrame, useRect } from '@studio-freight/hamo'
import cn from 'clsx'

import { Button } from 'components/button'
Expand Down Expand Up @@ -186,9 +186,12 @@ export default function Home() {

useScroll((e) => {
console.log(window.scrollY, e.scroll, e.isScrolling, e.velocity, e.isLocked)
// console.log(e.isScrolling)
})

useFrame(() => {
console.log('frame', window.scrollY, lenis?.scroll, lenis?.isScrolling)
}, 1)

const inUseRef = useRef()

const [visible, setIsVisible] = useState(false)
Expand Down

2 comments on commit 51f8228

@vercel
Copy link

@vercel vercel bot commented on 51f8228 Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"⚡️ Lighthouse report for the changes in this commit:

🔴 Performance: 28
🟠 Accessibility: 88
🟢 Best practices: 100
🟠 SEO: 67
🔴 PWA: 0

Lighthouse ran on https://gsap.com/requires-membership/?plugin=SplitText&source=trial"

Please sign in to comment.