Visitar URL original
Possibility to have a loop animation, using triggers. [Feature Request] · Issue #20621 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20201111210032/https://github.com/angular/angular/issues/20621
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to have a loop animation, using triggers. [Feature Request] #20621

Open
leonardopaiva opened this issue Nov 24, 2017 · 17 comments
Open

Possibility to have a loop animation, using triggers. [Feature Request] #20621

leonardopaiva opened this issue Nov 24, 2017 · 17 comments

Comments

@leonardopaiva
Copy link

@leonardopaiva leonardopaiva commented Nov 24, 2017 •

Hello i`m trying to make a loop animation with full control using triggers, right now theres no easy way to make it, only a few possibles work arounds.

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Not possible to make a loop inside a trigger config, right now i have to check animation done to swap the trigger back and create the loop.

edit:
sharing the example of how i do now:
https://angular-animations-demo.firebaseapp.com/loop-demo
source here:
https://github.com/leonardopaiva/angular-animation-basic-project/tree/master/src/app/loop-demo

Expected behavior

Something like a parameter to group, query or animate, setting loop true, or the number of repetitions, animate(300, boolLoopTrue) or animate(300, {boolRepetitions: 3}), example:


transition('default => standby', [
      group([
        query('.character', [
           animate(300, style({
            opacity: 0
          })),
          animate(300, style({
            opacity: 1
          }))
        ], boolLoopTrue)
      ])
    ])

What is the motivation / use case for changing the behavior?

I`m trying to make complex animations and need it to make things right, stop using work arounds.

Environment

Angular


Angular version: 5.0.0
"@angular/animations": "^5.0.1"

@nekkon
Copy link

@nekkon nekkon commented Dec 27, 2017 •

It would be great if iterations were defined the following ways:

animations: [
   ...
transition('inactive => active', animate('100ms ease-in infinite')),
transition('inactive => active', animate('100ms ease-in 5')), // iterates 5 times
...
]
 
@nekkon
Copy link

@nekkon nekkon commented Dec 27, 2017 •

@leonardopaiva Have you managed to make an infinite animation? Could you show an example? Unfortunately I tried by looping states but that crashed the browser :(. I have also tried adding a timeout but it didn't loop properly either and also delayed ssr.

@leonardopaiva
Copy link
Author

@leonardopaiva leonardopaiva commented Dec 29, 2017

@nekkon basically you need to use the animation done event that you can attach to the trigger, this event will give you a parameter with the event, it contains the end state, so using that you can swap between start and end states, but this is just a work around. I will up a repository with the example, as soon as possible, i will be back from a trip near January 3 to 5.

Another workaround is to make a basic css animation loop using keyframes, depends a lot on the case.

@jamesharvey2
Copy link

@jamesharvey2 jamesharvey2 commented Jan 13, 2018 •

Exactly. I support this idea. I'm surprised Angular 5 animation feature doesn't ship with built-in loop function. You'll have hard time to properly looping angular animation. CSS3 has loop count built in. You can set a number of times you want your animation to loop. Angular animations should come with the same loop function.

@jamesharvey2
Copy link

@jamesharvey2 jamesharvey2 commented Jan 13, 2018

Angular 6 should come with animation loop. Hopefully Google will make this happen.

@leonardopaiva
Copy link
Author

@leonardopaiva leonardopaiva commented Jan 15, 2018

awesome @jamesharvey2

@nekkon sry for the delay, but i finally created the loop demo "workaround" you can see it here
https://angular-animations-demo.firebaseapp.com/loop-demo just click "Go Loop State" the source is here https://github.com/leonardopaiva/angular-animation-basic-project/tree/master/src/app/loop-demo all you need is at loop demo component page.

hope that it helps you or another person with the same need.

@jamesharvey2
Copy link

@jamesharvey2 jamesharvey2 commented Jan 15, 2018

amazing... so it uses event fired when animation is finished.

Still I would like to see 'iteration-count' properly implemented on Angular 6.

@leonardopaiva
Copy link
Author

@leonardopaiva leonardopaiva commented Jan 16, 2018

yeah @jamesharvey2 this is just a work around, the ideal would be something like:

will make it repeat until the state changes:

transition('* => loop-state', [
    style({
      transform: 'translate3d(0,0,0)'
    }),
    animate('300ms', style({
      transform: 'translate3d(300%, 0, 0)'
    })),
    animate('300ms', style({
      transform: 'translate3d(300%, 100%, 0)'
    })),
    animate('300ms', style({
      transform: 'translate3d(0, 100%, 0)'
    })),
    animate('300ms', style({
      transform: 'translate3d(0, 0, 0)'
    }))
  ], {iterationCount: infinite })

or

will make it repeat 3 times

transition('* => loop-state', [
    style({
      transform: 'translate3d(0,0,0)'
    }),
    animate('300ms', style({
      transform: 'translate3d(300%, 0, 0)'
    })),
    animate('300ms', style({
      transform: 'translate3d(300%, 100%, 0)'
    })),
    animate('300ms', style({
      transform: 'translate3d(0, 100%, 0)'
    })),
    animate('300ms', style({
      transform: 'translate3d(0, 0, 0)'
    }))
  ], {iterationCount: 3})
@jamesharvey2
Copy link

@jamesharvey2 jamesharvey2 commented Jan 16, 2018

Yeah, iteration count should be implemented in Angular 6. I hope Google guys find this feature request.

@jamesharvey2
Copy link

@jamesharvey2 jamesharvey2 commented Jan 21, 2018 •

I would say it's time to abandon angular animations permanently.
Check this new javascript aniamtion framework and see how easily it can trigger complex animations:

http://animejs.com/documentation/#loop

I realized Angular animations is a waste of time. It's buggy, it has limitations and 'BrowserAnimationsModule' is extremely sensitive. It crashes the whole app for a tiny bit of mistake.
At this point, I will stop using angular animations until Google team decides to improve it but I doubt if they can create something that can beat Anime.js

@lazarljubenovic
Copy link
Contributor

@lazarljubenovic lazarljubenovic commented Jan 21, 2018

It crashes the whole app for a tiny bit of mistake.

And thank you for throwing an error instead of auto-magically not working and doing whatever the heck you think you should do to "correct" my mistake.

I will stop using angular animations until Google team decides to improve it

You do realize that Angular is open-source? You're free to jump in to help "create something that can beat Anime.js".

@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@MrCroft
Copy link

@MrCroft MrCroft commented Mar 21, 2018

@jamesharvey2
Let's say we would drop Angular Animations and design all animations with animejs instead... to get the loop functionality one desires, without having to write a workaround until it lands in a future 6.x version.
But then we'd have to write more code to:

  1. Take care of SSR (because animejs works directly with the DOM) - not sure if Domino would flawlessly handle animejs;
  2. Properly handle routing animations;
  3. Create stagger animations;
  4. Handle :enter, :leave and such;
  5. Probably there are more scenarios, these are just off the top of my head... The animations api in Angular is much more powerfull than animejs and, also, it's designed to work with Angular.

By the way, I see that the Animation Player has a restart method. Haven't tried it, but If it does what it intuitively says it does, you could use the player to handle a looping animation and restart() on onDone (if a condition is met, of course). Maybe that would be less code than other workarounds already in use out there.

OR I guess... ultimately you could use plain old css to create that one infinite loop animation you need in a project (not an n loop, for that you should probably use the Animation Player) rather than bringin in an entire library and then have headaches working to implement the above mentioned points without Angular Animations.

@Swoox
Copy link

@Swoox Swoox commented Jan 4, 2019 •

I'm not sure if it will help somebody out, but I have found a really nice infinity workaround.
I had seen this nice rotation rotate ball . What I did was add the following to the <img (@rotatedState.done)="rotate().
This will make the animation infinity.

@DmitryEfimenko
Copy link

@DmitryEfimenko DmitryEfimenko commented Feb 28, 2019 •

Here's how I implemented repeating an animation n times:

@ViewChild('animateEl', { read: ElementRef }) animateEl: ElementRef;

constructor(private _builder: AnimationBuilder) { }

private runAnimationNTimes(n: number) {
  // IMPORTANT: for some reason `const animation` needs to be declared in this function
  // If it's declared outside, animation does not work properly.
  const animation = animate('10s ease-out', keyframes([
    style({ offset: 0, transform: 'rotate(0)' }),
    // more styles here...
  ]));

  const factory = this._builder.build(animation);
  const player = factory.create(this.animateEl.nativeElement);
  player.onDone(() => {
    player.destroy();
    if (n > 1) {
      this.runAnimationNTimes(--n);
    }
  });

  player.play();
}
@xxzhong21330
Copy link

@xxzhong21330 xxzhong21330 commented May 7, 2020 •

it's so weird. when i use AnimationBuilder Service to create animationPlayer, the AnimationPlayer's onDestroy callback is never invoked with executing the animationPlayer. destroy() synchronously, but i execute animationPlayer.destroy() asynchronously, everything looks normal, here's how implemented infinite loop:

@ViewChild('el', { read: ElementRef }) el: ElementRef;
let myAnimation = this._builder.build(
    animate( '2s', keyframes([
        style({transform: `translate3d(0,0,0)`, offset: 0}),
        style({transform: `translate3d(-100px,0,0)`, offset: 1}),
    ]))
);
let animationPlay = ()=>{
    let player = myAnimation.create(this.el.nativeElement);
    player.play();
    player.onDone(()=>{
        //  must execute destroy() method asynchronously
        setTimeout(()=>{
            player.destroy();
        })
    });
    player.onDestroy(()=>{
        animationPlay();
    });
}
animationPlay();
@AliSawari
Copy link

@AliSawari AliSawari commented May 10, 2020

@leonardopaiva any updates on the requested feature ? this should have been implemented by now 😐

@jelbourn jelbourn added P4 and removed severity2: inconvenient labels Oct 1, 2020
@nektobit
Copy link

@nektobit nektobit commented Oct 15, 2020

Hi
Any news?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.