0.1.3 • Published 6 years ago

angular-animations-library v0.1.3

Weekly downloads
12
License
MIT
Repository
-
Last release
6 years ago

angular-animations-library

An angular library which provides several animations, highly customisable, ready to be used inside the animations metadata block of your components.

If you like the project, and if you would like that this will be updated and maintained adding new animations and features, please leave a star on the github repository.


Table Of Contents


Installation

In order to install the module execute the following command:

npm install --save angular-animations-library

and then inside your application import the AngularAnimationsLibraryModule including the following code:

...
import { AngularAnimationsLibraryModule } from 'angular-animations-library';
...
@NgModule({
  ...
  imports: [..., AngularAnimationsLibraryModule],
  ...
})
export class AppModule {}

Animations

All the animations can be used by calling the corresponding functions and passing in the parameters expected by the current animation you are using. There are two kind of animations:

  • Custom Animations
  • Builtin Animations

In order to use an animation, import the corresponding function from the angular-animations-library module, and then call this function from the animations metadata of the component.

Builtin Animations

The Builtin Animations are animations ready to be used for entering/leaving effects. They are really easy to be implemented and they can or cannot accept some parameters.

Every animation defines its trigger name, used inside the HTML in order to implement the animation.

Most of these animations define only 2 transitions, even if few of them define also the transitions you will see in the Custom Animations, so active and inactive:

  1. :enter
  2. :leave

The duration is 300 ms by default, but it can be changed passing in the parameter.

You can combine entering and leaving animation of different types on the same element.

Builtin Animations Example

In the HTML implement the trigger of the animation, even without specifying any state:

<div [@foldIn] [@foldOut]>
  ANIMATED ELEMENT ON ENTER AND ON LEAVE WITH FOLD ANIMATION
</div>

Inside the component definition, add in the animations metadata, the corresponding function:

@Component({
  ...
  animations: [
    foldInAnimation()
  ]
})

In this way, you use the default parameters for the animation, but you can change the behavior passing in the parameters that will override the default ones:

@Component({
  ...
  animations: [
    foldInAnimation({
      duration: 500
    })
  ]
})

Builtin Bounce Animations

The parameters are an array of BounceAnimationParams so structured:

{ opacity: `number`, translation: `string`, offset: `number` }
AnimationTriggerFunctionParameters Values
Bounce In LeftbounceInLeftbounceInLeftAnimation{ 0, '-100%', 0 }, { 0.5, '20%', 0.7 }, { 1, '0', 1.0 }
Bounce In RightbounceInRightbounceInRightAnimation{ 0, '100%', 0 }, { 0.5, '-20%', 0.7 }, { 1, '0', 1.0 }
Bounce Out LeftbounceOutLeftbounceOutLeftAnimation{ 0, '0%', 0 }, { 0.5, '20%', 0.7 }, { 1, '-100%', 1.0 }
Bounce Out RightbounceOutRightbounceOutRightAnimation{ 0, '0%', 0 }, { 0.5, '-20%', 0.7 }, { 1, '100%', 1.0 }
Bounce In UpbounceInUpbounceInUpAnimation{ 0, '-100%', 0 }, { 0.5, '-20%', 0.7 }, { 1, '0%', 1.0 }
Bounce In DownbounceInDownbounceInDownAnimation{ 0, '100%', 0 }, { 0.5, '-20%', 0.7 }, { 1, '0%', 1.0 }
Bounce Out UpbounceOutUpbounceOutUpAnimation{ 0, '0%', 0 }, { 0.5, '20%', 0.7 }, { 1, '-100%', 1.0 }
Bounce Out DownbounceOutDownbounceOutDownAnimation{ 0, '0%', 0 }, { 0.5, '-20%', 0.7 }, { 1, '100%', 1.0 }

Builtin Fade Animations

AnimationTriggerFunction
Fade InfadeInfadeInAnimation
Fade OutfadeOutfadeOutAnimation
Fade In LeftbounceInLeftbounceInRightAnimation
Fade In RightbounceInRightbounceInRightAnimation
Fade Out LeftfadeOutLeftfadeOutLeftAnimation
Fade Out RightfadeOutRightfadeOutRightAnimation
Fade In UpfadeInUpfadeInUpAnimation
Fade In DownfadeInDownfadeInDownAnimation
Fade Out UpfadeOutUpfadeOutUpAnimation
Fade Out DownfadeOutDownfadeOutDownAnimation

Builtin Fold Animations

AnimationTriggerFunction
Fold InfoldInfoldInAnimation
Fold OutfoldOutfoldOutAnimation

Custom Animations

The Custom Animations are based on simple linear style changes or style changes with different keyframes.

Every animation defines its trigger name, which will be used inside the HTML in order to embed the animation, and it defines 4 different transitions for every trigger:

  1. active
  2. inactive
  3. *void => **
  4. * => void

In this way, you are free to trigger the animation whenever you want, simply changing the state of its trigger from inactive to active. The last two transitions are included only if you want that the animation will work also when the elment enters/leaves the view, through setting the includeVoidTransitions parameter to true, which is false by default.

You can pass several parameters to the animation, depending on the animation itself, but all of them share the two parameters includeVoidTransitions and duration, which is 300ms by default.

All the animations of the library can be combined if needed, in order to create more complex animations given by the composition of two or more animations.

Custom Animations Example

In the HTML implement the trigger of the animation, and then change the state when you want to trigger the animation:

<div [@highlight]="elementState" (mouseover)="elementState = 'active';" (mouseout)="elementState = 'inactive';">
  ANIMATED ELEMENT ON MOUSE OVER
</div>

Inside the component definition, add in the animations metadata, the corresponding function:

@Component({
  ...
  animations: [
    highlightAnimation()
  ]
})

In this way, you use the default parameters for the animation, but you can change the behavior passing in the parameters that will override the default ones:

@Component({
  ...
  animations: [
    highlightAnimation({
      duration: 500,
      includeVoidTransitions: true,
      highlightColor: 'red'
    })
  ]
})

Bounce Animation

There are two types of bounce animations:

AnimationTriggerFunction
Vertical BouncebounceVerticalbounceVerticalAnimation
Horizontal BouncebounceHorizontalbounceHorizontalAnimation

The parameters are an array of BounceAnimationStyleProperties, where each element represent a keyframe in the transition, and each element includes:

ParameterTypeDescription
opacitynumberOpacity of the current keyframe
translationstringVertical or horizontal movement of the current keyframe
offsetnumberOffset of the current keyframe. It must be a value between 0 and 1.

The default values are 3 keyframes with the following values:

{ opacity: 0, translation: '-100%', offset: 0 },
{ opacity: 1, translation: '-15px', offset: 0.3 },
{ opacity: 1, translation: '0', offset: 1.0 }

Fill Size Animation

There are two types of fill size animations:

AnimationTriggerFunction
Height FillfillHeightfillHeightAnimation
Width FillfillWidthfillWidthAnimation

The parameters are an object of FillSizeAnimationParams which includes:

ParameterTypeDescription
sizestringThe size to be filled horizontally or vertically, so the width or the height

The default values are:

{ size: '100%' }

Fly Animation

There are two types of fly animations:

AnimationTriggerFunction
Vertical FlyflyVerticalflyVerticalAnimation
Horizontal FlyflyHorizontalflyHorizontalAnimation

The parameters are an object of FlyAnimationParams which includes:

ParameterTypeDescription
flyValuestringTranslation to be applied vertically or horizontally when the state is active
voidFlyValuestringTranslation to be applied vertically or horizontally when the state is void

The default values are:

{ flyValue: '100%', voidFlyValue: '-100%' }

Appear Animation

The trigger is appear and the parameters are an object of AppearAnimationParams which includes just the default ones available for all the animations.

Fade Animation

The trigger is fade and the parameters are an object of FadeAnimationParams which includes:

ParameterTypeDescription
fadeValuenumberOpacity to be applied when the state is active
voidFadeValuenumberOpacity to be applied when the state is void

The default values are:

{ fadeValue: 0, voidFadeValue: 0 }

Highlight Animation

The trigger is highlight and the parameters are an object of HighlightAnimationParams which includes:

ParameterTypeDescription
highlightColorstringBackground color to be applied when the state is active
highlightVoidColorstringBackground color to be applied when the state is void

The default values are:

{ highlightColor: 'yellow', highlightVoidColor: highlightColor }

Rotate Animation

The trigger is rotate and the parameters are an object of RotateAnimationParams which includes:

ParameterTypeDescription
rotationValuenumberDegrees of rotation to be applied when the state is active
voidRotationValuenumberDegrees of rotation to be applied when the state is void

The default values are:

{ rotationValue: 45, voidRotationValue: -45 }

Scale Animation

The trigger is scale and the parameters are an object of ScaleAnimationParams which includes:

ParameterTypeDescription
scaleValuenumberScale value to be applied when the state is active
scaleVoidValuenumberScale value to be applied when the state is void

The default values are:

{ scaleValue: 1.1, scaleVoidValue: 0 }

Notes

Sometimes you can see an error of the ATO compiler when adding directly the functions inside the animations metadata block of the component.

The precise error is the following:

Angular - Error during template compile. Function calls are not supported in decorators but was called

This is due to the fact that the animations metadata block wants in only static code.

In order to get rid of this error, you need to define the animation values in an another file and then import them inside the component file.

Define an external file called for example custom-animations.ts exporting the animation's value:

import { bounceInDownAnimation } from 'angular-animations-library';

export const bounceInDownAnimationValue = bounceInDownAnimation();

Then inside the component's animations block, include the bounceInDownAnimationValue