|
1 | 1 | # tween-here
|
2 |
| -The animation library to use when you don't want to bother about animations. |
| 2 | + |
| 3 | +The animation library to use when you don't really care about animations. |
| 4 | + |
| 5 | +------ |
| 6 | + |
| 7 | +### Install |
| 8 | + |
| 9 | +`npm install --save @pinyin/tween-here` |
| 10 | + |
| 11 | +It should support typescript out of the box. If not, please submit an issue. |
| 12 | + |
| 13 | +### Usage |
| 14 | + |
| 15 | +```typescript jsx |
| 16 | +import {tweenHere, tweenExit, getTweenState} from '@pinyin/tween-here' |
| 17 | + |
| 18 | +// Get a reference to the animation target |
| 19 | +// For React, you may want to use refs(https://reactjs.org/docs/refs-and-the-dom.html) |
| 20 | +const element: Element = document.getElementById("") |
| 21 | + |
| 22 | +// Make it fade in smoothly |
| 23 | +// For React, this line may be placed in componentDidMount() |
| 24 | +tweenHere(element, snapshot=> ({...snapshot, opacity: 0})) |
| 25 | + |
| 26 | +// Tweening from one place to another |
| 27 | +// 1. snapshot current position before element is moved |
| 28 | +// For React, this line may be in componentWillUpdate() or getSnapshotBeforeUpdate() |
| 29 | +const snapshot = getTweenState(element) |
| 30 | +// 2. when element is moved, call tweenHere on the snapshot |
| 31 | +tweenHere(element, snapshot) |
| 32 | + |
| 33 | +// When this element is detached from dom, make it fade out instead of suddenly disappear. |
| 34 | +// For React, this may appear in componentWillUpdate(), getSnapshotBeforeUpdate() or componentWillUnmount() |
| 35 | +// Yes, you can specify a component's unmount animation inside the component itself. |
| 36 | +tweenExit(element, snapshot=>({...snapshot, opacity: 0})) |
| 37 | + |
| 38 | +``` |
| 39 | + |
| 40 | +Demo is planned. For now, please refer to this [InfiniteList component demo](http://pinyin.github.io/InfiniteMasonry/InfiniteMasonry.html) to see this library in action. |
| 41 | + |
| 42 | +All animations in the current demo are implemented with this library. |
| 43 | + |
| 44 | +### Design Targets |
| 45 | + |
| 46 | +[Motions are important](https://material.io/guidelines/motion/material-motion.html#material-motion-why-does-motion-matter). |
| 47 | + |
| 48 | +But they are hard to implement. |
| 49 | + |
| 50 | +We've had many web animation solutions that are both precise and powerful, like [Popmotion](https://popmotion.io/) and [Web Animations API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API) and may other awesome ones, but sometimes, even these precise solutions seem to be a little too much for the simple use case. |
| 51 | + |
| 52 | +> Just make this element appear smoothly, please. It should be simple. |
| 53 | +
|
| 54 | +That's what `tween-here` is designed for. |
| 55 | + |
| 56 | + |
| 57 | +### APIs |
| 58 | + |
| 59 | +The document is not complete yet. Any contributions are welcome. |
| 60 | + |
| 61 | +`tween-here` comes with two functions, `tweenHere` and `tweenExit`, each function provides a fast way to implement a kind of motions. |
| 62 | + |
| 63 | +```typescript jsx |
| 64 | +async function tweenHere( |
| 65 | + element: HTMLElement, |
| 66 | + from: Maybe<TweenState> | ((snapshot: TweenState, to: TweenState) => Maybe<TweenState>) = nothing, |
| 67 | + duration: ms | ((from: TweenState, to: TweenState) => ms) = 200, |
| 68 | + easing: CubicBezierParam = [0, 0, 1, 1] |
| 69 | +): Promise<void> |
| 70 | + |
| 71 | +async function tweenExit( |
| 72 | + element: HTMLElement, |
| 73 | + to: Maybe<TweenState> | ((from: TweenState) => Maybe<TweenState>) = nothing, |
| 74 | + duration: ms | ((from: TweenState, to: TweenState) => ms) = 200, |
| 75 | + easing: CubicBezierParam = [0, 0, 1, 1] |
| 76 | +): Promise<void> |
| 77 | +``` |
| 78 | + |
| 79 | +In general, use `tweenHere` when you want a element to move smoothly, use `tweenExit` when you know an element is being detached from `document` and what it to disappear smoothly. |
| 80 | + |
| 81 | +TweenState is an object representing the position of an element (relative to viewport): |
| 82 | +```typescript jsx |
| 83 | +type TweenState = { |
| 84 | + x: number |
| 85 | + y: number |
| 86 | + width: number |
| 87 | + height: number |
| 88 | + opacity: number |
| 89 | +} |
| 90 | +``` |
| 91 | +In practise, you may get these numbers from `getBoundingClientRect()` and many other native APIs. |
| 92 | + |
| 93 | +For convenience, this library provides two helper functions, `getTweenState` and `getOriginalTweenState`, to capture the `TweenState` from an existing element. |
| 94 | + |
| 95 | +```typescript jsx |
| 96 | +getTweenState(element: HTMLElement): TweenState |
| 97 | +getOriginalTweenState(element: HTMLElement): TweenState |
| 98 | +``` |
| 99 | + |
| 100 | +By using these helper functions and `tweenHere`, you can easily make an element tween smoothly from the position of another element, constructing an visual effect of they are the same element. |
| 101 | + |
| 102 | + |
| 103 | +### Notice |
| 104 | + |
| 105 | +This library is very unstable yet, any contributions(code, issue, use cases proposal...) are welcome. |
| 106 | + |
| 107 | +### License |
| 108 | + |
| 109 | +MIT |
| 110 | + |
| 111 | + |
0 commit comments