Skip to content

Commit

Permalink
Initial commit!
Browse files Browse the repository at this point in the history
  • Loading branch information
vaneenige committed Jan 9, 2019
0 parents commit 476318f
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
dist
node_modules
.DS_Store
package-lock.json
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Colin van Eenige

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
58 changes: 58 additions & 0 deletions README.md
@@ -0,0 +1,58 @@
# Update Over Time (uot)

[![npm version](https://img.shields.io/npm/v/uot.svg)](https://www.npmjs.com/package/uot)
[![gzip size](http://img.badgesize.io/https://unpkg.com/uot/dist/uot.mjs?compression=gzip)](https://unpkg.com/uot)
[![license](https://img.shields.io/npm/l/uot.svg)](https://github.com/vaneenige/uot/blob/master/LICENSE)
[![dependencies](https://img.shields.io/badge/dependencies-none-ff69b4.svg)](https://github.com/vaneenige/uot/blob/master/package.json)

Update Over Time (uot) is a **200b** library to provide the easiest way for updating values over time. Provide a callback and a duration you're ready to go!

This utility can be useful for CSS animations, DOM changes, WebGL transitions or anything that can be updated based on a progress value.

>It's basically a setTimeout with progress.
#### Features:

- Small in size, no dependencies
- Based on requestAnimationFrame
- Optimized for multiple instances

## Install

```
$ npm install --save uot
```

## Usage

Import the library:
```js
import updateOverTime from 'uot';
```

Provide a callback and a duration:
```js
updateOverTime((progress) => {
// Progress value: 0 ... 1
if (progress === 1) {
// Handle complete
}
}, 2000);
```

Use the progress value to update the DOM (or anything):
```js
updateOverTime((progress) => {
// Add easing to the progress value
progress = easeInOutQuad(progress);
// Output the progress value to the DOM
document.body.textContent = progress.toFixed(2);
}, 4000);
```

> At any given time only a single requestAnimationFrame will be called.
## License

MIT © <a href="https://use-the-platform.com">Colin van Eenige</a>

34 changes: 34 additions & 0 deletions package.json
@@ -0,0 +1,34 @@
{
"name": "uot",
"version": "1.0.0",
"description": "A tiny 200b timeout alternative with progress.",
"main": "dist/uot.mjs",
"unpkg": "dist/uot.umd.js",
"scripts": {
"start": "http-server demo --silent & $npm_execpath run watch",
"watch": "microbundle watch --format umd --entry demo/src/index.js --output demo/dist/bundle.js",
"build": "microbundle --name uot --format es,umd --sourcemap false",
"prepare": "$npm_execpath run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vaneenige/uot.git"
},
"author": {
"name": "Colin van Eenige",
"email": "cvaneenige@gmail.com",
"url": "https://use-the-platform.com"
},
"files": [
"src",
"dist"
],
"keywords": [
"timeout",
"progress"
],
"license": "MIT",
"devDependencies": {
"microbundle": "^0.9.0"
}
}
28 changes: 28 additions & 0 deletions src/index.js
@@ -0,0 +1,28 @@
const instances = [];
const raf = requestAnimationFrame;
const p = performance;

/**
*
*/
function tick() {
for (let i = instances.length - 1; i >= 0; i -= 1) {
const instance = instances[i];
const progress = (p.now() - instance[2]) / instance[1];
instance[0](progress <= 1 ? progress : 1);
if (progress >= 1) instances.splice(i, 1);
}
if (instances.length > 0) raf(tick);
}

/**
*
* @param {function} callback
* @param {number} duration
*/
function uot(callback, duration) {
instances.push([callback, duration, p.now()]);
if (instances.length < 2) raf(tick);
}

export default uot;

0 comments on commit 476318f

Please sign in to comment.