player-javascript
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 creating an ICraftPlayer instance, 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.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <meta
      http-equiv="Content-Security-Policy"
      content="default-src 'self';
               script-src 'self' 'unsafe-inline' 'unsafe-eval' https://unpkg.com blob:;
               worker-src blob: 'self';
               style-src 'self' 'unsafe-inline';
               img-src 'self' data: blob:;
               font-src 'self' blob:;
               connect-src 'self' https://icraft.gantcloud.com blob:;
               object-src 'none';
               base-uri 'self';
               form-action 'self'"
    />
    <title>ICraft Player Animation Demo</title>
    <style>
      body {
        margin: 0;
        padding: 0;
      }
      #container {
        width: 100%;
        height: 100vh;
        position: relative;
      }
    </style>
    <script src="https://unpkg.com/@icraft/player@latest/dist/umd/icraft-player.min.js"></script>
  </head>
  <body>
    <div id="container"></div>
  </body>
  <script>
    const player = new ICraftPlayer({
      src: "https://icraft.gantcloud.com/api/static/templates/AWSCloud.iplayer",
      container: document.getElementById("container"),
      onReady: (player) => {
        const user = player.getElementsByName("User")?.[0];
        if (user) {
          player.playAnimationByElementKey(user.key, {
            animationDuration: 100,
            animationType: ICraftPlayer.AnimationType.Rotate,
          });
        }
      },
      onClick: (e) => {
        const { instance: Element } = e;
        if (!Element) {
          player?.cancelAnimation();
          return;
        }
        if (Element.typeName !== "line") {
          player?.playAnimationByElementKey(Element.key, {
            animationDuration: 3,
            animationType: ICraftPlayer.AnimationType.HeartBeat,
            animationShowTip: true,
          });
        } else {
          player?.playAnimationByElementKey(Element.key, {
            animationDuration: 3,
            animationType: ICraftPlayer.AnimationType.LoopFlow,
          });
        }
      },
    });
  </script>
</html>

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 color (line elements only)stringbluefalse
flowLineThinnessFlow line thinness factor - controls middle section contraction (line elements only)number0.5false
flowLineSizeFlow line size factor - controls overall thickness (line elements only)number0.12false

AnimationType

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