Antv G6 图例 实现点击‘指定节点’对应节点放大缩小动画

找到满足要求的节点 此处我自己写死的,根据自己业务去修改

this.graph.on('node:click', (evt) => {
  // 放大缩小方法
  if (evt.item._cfg.model.id === '9') {
    const node = this.graph.findById('8');
    console.log(node);
    this.graph.updateItem(node, {
      attrs: {
        time: '1'
      }
    });
  }

要实现放大缩小动画就要使用自定义节点方法, 用上面this.graph.updateItem接收到的attrs.time去判断G6A collection of charts made with the Grammar of Graphicshttps://g6.antv.antgroup.com/manual/middle/elements/nodes/custom-node

registerNode() {
  G6.registerNode( // registerNode自定义节点
    'circle-animate',
    {
      draw(cfg, group) { // draw绘制图片
        const keyShape  = group.addShape('image', {
          attrs: {
            x: 0, //图片在x轴的位置
            y: -3,
            width: 7,
            height: 7,
            img: cfg.img,
            label: cfg.label
          },
          name: cfg.label,
        });
        if (cfg.label) {
          // 如果有文本
          // 如果需要复杂的文本配置项,可以通过 labeCfg 传入
          // const style = (cfg.labelCfg && cfg.labelCfg.style) || {};
          // style.text = cfg.label;
          const label = group.addShape('text', {
            // attrs: style
            attrs: {
              x: 3, //文本在x轴的位置
              y: 10,
              textAlign: 'center',
              textBaseline: 'middle',
              text: cfg.label,
              fill: '#fff',
              fontSize: 4
            },

            name: 'text-shape',
            // 设置 draggable 以允许响应鼠标的图拽事件
            draggable: true,
          });
        }
        const attrs = cfg.attrs as any;
        if (attrs?.time) {
          keyShape.animate(
            (ratio) => {
              const diff = ratio <= 0.5 ? ratio : (1 - ratio);
              // [2,2] [1,1]
              const toMatrix = Util.transform(
                [1, 0, 0, 0, 1, 0, 0, 0, 1],
                [['s', 1 / 2 + diff, 1 / 2 + diff]],
              );
              return {
                matrix: toMatrix,
              };
            },
            {
              // 动画重复
              repeat: true,
              duration: 3000,
              easing: 'easeCubic',
            },
          );
        }
        return keyShape;
      },
    },
  );
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值