Skip to content

Commit

Permalink
feat: 🎸 freeze & unfreeze methods
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Nov 28, 2023
1 parent 82c01b6 commit 385e3c8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions apps/dotlottie-web-example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ app.innerHTML = `
<label for="loopToggle">Loop: </label>
<input type="checkbox" id="loopToggle" checked />
</div>
<div>
<button id="freeze" class="control-button">Freeze</button>
<button id="unfreeze" class="control-button">Unfreeze</button>
</div>
<label for="frameSlider">Frame: <span id="current-frame">0</span></label>
<input type="range" id="frameSlider" min="0" step="0.1" />
Expand Down Expand Up @@ -128,6 +133,16 @@ fetch('/hamster.lottie')
const startFrameInput = document.getElementById('startFrame') as HTMLInputElement;
const endFrameInput = document.getElementById('endFrame') as HTMLInputElement;
const setSegmentsButton = document.getElementById('setSegments') as HTMLButtonElement;
const freezeButton = document.getElementById('freeze') as HTMLButtonElement;
const unfreezeButton = document.getElementById('unfreeze') as HTMLButtonElement;

freezeButton.addEventListener('click', () => {
dotLottie.freeze();
});

unfreezeButton.addEventListener('click', () => {
dotLottie.unfreeze();
});

setSegmentsButton.addEventListener('click', () => {
const startFrame = parseInt(startFrameInput.value, 10);
Expand Down
2 changes: 2 additions & 0 deletions packages/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ The `DotLottie` constructor accepts a config object with the following propertie
| `load(config: Config)` | Loads a new configuration or a new animation. |
| `setMode(mode: string)` | Sets the animation play mode. |
| `setSegments(startFrame: number, endFrame: number)` | Sets the start and end frame of the animation. |
| `freeze()` | Freezes the animation by stopping the animation loop. |
| `unfreeze()` | Unfreezes the animation by resuming the animation loop. |

### Static Methods

Expand Down
17 changes: 17 additions & 0 deletions packages/web/src/dotlottie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,5 +832,22 @@ export class DotLottie {
this._playbackState = 'stopped';
}

/**
* Freezes the animation by stopping the animation loop.
*
*/
public freeze(): void {
this._stopAnimationLoop();
}

/**
* Unfreezes the animation by resuming the animation loop.
*
*/
public unfreeze(): void {
this._synchronizeAnimationTiming(this._currentFrame);
this._startAnimationLoop();
}

// #endregion
}

0 comments on commit 385e3c8

Please sign in to comment.