|
| 1 | +import { |
| 2 | + animate, |
| 3 | + animateChild, |
| 4 | + animation, |
| 5 | + AnimationTriggerMetadata, |
| 6 | + AUTO_STYLE, |
| 7 | + group, |
| 8 | + keyframes, |
| 9 | + query, |
| 10 | + style, |
| 11 | + transition, |
| 12 | + trigger, |
| 13 | + useAnimation |
| 14 | +} from '@angular/animations'; |
| 15 | + |
| 16 | +import { IAttentionSeekerAnimationOptions } from '../common/interfaces'; |
| 17 | + |
| 18 | +export interface IHeartBeatAnimationOptions extends IAttentionSeekerAnimationOptions { |
| 19 | + /** |
| 20 | + * Scale factor |
| 21 | + * |
| 22 | + * Default: 1.3 |
| 23 | + */ |
| 24 | + scale?: number; |
| 25 | +} |
| 26 | + |
| 27 | +const heartBeat = animation([ |
| 28 | + animate( |
| 29 | + '{{duration}}ms {{delay}}ms', |
| 30 | + keyframes([ |
| 31 | + style({ visibility: AUTO_STYLE, transform: 'scale(1)', easing: 'ease-in-out', offset: 0 }), |
| 32 | + style({ transform: 'scale({{scale}})', easing: 'ease-in-out', offset: 0.14 }), |
| 33 | + style({ transform: 'scale(1)', easing: 'ease-in-out', offset: 0.28 }), |
| 34 | + style({ transform: 'scale({{scale}})', easing: 'ease-in-out', offset: 0.42 }), |
| 35 | + style({ transform: 'scale(1)', easing: 'ease-in-out', offset: 0.7 }) |
| 36 | + ]) |
| 37 | + ) |
| 38 | +]); |
| 39 | + |
| 40 | +const DEFAULT_DURATION = 1300; |
| 41 | +const DEFAULT_SCALE = 1.3; |
| 42 | + |
| 43 | +export function heartBeatAnimation(options?: IHeartBeatAnimationOptions): AnimationTriggerMetadata { |
| 44 | + return trigger((options && options.anchor) || 'heartBeat', [ |
| 45 | + transition( |
| 46 | + `0 ${(options && options.direction) || '<=>'} 1`, |
| 47 | + [ |
| 48 | + ...(options && options.animateChildren === 'before' ? [query('@*', animateChild(), { optional: true })] : []), |
| 49 | + group([ |
| 50 | + useAnimation(heartBeat), |
| 51 | + ...(!options || !options.animateChildren || options.animateChildren === 'together' |
| 52 | + ? [query('@*', animateChild(), { optional: true })] |
| 53 | + : []) |
| 54 | + ]), |
| 55 | + ...(options && options.animateChildren === 'after' ? [query('@*', animateChild(), { optional: true })] : []) |
| 56 | + ], |
| 57 | + { |
| 58 | + params: { |
| 59 | + delay: (options && options.delay) || 0, |
| 60 | + duration: (options && options.duration) || DEFAULT_DURATION, |
| 61 | + scale: (options && options.scale) || DEFAULT_SCALE |
| 62 | + } |
| 63 | + } |
| 64 | + ) |
| 65 | + ]); |
| 66 | +} |
| 67 | + |
| 68 | +export function heartBeatOnEnterAnimation(options?: IHeartBeatAnimationOptions): AnimationTriggerMetadata { |
| 69 | + return trigger((options && options.anchor) || 'heartBeatOnEnter', [ |
| 70 | + transition( |
| 71 | + ':enter', |
| 72 | + [ |
| 73 | + ...(options && options.animateChildren === 'before' ? [query('@*', animateChild(), { optional: true })] : []), |
| 74 | + style({ visibility: 'hidden' }), |
| 75 | + group([ |
| 76 | + useAnimation(heartBeat), |
| 77 | + ...(!options || !options.animateChildren || options.animateChildren === 'together' |
| 78 | + ? [query('@*', animateChild(), { optional: true })] |
| 79 | + : []) |
| 80 | + ]), |
| 81 | + ...(options && options.animateChildren === 'after' ? [query('@*', animateChild(), { optional: true })] : []) |
| 82 | + ], |
| 83 | + { |
| 84 | + params: { |
| 85 | + delay: (options && options.delay) || 0, |
| 86 | + duration: (options && options.duration) || DEFAULT_DURATION, |
| 87 | + scale: (options && options.scale) || DEFAULT_SCALE |
| 88 | + } |
| 89 | + } |
| 90 | + ) |
| 91 | + ]); |
| 92 | +} |
0 commit comments