Skip to content

Commit 75c96e4

Browse files
committed
add animation api to docs
1 parent e4b00b7 commit 75c96e4

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ $ npm install blobs
2727
import * as blobs2 from "blobs/v2";
2828
```
2929

30+
```ts
31+
import * as blobs2Animate from "blobs/v2/animate";
32+
```
33+
3034
<p align="center">
3135
OR
3236
</p>
@@ -35,6 +39,10 @@ import * as blobs2 from "blobs/v2";
3539
<script src="https://unpkg.com/blobs/v2"></script>
3640
```
3741

42+
```html
43+
<script src="https://unpkg.com/blobs/v2/animate"></script>
44+
```
45+
3846
## SVG Path
3947

4048
```js
@@ -84,8 +92,42 @@ const path = blobs2.canvasPath(
8492
ctx.stroke(path);
8593
```
8694

95+
## Canvas Animation
96+
97+
```js
98+
const ctx = /* ... */;
99+
const animation = blobs2Animate.canvasPath();
100+
101+
// Set up "requestAnimationFrame" rendering loop.
102+
const renderAnimation = () => {
103+
ctx.clearRect(0, 0, size, size);
104+
ctx.fill(animation.renderFrame());
105+
requestAnimationFrame(renderAnimation);
106+
};
107+
requestAnimationFrame(renderAnimation);
108+
109+
// Initial frame.
110+
animation.transition({
111+
duration: 0, // Render immediately.
112+
callback: loopAnimation,
113+
blobOptions: {...},
114+
});
115+
116+
// Loop between random blobs every 4s.
117+
const loopAnimation = () => {
118+
animation.transition({
119+
duration: 4000,
120+
timingFunction: "ease",
121+
callback: loopAnimation,
122+
blobOptions: {...},
123+
});
124+
};
125+
```
126+
87127
## Complete API
88128

129+
### `"blobs/v2"`
130+
89131
```ts
90132
export interface BlobOptions {
91133
// A given seed will always produce the same blob.
@@ -113,6 +155,52 @@ export const svg: (blobOptions: BlobOptions, svgOptions?: SvgOptions) => string;
113155
export const svgPath: (blobOptions: BlobOptions) => string;
114156
```
115157

158+
### `"blobs/v2/animate"`
159+
160+
```ts
161+
export interface CanvasKeyframe {
162+
// Duration of the keyframe animation in milliseconds.
163+
duration: number;
164+
// Delay before animation begins in milliseconds.
165+
// Default: 0.
166+
delay?: number;
167+
// Controls the speed of the animation over time.
168+
// Default: "linear".
169+
timingFunction?:
170+
| "linear"
171+
| "easeEnd"
172+
| "easeStart"
173+
| "ease"
174+
| "elasticEnd0"
175+
| "elasticEnd1"
176+
| "elasticEnd2"
177+
| "elasticEnd3";
178+
// Called after keyframe end-state is reached or passed.
179+
// Called exactly once when the keyframe end-state is rendered.
180+
// Not called if the keyframe is preempted by a new transition.
181+
callback?: () => void;
182+
// Standard options, refer to "blobs/v2" documentation.
183+
blobOptions: {
184+
seed: number | string;
185+
randomness: number;
186+
extraPoints: number;
187+
size: number;
188+
};
189+
// Standard options, refer to "blobs/v2" documentation.
190+
canvasOptions?: {
191+
offsetX?: number;
192+
offsetY?: number;
193+
};
194+
}
195+
export const canvasPath: () => {
196+
// Renders the current state of the animation.
197+
renderFrame: () => Path2D;
198+
// Immediately begin animating through the given keyframes.
199+
// Non-rendered keyframes from previous transitions are cancelled.
200+
transition: (...keyframes: CanvasKeyframe[]) => void;
201+
};
202+
```
203+
116204
## License
117205

118206
[MIT](./LICENSE)

0 commit comments

Comments
 (0)