Skip to content

Commit

Permalink
feat: 🎸 add destroy method (#46)
Browse files Browse the repository at this point in the history
* feat: 🎸 add destroy method

* chore: 🤖 add README

* chore: 🤖 add changeset

* chore: 🤖 update example
  • Loading branch information
theashraf authored Nov 13, 2023
1 parent 541f491 commit 126a410
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-laws-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/dotlottie-web': minor
---

feat: 🎸 add destroy method
12 changes: 11 additions & 1 deletion apps/dotlottie-web-example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ app.innerHTML = `
<input type="checkbox" id="loopToggle" checked />
<label for="speed" class="speed-label">Speed: <span id="speed-value">x1</span></label>
<input type="range" id="speed" min="0.1" max="5" step="0.1" value="1" class="speed-slider" />
<button id="destroy" class="control-button" style="background: #cd3434;">Destroy</button>
</div>
</div>
`;
Expand Down Expand Up @@ -73,8 +74,10 @@ allCanvas.forEach((canvas) => {
fetch('/hamster.lottie')
.then(async (res) => res.arrayBuffer())
.then((data): void => {
const canvas = document.getElementById('canvas') as HTMLCanvasElement;

const dotLottie = new DotLottie({
canvas: document.getElementById('canvas') as HTMLCanvasElement,
canvas,
// src: 'https://lottie.host/f315768c-a29b-41fd-b5a8-a1c1dfb36cd2/CRiiNg8fqQ.lottie',
// src: '/lolo.json',
data,
Expand All @@ -89,6 +92,13 @@ fetch('/hamster.lottie')
const loopToggle = document.getElementById('loopToggle') as HTMLInputElement;
const speedSlider = document.getElementById('speed') as HTMLInputElement;
const speedValueSpan = document.getElementById('speed-value') as HTMLSpanElement;
const destroyButton = document.getElementById('destroy') as HTMLButtonElement;

destroyButton.addEventListener('click', () => {
canvas.remove();

dotLottie.destroy();
});

playPauseButton.addEventListener('click', () => {
if (dotLottie.playing) {
Expand Down
21 changes: 11 additions & 10 deletions packages/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,17 @@ const dotLottie = new DotLottie({

### Methods

| Method | Description |
| --------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| `play()` | Begins playback from the current animation position. |
| `pause()` | Pauses the animation without resetting its position. |
| `stop()` | Halts playback and returns the animation to its initial frame. |
| `setSpeed(speed: number)` | Sets the playback speed with the given multiplier. |
| `setLoop(loop: boolean)` | Configures whether the animation should loop continuously. |
| `setFrame(frame: number)` | Directly navigates the animation to a specified frame. |
| `addEventListener(event: string, listener: Function)` | Registers a function to respond to a specific animation event. |
| `removeEventListener(event: string, listener?: Function)` | Removes a previously registered function from responding to a specific animation event. |
| Method | Description |
| --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `play()` | Begins playback from the current animation position. |
| `pause()` | Pauses the animation without resetting its position. |
| `stop()` | Halts playback and returns the animation to its initial frame. |
| `setSpeed(speed: number)` | Sets the playback speed with the given multiplier. |
| `setLoop(loop: boolean)` | Configures whether the animation should loop continuously. |
| `setFrame(frame: number)` | Directly navigates the animation to a specified frame. |
| `addEventListener(event: string, listener: Function)` | Registers a function to respond to a specific animation event. |
| `removeEventListener(event: string, listener?: Function)` | Removes a previously registered function from responding to a specific animation event. |
| `destroy()` | Destroys the renderer instance and unregisters all event listeners. This method should be called when the canvas is removed from the DOM to prevent memory leaks. |

### Static Methods

Expand Down
10 changes: 10 additions & 0 deletions packages/web/src/dotlottie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,15 @@ export class DotLottie {
WasmLoader.setWasmUrl(url);
}

/**
* Destroys the DotLottie instance.
*
*/
public destroy(): void {
this._eventManager.removeAllEventListeners();
this._context = null;
this._renderer = null;
}

// #endregion
}
4 changes: 4 additions & 0 deletions packages/web/src/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,8 @@ export class EventManager {

listeners?.forEach((listener) => listener(event));
}

public removeAllEventListeners(): void {
this._eventListeners.clear();
}
}

0 comments on commit 126a410

Please sign in to comment.