Skip to content

feat: make options for appendAnimation and detachAnimation optional #5025

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
70 changes: 39 additions & 31 deletions packages/model-viewer/src/features/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@ const $detachAnimation = Symbol('detachAnimation');
const $paused = Symbol('paused');

interface PlayAnimationOptions {
repetitions: number, pingpong: boolean,
repetitions: number;
pingpong: boolean;
}

interface AppendAnimationOptions {
pingpong: boolean, repetitions: number|null, weight: number,
timeScale: number, fade: boolean|number, warp: boolean|number,
relativeWarp: boolean, time: number|null
pingpong?: boolean;
repetitions?: number|null;
weight?: number;
timeScale?: number;
fade?: boolean|number;
warp?: boolean|number;
relativeWarp?: boolean;
time?: number|null;
}

interface DetachAnimationOptions {
fade: boolean|number
fade?: boolean|number;
}

const DEFAULT_PLAY_OPTIONS: PlayAnimationOptions = {
Expand Down Expand Up @@ -260,27 +266,28 @@ export const AnimationMixin = <T extends Constructor<ModelViewerElementBase>>(
}
}

[$appendAnimation](
animationName: string = '',
options: AppendAnimationOptions = DEFAULT_APPEND_OPTIONS) {
const repetitions = options.repetitions ?? Infinity;
const mode = options.pingpong ?
LoopPingPong :
(repetitions === 1 ? LoopOnce : LoopRepeat);

const needsToStop = !!options.repetitions || 'pingpong' in options;

this[$scene].appendAnimation(
animationName ? animationName : this.animationName,
mode,
repetitions,
options.weight,
options.timeScale,
options.fade,
options.warp,
options.relativeWarp,
options.time,
needsToStop);
[$appendAnimation](
animationName: string = '',
options: AppendAnimationOptions = {}) {
const opts = {...DEFAULT_APPEND_OPTIONS, ...options};
const repetitions = opts.repetitions ?? Infinity;
const mode = opts.pingpong ?
LoopPingPong :
(repetitions === 1 ? LoopOnce : LoopRepeat);

const needsToStop = repetitions !== Infinity || mode !== LoopPingPong;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohammadbaghaei I am particularly not 100% sure about this line.


this[$scene].appendAnimation(
animationName ? animationName : this.animationName,
mode,
repetitions,
opts.weight,
opts.timeScale,
opts.fade,
opts.warp,
opts.relativeWarp,
opts.time,
needsToStop);

// If we are currently paused, we need to force a render so that
// the scene updates to the first frame of the new animation
Expand All @@ -290,11 +297,12 @@ export const AnimationMixin = <T extends Constructor<ModelViewerElementBase>>(
}
}

[$detachAnimation](
animationName: string = '',
options: DetachAnimationOptions = DEFAULT_DETACH_OPTIONS) {
this[$scene].detachAnimation(
animationName ? animationName : this.animationName, options.fade);
[$detachAnimation](
animationName: string = '',
options: DetachAnimationOptions = {}) {
const opts = {...DEFAULT_DETACH_OPTIONS, ...options};
this[$scene].detachAnimation(
animationName ? animationName : this.animationName, opts.fade);

// If we are currently paused, we need to force a render so that
// the scene updates to the first frame of the new animation
Expand Down