player-react
动画

动画

我们内置了一些动画类型,可以通过调用 player 实例的方法playAnimationByElementKey来执行元素动画

如何使用

通过refonReady拿到 player 实例后,调用playAnimationByElementKey方法,传入元素的key和动画参 数,即可执行动画。通过cancelAnimation方法可以取消当前正在播放的动画。

import {
  AnimationType,
  ClickParams,
  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: ClickParams) => {
    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
getElementsByName通过名称获取多个元素(name: string) => Element3D[]
playAnimationByElementKey使元素执行内置动画(elementKey: string,animationOptions: AnimationActionOptions) => void
cancelAnimation取消所有动画() => void

AnimationActionOptions

Namedescriptiontypedefaultrequired
animationType动画类型AnimationTypeAnimationType.FadeIntrue
animationDuration动画时长(秒)number2true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse
tipContent演说文字stringnamefalse
tipBackground演说文字颜色stringwhitefalse
flowLineColor流线颜色(只试用于线条元素)stringbluefalse
flowLineThinness流线细度系数,控制中间部分的收缩程度,越大中间部分越突出(只试用于线条元素)number0.5false
flowLineSize流线大小系数,控制流线整体的粗细程度(只试用于线条元素)number0.12false

AnimationType

Namedescription
FadeIn淡入 (只试用于非线条元素)
FadeOut淡出 (只试用于非线条元素)
UpAndDown上下浮动 (只试用于非线条元素)
HeartBeat心跳 (只试用于非线条元素)
ColorChange颜色变化 (只试用于非线条元素)
Bounce跳动 (只试用于非线条元素)
Rotate旋转 (只试用于非线条元素)
EnterSubScene进入子场景 (只试用于非线条元素)
ExitSubScene退出子场景 (只试用于非线条元素)
Draw绘制动画 (只试用于线条元素)
Flow流动动画 (只试用于线条元素)
LoopFlow循环流动 (只试用于线条元素)