| 44 | * CSS animation player/processor. |
| 45 | */ |
| 46 | export class Animation { |
| 47 | private renderer: Renderer2; |
| 48 | |
| 49 | /** Parsed rules. Time is in milliseconds. */ |
| 50 | private rules: AnimationRule<ParsedStyles>[] = []; |
| 51 | private config: AnimationConfig; |
| 52 | private currentTime: number = 0; |
| 53 | private allObjects = new Map<string, Element | Element[]>(); // selector; element(s) |
| 54 | private activeStyles = new Map<string, ParsedStyles>(); // selector; ParsedStyles |
| 55 | private animationFrameId: number | null = null; |
| 56 | private completed: boolean = false; |
| 57 | private plugins: AnimationPlugin[] = []; |
| 58 | private _duration: number = 0; |
| 59 | private readonly _isPlaying = signal<boolean>(false); |
| 60 | private readonly _progress = signal<number>(0); |
| 61 | |
| 62 | /** Returns whether the animation is playing or not */ |
| 63 | isPlaying = this._isPlaying.asReadonly(); |
| 64 | |
| 65 | /** Returns the animation progress (`[0,1]`) */ |
| 66 | progress = this._progress.asReadonly(); |
| 67 | |
| 68 | constructor( |
| 69 | layers: readonly AnimationLayerDirective[], |
| 70 | injector: Injector, |
| 71 | config?: Partial<AnimationConfig>, |
| 72 | ) { |
| 73 | this.renderer = injector.get(RendererFactory2).createRenderer(null, null); |
| 74 | this.config = {...DEFAULT_CONFIG, ...(config || {})}; |
| 75 | |
| 76 | // Set layer elements in the objects map. |
| 77 | this.allObjects = new Map(layers.map((f) => [f.id(), f.elementRef.nativeElement])); |
| 78 | } |
| 79 | |
| 80 | /** Animation duration. In milliseconds */ |
| 81 | get duration() { |
| 82 | return this._duration; |
| 83 | } |
| 84 | |
| 85 | /** Animation timestep (config). In milliseconds */ |
| 86 | get timestep() { |
| 87 | return this.config.timestep; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Define the animation. |
| 92 | * |
| 93 | * @param definition Definition (i.e. `AnimationRule` array) |
| 94 | * @returns The animation |
| 95 | */ |
| 96 | define(definition: AnimationDefinition) { |
| 97 | this.reset(); |
| 98 | this.extractObjectsAndValidateRules(definition); |
| 99 | |
| 100 | // Parse the rules. |
| 101 | // IMPORTANT: Parsed rules use milliseconds instead of seconds. |
| 102 | this.rules = definition |
| 103 | .sort((a, b) => getStartTime(a) - getStartTime(b)) |
nothing calls this directly
no test coverage detected
searching dependent graphs…