player-react
动画
内置动画

内置动画

我们内置了一些动画类型,可以通过调用 player 实例的方法playAnimationByElementKey来执行元素动画。每 种动画类型都有其适用的元素类型和专有参数配置。

如何使用

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

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 === "tube") {
      player?.playAnimationByElementKey(Element.key, {
        animationDuration: 4,
        animationType: AnimationType.TubeZebraFlow,
        colorPrimary: "#0066ff",
        speed: 2.0,
      });
    } else if (Element.typeName === "line") {
      player?.playAnimationByElementKey(Element.key, {
        animationDuration: 3,
        animationType: AnimationType.LoopFlow,
        flowLineColor: "#00ff88",
      });
    } else {
      player?.playAnimationByElementKey(Element.key, {
        animationDuration: 3,
        animationType: AnimationType.HeartBeat,
        animationShowTip: true,
      });
    }
  };
 
  return (
    <div style={style}>
      <ICraftPlayer
        src='/templates/AWSCloud.iplayer'
        onClick={onClick}
        ref={playerRef}
      />
    </div>
  );
};

API

Functions

Namedescriptiontype
getElementsByName通过名称获取多个元素(name: string) => Element3D[]
playAnimationByElementKey使元素执行内置动画(elementKey: string,animationOptions: GlobalAnimationOptions) => void
cancelAnimation取消所有动画() => void

通用元素动画

适用于 cubecylindersphereprismconetorusmodelimagemodelcustommodeliconimagetexttext3dsvg 等通用元素类型。

FadeIn - 淡入

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"fadeIn"true
animationDuration动画时长(秒)number2true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse

FadeOut - 淡出

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"fadeOut"true
animationDuration动画时长(秒)number2true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse

UpAndDown - 上下浮动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"upAndDown"true
animationDuration动画时长(秒)number3true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse

HeartBeat - 心跳

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"heartBeat"true
animationDuration动画时长(秒)number2true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse

Bounce - 跳动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"bounce"true
animationDuration动画时长(秒)number2true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse

Rotate - 旋转

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"rotate"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse

EnterSubScene - 进入子场景

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"enterSubScene"true
animationDuration动画时长(秒)number1true
animationDelay动画延迟(秒)number0false

ExitSubScene - 退出子场景

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"exitSubScene"true
animationDuration动画时长(秒)number1true
animationDelay动画延迟(秒)number0false

BreathingWireframe - 呼吸线框

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"breathingWireframe"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse
speed速度number2.0false
colorPrimary颜色string#ffee00false
breathingFrequency呼吸频率number2false
width线宽number0.05false

FlowWireframe - 流线线框

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"flowWireframe"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse
speed速度number2.0false
colorPrimary颜色string#ffee00false
width线宽number0.05false

PulseRing - 脉冲圈

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"pulseRing"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleantruefalse
colorPrimary颜色string#00fffffalse
pulseFrequency脉冲发射频率(秒)number0.3false
maxRadius最大扩散半径number10.0false

线条元素动画

适用于 line 线条元素类型。

Draw - 绘制动画

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"draw"true
animationDuration动画时长(秒)number3true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleanfalsefalse
tipContent演说文字string''false
tipBackground演说文字背景色string#fffffffalse
tipHeight演说文字高度number1.4false

Flow - 流动动画

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"flow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
animationShowTip动态显示 tipbooleanfalsefalse
tipContent演说文字string''false
tipBackground演说文字背景色string#fffffffalse
tipHeight演说文字高度number1.4false
flowLineColor流线颜色string#18A1FFfalse
flowLineThinness流线细度系数,控制中间部分的收缩程度number0.5false
flowLineSize流线长度系数,控制整体长度number0.3false

LoopFlow - 循环流动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"loopFlow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
flowLineColor流线颜色string#18A1FFfalse
flowLineThinness流线细度系数,控制中间部分的收缩程度number0.5false
flowLineSize流线长度系数,控制整体长度number0.3false

管道元素动画

适用于 tube 管道元素类型。

TubeGasFlow - 管道气体流动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"tubeGasFlow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
speed流动速度number2.0false
colorPrimary基础颜色string#00227efalse
colorSecondary次要颜色string#0243f4false

TubeZebraFlow - 管道斑马线流动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"tubeZebraFlow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
speed流动速度number1false
colorPrimary基础颜色string#0066fffalse
colorSecondary次要颜色string#fffffffalse
stripeWidth条纹长度number1false

TubeFoamFlow - 管道泡沫流动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"tubeFoamFlow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
speed流动速度number0.1false
foamDensity泡沫密度number30false
colorPrimary基础颜色string#0243f4false
colorSecondary次要颜色string#85a5fffalse
pulseIntensity波动强度number0.1false

TubeArrowFlow - 管道箭头流动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"tubeArrowFlow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
speed流动速度number1.5false
arrowSize箭头大小number0.25false
arrowCount箭头数量number8false
colorPrimary箭头颜色string#246af7false

TubeEnergyFlow - 管道能量流动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"tubeEnergyFlow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
speed流动速度number3.0false
colorPrimary基础颜色string#0044fffalse
colorSecondary次要颜色string#0088fffalse

TubeElectronFlow - 管道电子流动

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"tubeElectronFlow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
speed流动速度number2.0false
colorPrimary基础颜色string#000000false
electronDensity电子流密度number16false

TubeLightFlow - 管道光流动画

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"tubeLightFlow"true
animationDuration动画时长(秒)number4true
animationDelay动画延迟(秒)number0false
speed流动速度number2.0false
colorPrimary基础颜色string#0243f4false
colorSecondary次要颜色string#cbdcfffalse

相机动画

特殊的相机控制动画。

Camera - 相机动画

Namedescriptiontypedefaultrequired
animationType动画类型AnimationType"camera"true
animationDuration动画时长(秒)number2true
animationDelay动画延迟(秒)number0false
cameraData相机位置CameraDataundefinedtrue

相关类型

interface CameraData {
  key: string; // 相机唯一标识
  type: string; // 相机类型
  position: number[]; // 相机位置坐标 [x, y, z]
  target: number[]; // 相机目标点坐标 [x, y, z]
  zoom: number; // 缩放级别
  quaternion?: number[]; // 相机旋转四元数 [x, y, z, w]
}
 
// 动画类型
export enum AnimationType {
  // 淡入
  FadeIn = "fadeIn",
  // 淡出
  FadeOut = "fadeOut",
  // 上下浮动
  UpAndDown = "upAndDown",
  // 心跳
  HeartBeat = "heartBeat",
  // 跳动
  Bounce = "bounce",
  // 旋转
  Rotate = "rotate",
  // 进入子场景
  EnterSubScene = "enterSubScene",
  // 退出子场景
  ExitSubScene = "exitSubScene",
  // 绘制动画
  Draw = "draw",
  // 流动动画
  Flow = "flow",
  // 相机关键点
  Camera = "camera",
  // 循环流动
  LoopFlow = "loopFlow",
  // 管道气体流动
  TubeGasFlow = "tubeGasFlow",
  // 管道斑马线流动
  TubeZebraFlow = "tubeZebraFlow",
  // 管道泡沫流动
  TubeFoamFlow = "tubeFoamFlow",
  // 管道箭头流动
  TubeArrowFlow = "tubeArrowFlow",
  // 管道能量流动
  TubeEnergyFlow = "tubeEnergyFlow",
  // 管道电子流动
  TubeElectronFlow = "tubeElectronFlow",
  // 管道光流动
  TubeLightFlow = "tubeLightFlow",
  // 呼吸线框
  BreathingWireframe = "breathingWireframe",
  // 流线线框
  FlowWireframe = "flowWireframe",
  // 脉冲圈
  PulseRing = "pulseRing",
}