GithubHelp home page GithubHelp logo

snelsi / lethargy-ts Goto Github PK

View Code? Open in Web Editor NEW

This project forked from d4nyll/lethargy

61.0 1.0 0.0 594 KB

๐Ÿ” Distinguish between intentional wheel events and inertial scrolling

Home Page: https://lethargy-ts.vercel.app/

License: MIT License

JavaScript 1.12% TypeScript 87.69% HTML 1.79% SCSS 9.40%
inertial magic mouse mousewheel scroll scrolljacking trackpad wheel

lethargy-ts's Introduction

โญ Lethargy-TS

lethargy-ts is a modern TypeScript rewrite of lethargy โ€“ a popular JavaScript library to help distinguish between scroll events initiated by the user, and those by inertial scrolling.

npm (scoped) Bundle Size type definition License: MIT

๐ŸŒณ Tiny and easy to use

๐Ÿฆ„ Written in TypeScript

๐ŸŽ Highly customizable

๐Ÿ– No external dependencies

Install

Install with Yarn:

yarn add lethargy-ts

Or with npm:

npm install --save lethargy-ts

Usage

Import and create an instance of Lethargy. It will remember previously checked wheelEvents to help determine if they are inertial or not:

import { Lethargy } from "lethargy-ts";

const lethargy = new Lethargy();

You can customize the sensitivity, delay, and other parameters to better match your application's needs:

const lethargy = new Lethargy({
  sensitivity: 2,
  delay: 100,
  increasingDeltasThreshold: 3,
});

๐Ÿ˜‰ If you find optimizations for the defaults, please share them in this discussion!

Bind the wheel event and pass the event to Lethargy:

const checkWheelEvent = (e: WheelEvent) => {
  const isIntentional = lethargy.check(e);

  if (isIntentional) {
    // Do something with the scroll event
  }
};

window.addEventListener("wheel", checkWheelEvent, { passive: true });

lethargy.check(e) will return true if it's a normal wheel event initiated by the user, and false if it's initiated by inertial scrolling.

Options

All parameters are optional:

  • sensitivity - Specifies the minimum value for wheelDelta for it to register as a valid scroll event. Because the tail of the curve has low wheelDelta values, this will stop them from registering as valid scroll events.

  • delay - If there was a pause of this amount of milliseconds between two events, the current event is assumed to be user-triggered.

  • increasingDeltasThreshold - If wheelDelta has been increasing for this amount of consecutive events, the current event is assumed to be user-triggered.

What problem does it solve?

Scroll plugins such as smartscroll, jquery-mousewheel, or fullPage.js work by detecting scroll events and then doing something with them, such as scrolling to the next frame. However, inertial scrolling continues to emit scroll events even after the user stopped, and this can often lead to problems, such as scrolling multiple frames when the user only intended to scroll once. lethargy-ts helps distinguish between scroll events initiated by the user and those by inertial scrolling, making it easier to create smooth and accurate scrolling experiences.

How it works

lethargy-ts uses a set of checks to determine if a scroll event is initiated by the user or by inertial scrolling. The following checks are performed:

  • Enough time has passed between two events.

  • The delta of the event doesn't decrease and immediately follows a known human event.

  • The delta of the event has been increasing for n consecutive events.

If any of these checks are true, the event is considered intentional. Otherwise, it is considered to be initiated by inertial scrolling.

Limitations

Not all trackpads work the same, so Lethargy makes its best effort to cover as many use cases as possible, but full coverage is not guaranteed. Lethargy focuses on preventing false positives (i.e., saying it's a normal scroll event when it wasn't) but tolerates false negatives (i.e., saying it's not a normal scroll event when it is).

Sadly, right now there is no easy native way to tell if the wheel event was generated by the user or by inertia. You can change this by leaving an upvote and a comment in the w3c proposal ticket. ๐Ÿ‘ˆ

TypeScript

The module is written in TypeScript and type definitions are included.

Contributing

Contributions, issues, and feature requests are welcome!

Show your support

If you find this project useful, please give it a star on GitHub! โญ๏ธ

LICENSE

MIT

lethargy-ts's People

Contributors

ayamflow avatar d4nyll avatar fossabot avatar pvnr0082t avatar snelsi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

lethargy-ts's Issues

Incompatible with useGestureโ€™s useWheel event handler

Describe the bug
When passing the event from useGestureโ€™s wheel gesture, I get this error:

Uncaught Error: No event provided

To Reproduce
Steps to reproduce the behavior:
Implement basic wheel gesture like in this example:
https://codesandbox.io/p/sandbox/lethargy-2coe8?file=%2Fsrc%2Findex.js%3A22%2C41

Expected behavior
lethargy.check(event) is expected to return -1 or 1 but instead the above mentioned error is thrown.

Screenshots
Screenshot 2024-01-03 at 20 18 06

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.