Performance: Keyframe animations
Keyframe animations affect the smoothness of visual transitions and can impact the browser's paint and composite stages, influencing user experience during animations.
Jump into concepts and practice - no test required
import { animate, keyframes, style, transition, trigger } from '@angular/animations'; export const slideAnimation = trigger('slide', [ transition(':enter', [ animate('500ms ease-in', keyframes([ style({ transform: 'translateX(0)', offset: 0 }), style({ transform: 'translateX(100px)', offset: 1 }) ])) ]) ]);
import { animate, keyframes, style, transition, trigger } from '@angular/animations'; export const slideAnimation = trigger('slide', [ transition(':enter', [ animate('500ms ease-in', keyframes([ style({ left: '0px', offset: 0 }), style({ left: '100px', offset: 1 }) ])) ]) ]);
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Animating 'left' property | Multiple style updates | Triggers reflow every frame | High paint cost due to layout changes | [X] Bad |
| Animating 'transform' property | Style updates only | No reflows triggered | Low paint cost, uses compositor | [OK] Good |
keyframe animations in Angular?animate function to control detailed animation steps.keyframes and animate are imported from '@angular/animations'.keyframes and animate, imported with curly braces from '@angular/animations'.animate('2s', keyframes([
style({ backgroundColor: 'red', offset: 0 }),
style({ backgroundColor: 'blue', offset: 0.5 }),
style({ backgroundColor: 'green', offset: 1 })
]))trigger('fadeIn', [
transition(':enter', [
animate('1s', keyframes([
style({ opacity: 0, offset: 0 }),
style({ opacity: 1 })
]))
])
])offset between 0 and 1 to define timing precisely.offset property, which is required for keyframes to work correctly.keyframes arrays correctly implements this?offset between 0 and 1 to define animation timing precisely.