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

inertia-state

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inertia-state

Minimum state manager for moment of inertia.

  • 1.1.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
20
decreased by-13.04%
Maintainers
1
Weekly downloads
 
Created
Source

inertia-state

Minimum state manager for moment of inertia.

  • 0 dependencies
  • Platform agnostic
  • About 100 lines of code

Install

npm i inertia-state
import { InertiaState } from "inertia-state"

or

<script src="https://unpkg.com/inertia-state/dist/inertia-state.umd.js"></script>
<script>
  const { InertiaState } = window.inertiaState;
</script>

Options

new InertiaState({
  coefficient: 0.5,
  minTimeToMove: 15,
});
namedescriptionrequireddefault
coefficientFriction coefficient.false0.85
minTimeToMoveThe minimum time(sub-ms) from start > stop, determined that movement has occurred.false10

Example

See https://leader22.github.io/inertia-state/ to check how it works.

//
// Usual drag implementation
//
const state = {
  position: [100, 200],
  dragging: false,
};

const $box = document.getElementById("box");
const rect = $box.getBoundingClientRect();

$box.addEventListener("pointerdown", ({ clientX, clientY }) => {
  state.dragging = true;
  state.position[0] = clientX - rect.width / 2;
  state.position[1] = clientY - rect.height / 2;
});
$box.addEventListener("pointermove", ({ clientX, clientY }) => {
  if (!state.dragging) return;
  state.position[0] = clientX - rect.width / 2;
  state.position[1] = clientY - rect.height / 2;
});
$box.addEventListener("pointerup", () => {
  state.dragging = false;
});

(function render() {
  requestAnimationFrame(render);

  const [x, y] = state.position;
  $box.style.transform = `translate3d(${x}px, ${y}px, 0)`;
})();

//
// + Inertia movement 🙌
//
const iState = new InertiaState();

$box.addEventListener("pointerdown", ({ clientX, clientY }) => {
  iState.start([clientX, clientY]);
});
$box.addEventListener("pointermove", ({ clientX, clientY }) => {
  iState.update([clientX, clientY]);
});
$box.addEventListener("pointerup", () => {
  iState.stop();
});

iState.addCallback((ev) => {
  state.position[0] += ev.delta[0];
  state.position[1] += ev.delta[1];
});

Usage w/ react-use-gesture

import { useRef, useEffect } from "react";
import { useDrag } from "react-use-gesture";
import { InertiaState } from "inertia-state";

const inertiaState = useRef(new InertiaState());

useDrag((state) => {
  // handle drag...

  // 1️⃣ update inertia state
  if (state.first) inertiaState.current.start(state.movement);
  inertiaState.current.update(state.movement);
  if (state.last) inertiaState.current.stop();
});

useEffect(() => {
  const iState = inertiaState.current;
  const dispose = iState.addCallback(({ delta, movement }) =>
    // 2️⃣ apply inertia here
  );

  return () => {
    dispose();
    iState.reset();
  };
}, [inertiaState]);

Thanks

Keywords

FAQs

Package last updated on 31 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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