Animate
- appear: 初始化时的入场动画;
- enter: 更新时的出现动画;
- update: 更新时的变化动画;
- leave: 更新时的动画;
通过如下方式为图形定义这四种动画场景的配置:
另外 均可支持回调,如下:
- /**
- * @param {Number} index shape 的索引值
- * @param {Number} id shape 的 id 标识
- **/
- delay(index, id) {}
参数 animation 为对应执行的动画名称,G2 默认内置了如下几种动画:
- const { Animate } = G2;
- /**
- * @param {String} animationType 动画场景类型 appear enter leave update
- * @param {String} 动画名称,用户自定义即可
- * @param {Function} 动画执行函数
- **/
- Animate.registerAnimation(animationType, animationName, animationFun);
图形 Shape 的动画接口说明:
说明:
例子
- const { Chart, Animate, Util } = G2;
- Animate.registerAnimation('appear', 'delayScaleInY', function(shape, animateCfg) {
- const box = shape.getBBox(); // 获取柱子包围盒
- const origin = shape.get('origin'); // 获取柱子原始数据
- const points = origin.points; // 获取柱子顶点
- // 计算柱子的变换中点
- const centerX = (box.minX + box.maxX) / 2;
- let centerY;
- if (points[0].y - points[1].y <= 0) { // 当顶点在零点之下
- centerY = box.maxY;
- centerY = box.minY;
- }
- // 设置初始态
- shape.attr('transform', [
- [ 't', -centerX, -centerY ],
- [ 's', 1, 0.1 ],
- [ 't', centerX, centerY ]
- ]);
- const index = shape.get('index');
- let delay = animateCfg.delay;
- if (Util.isFunction(delay)) {
- delay = animateCfg.delay(index);
- }
- let easing = animateCfg.easing;
- if (Util.isFunction(easing)) {
- easing = animateCfg.easing(index);
- }
- // 设置动画目标态
- shape.animate({
- transform: [
- [ 't', -centerX, -centerY ],
- [ 's', 1, 10 ],
- [ 't', centerX, centerY ]
- ]
- }, animateCfg.duration, easing, animateCfg.callback, delay);
- });
-
- for (let i = 0; i < 50; i++) {
- data.push({
- x: i,
- y: (Math.sin(i / 5) * (i / 5 - 10) + i / 6) * 5
- });
- }
- const chart = new Chart({
- container: 'c1',
- forceFit: true,
- height: 400
- });
- chart.axis('x', false);
- chart.legend(false);
- chart.source(data);
- chart.interval()
- .position('x*y')
- .color('y', '#4a657a-#308e92-#b1cfa5-#f5d69f-#f5898b-#ef5055')
- .animate({
- appear: {
- animation: 'delayScaleInY',
- easing: 'easeElasticOut',
- delay(index) {
- return index * 10;
- }
- }
- });
- chart.render();