-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 476318f
Showing
5 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
node_modules | ||
.DS_Store | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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](https://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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[email protected]", | ||
"url": "https://use-the-platform.com" | ||
}, | ||
"files": [ | ||
"src", | ||
"dist" | ||
], | ||
"keywords": [ | ||
"timeout", | ||
"progress" | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
"microbundle": "^0.9.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |