Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lethargy-ts

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lethargy-ts - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

4

lib/index.esm.js

@@ -29,3 +29,3 @@ const scrollDirections = ["up", "down", "left", "right"];

class Lethargy {
constructor({ stability = 8, sensitivity = 100, tolerance = 1.1, delay = 150 } = {}) {
constructor({ stability = 8, sensitivity = 100, tolerance = 0.1, delay = 150 } = {}) {
this.stability = stability;

@@ -75,3 +75,3 @@ this.sensitivity = sensitivity;

const newAverage = getAverage(newDeltas);
const newAverageIsHigher = Math.abs(newAverage * this.tolerance) > Math.abs(oldAverage);
const newAverageIsHigher = Math.abs(newAverage * (1 + this.tolerance)) > Math.abs(oldAverage);
const matchesSensitivity = Math.abs(newAverage) > this.sensitivity;

@@ -78,0 +78,0 @@ if (newAverageIsHigher && matchesSensitivity) {

@@ -31,3 +31,3 @@ 'use strict';

class Lethargy {
constructor({ stability = 8, sensitivity = 100, tolerance = 1.1, delay = 150 } = {}) {
constructor({ stability = 8, sensitivity = 100, tolerance = 0.1, delay = 150 } = {}) {
this.stability = stability;

@@ -77,3 +77,3 @@ this.sensitivity = sensitivity;

const newAverage = getAverage(newDeltas);
const newAverageIsHigher = Math.abs(newAverage * this.tolerance) > Math.abs(oldAverage);
const newAverageIsHigher = Math.abs(newAverage * (1 + this.tolerance)) > Math.abs(oldAverage);
const matchesSensitivity = Math.abs(newAverage) > this.sensitivity;

@@ -80,0 +80,0 @@ if (newAverageIsHigher && matchesSensitivity) {

{
"name": "lethargy-ts",
"version": "0.0.1",
"version": "0.0.2",
"description": "Distinguish between scroll events initiated by the user, and those by inertial scrolling",

@@ -15,4 +15,3 @@ "repository": "https://github.com/snelsi/lethargy-ts",

"trackpad",
"magic",
"mouse"
"magic"
],

@@ -44,3 +43,3 @@ "main": "lib/index.js",

"pretty-quick": "^3.1.3",
"rollup": "^3.15.0",
"rollup": "^3.16.0",
"typescript": "^4.9.5"

@@ -47,0 +46,0 @@ },

@@ -1,101 +0,109 @@

# Lethargy
# ⭐ Lethargy-TS
[![CDNJS](https://img.shields.io/cdnjs/v/lethargy-ts.svg)](https://cdnjs.com/libraries/lethargy-ts)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fd4nyll%2Flethargy-ts.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fd4nyll%2Flethargy-ts?ref=badge_shield)
`lethargy-ts` is a modern rewrite of `lethargy` – a popular JavaScript library to help distinguish between scroll events initiated by the user, and those by inertial scrolling.
Lethargy-ts is a tiny (612b minified + gzipped) JavaScript library to help distinguish between scroll events initiated by the user, and those by inertial scrolling. Lethargy does **_not_** have external dependencies.
[![npm (scoped)](https://img.shields.io/npm/v/lethargy-ts?style=flat-square)](https://www.npmjs.com/package/lethargy-ts)
[![Bundle Size](https://img.shields.io/bundlephobia/min/lethargy-ts?style=flat-square)](https://bundlephobia.com/result?p=lethargy-ts)
![type definition](https://img.shields.io/npm/types/lethargy-ts)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/snelsi/lethargy-ts/blob/master/LICENSE)
### [Demo](http://d4nyll.github.io/lethargy/)
🌳 Tiny and Easy to use
### Install
🦄 Written in TypeScript
**npm**
🎏 Highly Customizable
npm install lethargy-ts
🏖 No external dependencies
**yarn**
## Install
yarn add lethargy-ts
```ssh
yarn add lethargy-ts
```
### Use
or
Include `lethargy.min.js` in your document.
```ssh
npm install --save lethargy-ts
```
<script src="./lethargy.js"></script>
## Usage
Create an instance of Lethargy. You may pass in options (see below), but usually the default is good enough.
Import and create an instance of `Lethargy`. It will remember previously checked `wheelEvents` to help to determine if they are inertial or not:
var lethargy = new Lethargy(); // Use defaults
var lethargy = new Lethargy(7, 100, 0.05); // Tinkering with the options
```tsx
import { Lethargy } from "lethargy-ts";
> If you found optimizations for the defaults, please share it in this [ticket](https://github.com/d4nyll/lethargy/issues/2)!
const lethargy = new Lethargy();
```
Bind the mousewheel or scroll events and pass the event to Lethargy.
You can customize parameters to better match your application's needs:
$(window).bind('mousewheel DOMMouseScroll wheel MozMousePixelScroll', function(e){
e.preventDefault()
e.stopPropagation();
if(lethargy.check(e) !== false) {
// Do something with the scroll event
}
});
```tsx
const lethargy = new Lethargy({
stability: 8,
sensitivity: 100,
tolerance: 0.1,
delay: 150,
});
```
`lethargy.check(e)` will return `1` if it is a normal scroll _up_ event, `-1` if it is a normal scroll _down_ event, and `false` if it is initiated by inertial scrolling.
> 😉 If you found optimizations for the defaults, please share them in this [ticket](https://github.com/d4nyll/lethargy/issues/2)!
Lethargy focus on preventing false positives (saying it's a normal scroll event when it wasn't), but tolerates false negatives (saying it's not a normal scroll event when it is).
Bind the wheel event and pass the event to `Lethargy`:
#### Webpack
```tsx
const checkWheelEvent = (e: WheelEvent) => {
const isIntentional = lethargy.check(e);
If you are using Webpack, you can use the [`exports-loader`](https://www.npmjs.com/package/exports-loader) to require the `Lethargy` constructor.
if (isIntentional) {
// Do something with the scroll event
}
};
window.addEventListener("wheel", checkWheelEvent, { passive: true });
```
$ yarn add exports-loader
```
Then, to import Lethargy:
`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.
```
const Lethargy = require("exports-loader?this.Lethargy!lethargy/lethargy");
```
`Lethargy` focus on preventing false positives (saying it's a normal scroll event when it wasn't), but tolerates false negatives (saying it's not a normal scroll event when it is).
### Options
## Options
All options are optional.
All options are optional:
![MacBook Air Trackpad `wheelDelta` curve](http://blog.danyll.com/content/images/2015/05/air.png)
- `stability` - Specifies the length of the rolling average. In effect, the larger the value, the smoother the curve will be. This attempts to prevent anomalies from firing 'real' events. Valid values are all positive integers, but in most cases, you would need to stay between `5` and around `30`.
**stability** - Specifies the length of the rolling average. In effect, the larger the value, the smoother the curve will be. This attempts to prevent anomalies from firing 'real' events. Valid values are all positive integers, but in most cases, you would need to stay between `5` and around `30`.
- `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. The unofficial standard `wheelDelta` is `120`, so valid values are positive integers below `120`.
**sensitivity** - Specifies the minimum value for `wheelDelta` for it to register as a valid scroll event. Because the tail of the curve have low `wheelDelta` values, this will stop them from registering as valid scroll events. The unofficial standard `wheelDelta` is `120`, so valid values are positive integers below `120`.
- `tolerance` - Prevent small fluctuations from affecting results. Valid values are decimals from `0`, but should ideally be between `0.05` and `0.3`.
**tolerance** - Prevent small fluctuations from affecting results. Valid values are decimals from `0`, but should ideally be between `0.05` and `0.3`.
- `delay` - Threshold for the amount of time between mouse wheel events for them to be deemed separate.
### What problem does it solve?
## What problem does it solve?
Scroll plugins such as [smartscroll](https://github.com/d4nyll/smartscroll), [jquery-mousewheel](https://github.com/jquery/jquery-mousewheel) or [fullPage.js](http://alvarotrigo.com/fullPage/) work by detecting scroll events and then doing something with them, such as scroll 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 two to three frames when the user only scrolled once.
Scroll plugins such as [smartscroll](https://github.com/d4nyll/smartscroll), [jquery-mousewheel](https://github.com/jquery/jquery-mousewheel), or [fullPage.js](http://alvarotrigo.com/fullPage/) work by detecting scroll events and then doing something with them, such as scroll 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 two to three frames when the user only scrolled once.
Below charts the `wheelDelta` values of each scroll action using this [Gist](https://gist.github.com/msimpson/cd7eca7907132c984171) and [demo](http://jsfiddle.net/n7bk6pb9/1/) by [Matthew Simpson](https://github.com/msimpson).
### How does it work?
**Desktop Mouse**
Lethargy keeps a record of the last few `wheelDelta` values that are passed through it, it will then work out whether these values are decreasing (decaying), and if so, concludes that the scroll event originated from inertial scrolling, and not directly from the user.
![Desktop Mouse `wheelDelta` graph](http://blog.danyll.com/content/images/2015/05/desktop.png)
### Limitations
**MacBook Air Trackpad**
Not all trackpads work the same, some trackpads do not have a decaying `wheelDelta` value, so our method of decay detection would not work. Instead, to cater to this situation, we had to, grudgingly, set a very small time delay between when events will register. We have tested this and normal use does not affect user experience more than usual.
![MacBook Air Trackpad `wheelDelta` curve](http://blog.danyll.com/content/images/2015/05/air.png)
## TypeScript
### How does it work?
The module is written in TypeScript and type definitions are included.
Lethargy keeps a record of the last few `wheelDelta` values that is passed through it, it will then work out whether these values are decreasing (decaying), and if so, concludes that the scroll event originated from inertial scrolling, and not directly from the user.
## Contributing
### Limitations
Contributions, issues, and feature requests are welcome!
Not all trackpads work the same, some trackpads do not have a decaying `wheelDelta` value, and so our method of decay detection would not work. Instead, to cater for this situation, we had to, grudgingly, set a very small time delay between when events will register. We have tested this and for normal use does not affect user experience more than usual.
## Show your support
**ASUS Trackpad**
Give a ⭐️ if you like this project!
![ASUS Trackpad](http://blog.danyll.com/content/images/2015/05/asus.png)
## LICENSE
## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fd4nyll%2Flethargy.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fd4nyll%2Flethargy?ref=badge_large)
[MIT](./LICENSE)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc