player-react
Animation

Animation

We have built-in animation types that can be executed on elements by calling the player instance method playAnimationByElementKey.

How to Use

After getting the player instance through ref or onReady, call the playAnimationByElementKey method with the element's key and animation parameters to execute the animation. Use the cancelAnimation method to cancel the currently playing animation.

import {
  AnimationType,
  MouseEventParams,
  ICraftPlayer,
  ICraftPlayerInstance,
} from "@icraft/player-react";
import React from "react";
 
const style = {
  width: "100%",
  height: "100%",
  position: "relative" as const,
  overflow: "hidden" as const,
};
 
export default () => {
  const playerRef = React.useRef<ICraftPlayerInstance>();
 
  const onClick = (e: MouseEventParams) => {
    const player = playerRef.current;
    const { instance: Element } = e;
    if (!Element) {
      player?.cancelAnimation();
      return;
    }
    if (Element.typeName !== "line") {
      player?.playAnimationByElementKey(Element.key, {
        animationDuration: 3,
        animationType: AnimationType.HeartBeat,
        animationShowTip: true,
      });
    } else {
      player?.playAnimationByElementKey(Element.key, {
        animationDuration: 3,
        animationType: AnimationType.LoopFlow,
      });
    }
  };
 
  const onReady = (player: ICraftPlayerInstance) => {
    const user = player.getElementsByName("User")?.[0];
    if (user) {
      player.playAnimationByElementKey(user.key, {
        animationDuration: 100,
        animationType: AnimationType.Rotate,
      });
    }
  };
 
  return (
    <div style={style}>
      <ICraftPlayer
        src='/templates/AWSCloud.iplayer'
        onClick={onClick}
        onReady={onReady}
        ref={playerRef}
      />
    </div>
  );
};

API

Functions

NameDescriptionType
getElementsByNameGet multiple elements by name(name: string) => Element3D[]
playAnimationByElementKeyExecute built-in animation on element(elementKey: string,animationOptions: AnimationActionOptions) => void
cancelAnimationCancel all animations() => void

AnimationActionOptions

NameDescriptionTypeDefaultRequired
animationTypeAnimation typeAnimationTypeAnimationType.FadeIntrue
animationDurationAnimation duration (sec)number2true
animationDelayAnimation delay (sec)number0false
animationShowTipShow dynamic tipbooleantruefalse
tipContentNarration textstringnamefalse
tipBackgroundNarration text colorstringwhitefalse
flowLineColorFlow line colorstringbluefalse
flowLineThinnessFlow line thinness factor (controls middle section contraction, higher value means more prominent middle section)number0.5false
flowLineSizeFlow line size factor (controls overall line thickness)number0.12false

AnimationType

NameDescription
FadeInFade in (non-line elements only)
FadeOutFade out (non-line elements only)
UpAndDownFloat up/down (non-line elements only)
HeartBeatHeartbeat (non-line elements only)
ColorChangeColor change (non-line elements only)
BounceBounce (non-line elements only)
RotateRotate (non-line elements only)
EnterSubSceneEnter sub-scene (non-line elements only)
ExitSubSceneExit sub-scene (non-line elements only)
DrawDraw animation (line elements only)
FlowFlow animation (line elements only)
LoopFlowLoop flow (line elements only)