UNPKG

2.78 kBTypeScriptView Raw
1import { Observable } from '../../Observable';
2import { TimestampProvider } from "../../types";
3/**
4 * An observable of animation frames
5 *
6 * Emits the the amount of time elapsed since subscription and the timestamp on each animation frame.
7 * Defaults to milliseconds provided to the requestAnimationFrame's callback. Does not end on its own.
8 *
9 * Every subscription will start a separate animation loop. Since animation frames are always scheduled
10 * by the browser to occur directly before a repaint, scheduling more than one animation frame synchronously
11 * should not be much different or have more overhead than looping over an array of events during
12 * a single animation frame. However, if for some reason the developer would like to ensure the
13 * execution of animation-related handlers are all executed during the same task by the engine,
14 * the `share` operator can be used.
15 *
16 * This is useful for setting up animations with RxJS.
17 *
18 * ### Example
19 *
20 * Tweening a div to move it on the screen
21 *
22 * ```ts
23 * import { animationFrames } from 'rxjs';
24 * import { map, takeWhile, endWith } from 'rxjs/operators';
25 *
26 * function tween(start: number, end: number, duration: number) {
27 * const diff = end - start;
28 * return animationFrames().pipe(
29 * // Figure out what percentage of time has passed
30 * map(({elapsed}) => elapsed / duration),
31 * // Take the vector while less than 100%
32 * takeWhile(v => v < 1),
33 * // Finish with 100%
34 * endWith(1),
35 * // Calculate the distance traveled between start and end
36 * map(v => v * diff + start)
37 * );
38 * }
39 *
40 * // Setup a div for us to move around
41 * const div = document.createElement('div');
42 * document.body.appendChild(div);
43 * div.style.position = 'absolute';
44 * div.style.width = '40px';
45 * div.style.height = '40px';
46 * div.style.backgroundColor = 'lime';
47 * div.style.transform = 'translate3d(10px, 0, 0)';
48 *
49 * tween(10, 200, 4000).subscribe(x => {
50 * div.style.transform = `translate3d(${x}px, 0, 0)`;
51 * });
52 * ```
53 *
54 * ### Example
55 *
56 * Providing a custom timestamp provider
57 *
58 * ```ts
59 * import { animationFrames, TimestampProvider } from 'rxjs';
60 *
61 * // A custom timestamp provider
62 * let now = 0;
63 * const customTSProvider: TimestampProvider = {
64 * now() { return now++; }
65 * };
66 *
67 * const source$ = animationFrames(customTSProvider);
68 *
69 * // Log increasing numbers 0...1...2... on every animation frame.
70 * source$.subscribe(({ elapsed }) => console.log(elapsed));
71 * ```
72 *
73 * @param timestampProvider An object with a `now` method that provides a numeric timestamp
74 */
75export declare function animationFrames(timestampProvider?: TimestampProvider): Observable<{
76 timestamp: number;
77 elapsed: number;
78}>;
79//# sourceMappingURL=animationFrames.d.ts.map
\No newline at end of file