Skip to content
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

支持 Lottie 播放能力 #4221

Closed
xiaoiver opened this issue Oct 26, 2022 · 5 comments
Closed

支持 Lottie 播放能力 #4221

xiaoiver opened this issue Oct 26, 2022 · 5 comments
Assignees
Labels

Comments

@xiaoiver
Copy link
Contributor

xiaoiver commented Oct 26, 2022

问题背景

除了使用 Web Animations API 描述动画,我们还支持播放 Lottie 格式,为此我们提供了一个类似 lottie-web 的播放器。配合图表中已有元素也可以增强动画表现力。

ECharts 中的用法如下:https://github.com/pissang/lottie-parser#basic-usage
经由 Lottie Parser 解析后得到的 elements 可以加入 group 中渲染:

const { elements } = lottieParser.parse(data);

chart.setOption({
  graphic: {
    elements: [
      {
        type: 'group',
        scaleX: scale,
        scaleY: scale,
        // Elements is compatitable with echarts graphic.
        children: elements,
      },
    ],
  },
});

我们提供的 g-lottie-player 使用方式如下。同时提供简单的动画控制方法例如播放、暂停以及跳转到指定时刻或帧,加入到画布后就可以像基础图形一样任意操作它们。

import { loadAnimation } from '@antv/g-lottie-player';

// 解析 Lottie 文件
const animation = loadAnimation(lottie_json, { loop: true, autoplay: true });

// 返回一个 Group 并渲染到画布
const wrapper = animation.render(canvas);

// 自由操作
wrapper.scale(0.5);
wrapper.translate(100, 100);

// 动画控制
animation.play();
animation.pause();
animation.goTo(2000);

Chart API

由于 g-lottie-player 返回的是一个解析后的容器 Group,随后的变换、动画控制都交由用户自由操作。因此只需要拿到画布即可:

const chart = new Chart({
  container: node,
});
const { canvas } = chart.context();

// 解析后的 Group 渲染到画布
const wrapper = animation.render(canvas);
animation.play();

这样也不用 G2 设计额外的 API 了。

@pearmini
Copy link
Member

pearmini commented Oct 27, 2022

嗯嗯,这是一个天然支持的方案。更好的方案如下(可以之后支持):

const options = {
  children: [
    {
      type: 'lottie',
      encode: {
        x: '',
        y: '',
        src: data: lottie_json,
      },
      loop: true,
      autoplay: true,
      style: {
        transform: 'scale(0.5) translate(100, 100)',
      },
    },
  ],
};

@xiaoiver
Copy link
Contributor Author

xiaoiver commented Oct 27, 2022

两种方式优缺点如下:

  • 自由操作方案:
    • 优点:最高的自由度。从 Chart 上下文拿到画布后,用户自行操作:变换 & 动画控制(播放、暂停、跳转到任意帧)
    • 缺点:无法数据驱动
  • Mark 方案:
    • 优点:数据驱动
    • 缺点:新增 Mark 类型,G2 需要内置 g-lottie-player。无法进行动画控制(或者有办法拿到 animation 对象)

两者并不冲突,可以都提供。

@pearmini
Copy link
Member

嗯嗯,都可以提供

@hustcc
Copy link
Member

hustcc commented Oct 27, 2022

以不侵入的方式加到官网吧,做成 mark 后面做!

xiaoiver added a commit that referenced this issue Nov 10, 2022
hustcc pushed a commit that referenced this issue Nov 14, 2022
@hustcc
Copy link
Member

hustcc commented Dec 1, 2022

转入到讨论,这里关闭。

@hustcc hustcc closed this as completed Dec 1, 2022
hustcc pushed a commit that referenced this issue May 16, 2023
hustcc pushed a commit that referenced this issue May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants