Skip to content

Commit 65fe45e

Browse files
committed
Cancel current animation before to play another one
1 parent 3dd93d0 commit 65fe45e

File tree

8 files changed

+28
-15
lines changed

8 files changed

+28
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [1.1.4] - 2020-04-22
10+
### Fixed
11+
- Cancel current animation before to play another one, it's required to avoid issues when `iterations` was equal to `Infinity`.
12+
913
## [1.1.3] - 2020-04-21
1014
### Fixed
1115
- Restore class name from `onCancel` event and remove `previousAnimation` from Animation manager.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@proyecto26/animatable-component",
3-
"version": "1.1.3",
3+
"version": "1.1.4",
44
"private": false,
55
"description": "Animate once, use Everywhere! 💫",
66
"author": "Proyecto 26",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const easingOutCubic = EASING_FUNCTIONS[EASING.EASE_OUT_CUBIC];
9999

100100
### Script tag
101101

102-
- Put a script tag similar to this `<script src='https://unpkg.com/animatable-component@1.1.1/dist/animatable-component.js'></script>` in the head of your index.html
102+
- Put a script tag similar to this `<script src='https://unpkg.com/animatable-component@1.1.4/dist/animatable-component.js'></script>` in the head of your index.html
103103
- Then you can use the element anywhere in your template, JSX, html etc
104104

105105
### Node Modules

src/components/animatable-component/animatable-component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@ export class AnimatableComponent implements IAnimatableComponent {
327327
}
328328

329329
componentDidLoad() {
330-
this.manager.update();
330+
this.manager.savedState();
331331
}
332332

333333
componentWillUpdate() {
334334
this.manager.setState(this.element, this);
335335
}
336336

337337
componentDidUpdate() {
338-
this.manager.update();
338+
this.manager.savedState();
339339
}
340340

341341
componentDidUnload() {

src/components/animatable-cube/animatable-cube.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,15 @@ export class Cube implements IAnimatableComponent {
337337

338338
componentDidLoad() {
339339
this.manager.setState(this.element, this);
340-
this.manager.update();
340+
this.manager.savedState();
341341
}
342342

343343
componentWillUpdate() {
344344
this.manager.setState(this.element, this);
345345
}
346346

347347
componentDidUpdate() {
348-
this.manager.update();
348+
this.manager.savedState();
349349
}
350350

351351
componentDidUnload() {

src/index.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,6 @@ <h3>
11271127
animatableCube.keyFrames = spinCube;
11281128

11291129
function playAnimation (animation) {
1130-
currentAnimatable.destroy();
11311130
if (animation) currentAnimatable.animation = animation;
11321131
currentAnimatable.play();
11331132
}
@@ -1184,7 +1183,7 @@ <h3>
11841183
});
11851184
button.addEventListener('click', function(e) {
11861185
e.preventDefault();
1187-
const animation = selectAnimation.value
1186+
const animation = selectAnimation.value;
11881187
currentAnimatable.animation = animation;
11891188
currentAnimatable.play();
11901189
animatableButton.play();
@@ -1198,7 +1197,6 @@ <h3>
11981197
console.log('ANIMATION FINISH', event);
11991198

12001199
//Remove listener and create my own infinity :)
1201-
animatable.destroy();
12021200
animatable.autoPlay = false;
12031201
animatable.removeEventListener("finish", startSecondAnimation);
12041202
animatable.options = {

src/utils/waapi.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export class AnimationManager {
8686
private state: IAnimatable
8787
private animation: Animation = null
8888
private className: string
89+
private isUpdatingState: boolean
8990

9091
constructor(initState: IAnimatable) {
9192
this.state = initState;
@@ -126,28 +127,38 @@ export class AnimationManager {
126127
currentAnimation.cancel();
127128
}
128129

130+
/**
131+
* Emit start event if playState is not running or playing a new animation.
132+
*/
129133
playAnimation() {
134+
if (
135+
this.currentAnimation.playState === 'running' &&
136+
!this.isUpdatingState
137+
) return;
130138
/**
131-
* Prevent emit start event if playState is running
139+
* Cancel current animation before to create another one
132140
*/
133-
if (this.currentAnimation.playState === 'running') return;
134-
this.onStartAnimation();
141+
if (this.isUpdatingState) {
142+
this.destroyAnimation();
143+
}
135144
this.currentAnimation.play();
145+
this.onStartAnimation();
136146
}
137147

138148
setState(element: HTMLElement, newState: IAnimatable) {
149+
this.isUpdatingState = true;
139150
this.element = element;
140151
this.state = newState;
141152
}
142153

143-
update() {
154+
savedState() {
144155
/**
145156
* Check if `autoPlay` is enabled to play a new animation and emit the event.
146157
*/
147158
if (this.state.autoPlay) {
148-
this.destroyAnimation();
149159
this.playAnimation();
150160
}
161+
this.isUpdatingState = false;
151162
}
152163

153164
/**

0 commit comments

Comments
 (0)