自定义动画

    • 动画注册 Animate.registAnimation()
    • 动画配置 geom.animate()
    1. chart.interval().position('x*y').animate({
    2. enter:{
    3. animation: 'fadeIn', // 动画名称
    4. easing: 'easeQuartIn', // 动画缓动效果
    5. delay: 100, // 动画延迟执行时间
    6. duration: 600 // 动画执行时间
    7. }
    8. });

    在 G2 中,我们提供了四种动画场景类型:

    • appear: 初始化时的入场动画;
    • enter: 更新时的出现动画;
    • update: 更新时的变化动画;
    • leave: 更新时的动画;
      上述方法更详细的使用说明详见: 。

    • 设置图形的初始状态
    • 确定图形的结束状态
    • 调用动画函数
      这里以定义一个柱状图的动画示例:

    • 计算变换中点。当顶点在零点之上时,延 Y 轴正向放大,所以变换的中点在包围盒底部中间;当顶点在零点之下时,延 Y 轴反向放大。所以变换的中点在包围盒顶部中间。
    • 实现延迟放大效果的动画。调用 shape 的 animate() 方法,传入变换的结束状态、动画时间和缓动函数。结束状态中可以配置延迟参数 delay ,给每个 shape 的动画添加一个跟序号成正比的延迟,即可实现依次放大的效果。
    1. // shape 是柱子对象,animateCfg 是动画配置
    2. const animateFun = function(shape, animateCfg) {
    3. const box = shape.getBBox(); // 获取柱子包围盒
    4. const origin = shape.get('origin'); // 获取柱子原始数据
    5. const points = origin.points; // 获取柱子顶点
    6. // 计算柱子的变换中点
    7. const centerX = (box.minX + box.maxX) / 2;
    8. let centerY;
    9. if (points[0].y - points[1].y <= 0) { // 当顶点在零点之下
    10. centerY = box.maxY;
    11. } else {
    12. centerY = box.minY;
    13. }
    14. // 设置初始态
    15. shape.attr('transform', [
    16. [ 't', -centerX, -centerY ],
    17. [ 's', 1, 0.1 ],
    18. [ 't', centerX, centerY ]
    19. ]);
    20. const index = shape.get('index');
    21. let delay = animateCfg.delay;
    22. if (G2.Util.isFunction(delay)) {
    23. delay = animateCfg.delay(index);
    24. let easing = animateCfg.easing;
    25. if (G2.Util.isFunction(easing)) {
    26. easing = animateCfg.easing(index);
    27. }
    28. // 设置动画目标态
    29. shape.animate({
    30. transform: [
    31. [ 't', -centerX, -centerY ],
    32. [ 's', 1, 10 ],
    33. [ 't', centerX, centerY ]
    34. ]
    35. }, animateCfg.duration, easing, animateCfg.callback, delay);
    36. }
    1. chart.interval()
    2. .position('x*y')
    3. .color('y', '#4a657a-#308e92-#b1cfa5-#f5d69f-#f5898b-#ef5055')
    4. .animate({
    5. appear: {
    6. animation: 'delayScaleInY',
    7. easing: 'easeElasticOut',
    8. delay: index => {
    9. return index * 10;
    10. }
    11. }