• Stars
    star
    122
  • Rank 291,103 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ridiculously small animation library.

nm8

Ridiculously small animation library. Less than 260 bytes. Fits in a tweet.

Usage

<script src="https://unpkg.com/nm8"></script>
import nm8 from 'nm8';

const output = value => console.log(value);

// create animation
const animation = nm8(output, 1000);

// play animation
animation.play();

// pause animation
animation.pause();

// stop animation
animation.stop();

API

nm8(onTick: (value: number) => void, duration?: number)

Creates an animation that calls onTick with the current:

  • offset (between 0 and 1) if duration is specified
  • delta (in ms) if no duration is specified. Usually 16 or 17.

animation.play()

Starts playing the animation, because the animation doesn't just fire off immediately. That's irresponsible.

animation.pause()

Pauses the animation. The onTick handler won't be called again until .play() or .stop() is called.

animation.stop()

Stops the animation. The onTick handler will be called with the end value (either 1 if duration is specified or Infinity otherwise). Calling .play() on a stopped animation will restart it.

FAQs

How do I actually make stuff move?

const ball = document.querySelector('#ball');

const animation = nm8(offset => {
  // moves ball from 0 to 1000px over 1 second
  ball.style.transform = `translateX(${offset * 1000}px)`
}, 1000).play();

What about easing?

// use sine easing, it's really nice
const easeSine = fn => offset => fn((1 - Math.cos(offset * Math.PI)) / 2);

const animation = nm8(easeSine(offset => {
  ball.style.transform = `translateX(${offset * 1000}px)`
}), 1000).play();

What about delays?

// just copy-paste this. it's math
const delayNm8 = (fn, duration, delay) => nm8(delayOffset => {
  const offset = Math.max(delayOffset - delay / (duration + delay), 0) * (duration + delay) / duration;
  fn(offset);
}, duration + delay);

// 1-second ease animation with 2-second delay
const animation = delayNm8(easeSine(offset => {
  ball.style.transform = `translateX(${offset * 1000}px)`
}), 1000, 2000).play();

I want more easing functions.

Here's a bunch: https://github.com/just-animate/just-curves

Can I request a feature?

No. Use https://github.com/tweenrex/tweenrex if you want a small tweening library with more features.

What is the browser support?

Just avoid IE, okay?

Why is it so small?

So you can copy-paste it:

function nm8(a,b){let c,d,e,f=(g)=>{let h=+!c||-(d||g)+(d=g);a(b?Math.min(Math.max((e+=h)/b,0),1):h);return!c||e>=b||requestAnimationFrame(f)},g={play:()=>(c=1,b&&e<=b||(e=0),f(performance.now()),g),pause:()=>(c=0,g),stop:()=>(e=d=c=0,g)};return g}

Have examples? Check out the examples directory: https://github.com/davidkpiano/nm8/blob/master/examples

More Repositories

1

react-redux-form

Create forms easily in React with Redux.
JavaScript
2,066
star
2

flipping

Flipping awesome animations.
TypeScript
1,380
star
3

useEffectReducer

useReducer + useEffect = useEffectReducer
TypeScript
791
star
4

RxCSS

JavaScript
416
star
5

sassdash

The Sass implementation of lodash.
SCSS
386
star
6

sass-svg

Inline SVG for Sass.
CSS
314
star
7

frontend-masters-xstate-workshop

Frontend Masters State Machines & XState Workshop
JavaScript
243
star
8

frontend-masters-css

SCSS
169
star
9

sassport

Sass with JavaScript superpowers.
JavaScript
147
star
10

frontend-masters-react-workshop

Code for the Frontend Masters React State Modeling Workshop
JavaScript
128
star
11

frontend-masters-xstate-v2

Frontend Masters XState Workshop v2
JavaScript
121
star
12

react-simple-datepicker

Simple datepicker for React.
JavaScript
119
star
13

xstate-test-demo

Demo React project for model-based testing with @xstate/test
JavaScript
111
star
14

durable-entities-xstate

XState + Durable Entities = 🚀
TypeScript
53
star
15

xstate-react-workshop

React Finland - Modeling React Applications with Statecharts Workshop
JavaScript
24
star
16

weatherboard

TypeScript
23
star
17

xviz

Small graph visualization library
TypeScript
19
star
18

propelcss

CSS
19
star
19

postcss-emoji-style

JavaScript
19
star
20

xstate-todomvc

Created with CodeSandbox
JavaScript
18
star
21

xstate-examples

Example XState Projects
17
star
22

xstate-command

Command palette with XState
TypeScript
12
star
23

react-finland-xstate-2022

React Finland 2022 XState Workshop
JavaScript
11
star
24

mbt-workshop

Model-Based Testing Workshop
JavaScript
11
star
25

react-finland-xstate-workshop

React Finland XState Workshop
JavaScript
11
star
26

sass-v

CSS Custom Properties + Sass = 🚀
SCSS
10
star
27

RxAnimate

Reactive Animations
TypeScript
10
star
28

openapi-test

OpenAPI Testing Library
TypeScript
10
star
29

node-azure-mvc

Example application for creating an MVC Express + Node + TypeScript app and deploying it to Azure
TypeScript
10
star
30

redux-test-store

Test existing stores in Redux, the easy way
JavaScript
8
star
31

skylake

💎 A light JavaScript library.
JavaScript
8
star
32

xstate-interpreter

Reactive statechart interpreter.
TypeScript
7
star
33

paz

Peaceful Azure deployments with node
6
star
34

movement

Sass/SCSS/CSS animation framework for creating, composing, sequencing, and using animations.
CSS
6
star
35

davidkpiano

5
star
36

getrect

TypeScript
5
star
37

rxio

TypeScript
4
star
38

xstate-tetris

TypeScript
4
star
39

issuspensereadyyet

...
JavaScript
4
star
40

tracker

Track DOM elements efficiently.
4
star
41

netlify-edge-machine

TypeScript
4
star
42

neo4j-workshop

Neo4j Statecharts Workshop
JavaScript
3
star
43

rxform

TypeScript
3
star
44

gulp-sassport

Sassport gulp plugin
JavaScript
3
star
45

sassport-reference

Import by Reference in Sass using Sassport
JavaScript
3
star
46

vanilla-todomvc-ts

TypeScript
3
star
47

cadenza-old

SCSS/CSS Framework
CSS
2
star
48

movi

Stately animations
2
star
49

cse-articles

Microsoft CSE Articles
2
star
50

rxstyle

Animation framework for RxJS
JavaScript
2
star
51

sassport-svgo

Inline, optimized SVG inside your Sass files.
JavaScript
2
star
52

sassport-dynamic

Dynamic imports for Node-Sass using Sassport
JavaScript
2
star
53

rrf-docs

CSS
2
star
54

react-redux-form-builder

React Redux Form Builder
JavaScript
2
star
55

webanimate

TypeScript
2
star
56

stately-site

HTML
2
star
57

cz-temp

CSS
2
star
58

angular-fluid

Form Logic User Interface Directive - Form Logic implementation for AngularJS
CSS
2
star
59

orlandodevs-site

HTML
1
star
60

react-redux-form-docs

1
star
61

styledash

JavaScript
1
star
62

react-state-machine

Finite State Machines for React.
1
star
63

logicapp-specflow-demo

C#
1
star
64

generator-sass-guide

Yeoman generator for Sass Guidelines
JavaScript
1
star
65

sassport-math

Math for Sass.
JavaScript
1
star
66

emojio

1
star
67

goatspresso

Goats and Espresso
JavaScript
1
star
68

test-gen-demo

TypeScript
1
star
69

react-redux-form-book

1
star
70

postcss-movement

1
star
71

rxy

For Atomic Hack Party
PHP
1
star
72

movt

Movement
JavaScript
1
star
73

react-dynamic

Dynamic.js Animation Library for React
1
star
74

styleguides

1
star
75

react-move

Nothing to see here
JavaScript
1
star
76

cadenza

1
star
77

sass-z

Z-index management for Sass.
1
star
78

eyeglass-math

Math. For Sass.
JavaScript
1
star