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

Allow playOnce for interactivity action #111

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/hooks/useLottieInteractivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const useInitInteractivity = ({
}: InitInteractivity) => {
useEffect(() => {
const wrapper = wrapperRef.current;
let playCount = 0;

if (!wrapper || !animationItem || !actions.length) {
return;
Expand Down Expand Up @@ -117,6 +118,12 @@ export const useInitInteractivity = ({
animationItem.resetSegments(true);
animationItem.play();
}
if (action.type === "playOnce" && playCount === 0) {
// Play: Reset segments and continue playing full animation from current position
animationItem.resetSegments(true);
animationItem.play();
playCount++;
}

if (action.type === "stop") {
// Stop: Stop playback
Expand Down Expand Up @@ -218,6 +225,14 @@ export const useInitInteractivity = ({
animationItem.playSegments(action.frames as AnimationSegment);
}

if (action.type === "playOnce" && playCount === 0) {
if (animationItem.isPaused) {
animationItem.resetSegments(false);
}
animationItem.playSegments(action.frames as AnimationSegment);
playCount++;
}

if (action.type === "stop") {
animationItem.goToAndStop(action.frames[0], true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type Axis = "x" | "y";
export type Position = { [key in Axis]: number | [number, number] };

export type Action = {
type: "seek" | "play" | "stop" | "loop";
type: "seek" | "play" | "stop" | "loop" | "playOnce";
frames: [number] | [number, number];
visibility?: [number, number];
position?: Position;
Expand Down