Skip to content

Commit

Permalink
Add chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
4ver committed Jun 24, 2024
1 parent 140d7de commit 7f952a0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 30 deletions.
36 changes: 17 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ const animate = new Animate({
easing: Easing.easeInOutQuad,
});

// Attach event listeners
animate.on('start', () => console.log('Animation Start'));
animate.on('update', value => console.log('Animation Update:', value));
animate.on('complete', () => console.log('Animation Complete'));
animate.on('stop', () => console.log('Animation Stop'));

// Start the animation
animate.start();
animate
.on('start', () => console.log('Animation Start'))
.on('update', value => console.log('Animation Update:', value))
.on('complete', () => console.log('Animation Complete'))
.on('stop', () => console.log('Animation Stop'))
.start();
```

### Sequence of Animations
Expand All @@ -82,13 +80,12 @@ const animate2 = new Animate({
const sequence = new Sequence({ items: [animate1, animate2] });

// Attach event listeners
sequence.on('start', () => console.log('Sequence Start'));
sequence.on('update', value => console.log('Sequence Update:', value));
sequence.on('complete', () => console.log('Sequence Complete'));
sequence.on('stop', () => console.log('Sequence Stop'));

// Start the sequence
sequence.start();
sequence
.on('start', () => console.log('Sequence Start'))
.on('update', value => console.log('Sequence Update:', value))
.on('complete', () => console.log('Sequence Complete'))
.on('stop', () => console.log('Sequence Stop'))
.start();
```

### Parallel Sequences
Expand Down Expand Up @@ -125,10 +122,11 @@ const sequence1 = new Sequence({ items: [animate1, animate2] });
const parallelSequence = new Sequence({ items: [sequence1, animate3], parallel: true });

// Attach event listeners
parallelSequence.on('start', () => console.log('Parallel Sequence Start'));
parallelSequence.on('update', value => console.log('Parallel Sequence Update:', value));
parallelSequence.on('complete', () => console.log('Parallel Sequence Complete'));
parallelSequence.on('stop', () => console.log('Parallel Sequence Stop'));
parallelSequence
.on('start', () => console.log('Parallel Sequence Start'))
.on('update', value => console.log('Parallel Sequence Update:', value))
.on('complete', () => console.log('Parallel Sequence Complete'))
.on('stop', () => console.log('Parallel Sequence Stop'));

// Start the sequence
parallelSequence.start();
Expand Down
13 changes: 5 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"author": "Donal Linehan",
"license": "MIT",
"dependencies": {
"events": "^3.3.0",
"eventemitter3": "^5.0.1",
"ts-easing": "^0.2.0"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion src/animate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from 'events';
import EventEmitter from 'eventemitter3';
import { TEasing } from 'ts-easing';
import { easing as e } from './easing';

Expand Down Expand Up @@ -96,12 +96,14 @@ export class Animate extends EventEmitter {
this.startTime = performance.now();
this.emit('start');
this.requestId = requestAnimationFrame(this.tick);
return this;
}

public stop() {
if (this.requestId) {
cancelAnimationFrame(this.requestId);
this.emit('stop');
}
return this;
}
}
4 changes: 3 additions & 1 deletion src/sequence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from 'events';
import EventEmitter from 'eventemitter3';
import { Animate, AnimatableValue } from './animate';

type SequenceItem = Animate | Sequence;
Expand Down Expand Up @@ -64,6 +64,7 @@ export class Sequence extends EventEmitter {
this.activeAnimates = 0;
this.emit('start');
this.next();
return this;
}

public stop() {
Expand All @@ -75,5 +76,6 @@ export class Sequence extends EventEmitter {
}
});
this.emit('stop');
return this;
}
}

0 comments on commit 7f952a0

Please sign in to comment.