antv x6 沿边图标循环动画实现

文章讲述了如何在AntVX6图形库中使用自定义边和图标,通过注册边缘并设置过渡动画,实现图标沿边的循环动画效果。代码涉及图形数据处理、动画延迟和完成回调,以及计算节点端口以确定动画路径。

实现效果,如下图,边上存在两个图标,要求图标延边一直循环动画
在这里插入图片描述
实现方法:
1.注册一个自定义边,边上定义两个图标,并设置其初始位置
在这里插入图片描述
2.使用transition给边设置动画,利用complte方法实现循环动画
在这里插入图片描述

补充代码:
1.注册边的代码

Graph.registerEdge(
  'arrow-edge',
  {
   
   
    markup: [
      {
   
   
        tagName: 'path',
        selector: 'wrap',
        attrs: {
   
   
          fill: 'none',
          cursor: 'pointer'
          // stroke: '#ff0000'
        }
      },
      {
   
   
        tagName: 'path',
        selector: 'line',
        attrs: {
   
   
          fill: 'none',
          'pointer-events': 'none'
        }
      },
      {
   
   
        tagName: 'path',
        groupSelector: 'arrow',
        selector: 'arrow1'
      },
      {
   
   
        tagName: 'path',
        groupSelector: 'arrow',
        selector: 'arrow2'
      }
    ],
    attrs: {
   
   
      wrap: {
   
   
        connection: true,
        strokeWidth: 10,
        strokeLinejoin: 'round',
        style: {
   
   
          animation: 'animation-line 30s infinite linear'
        }
      },
      line: {
   
   
        connection: true,
        stroke: '#7eb2fb',
        strokeLinejoin: 'round',
        targetMarker: 'classic',
        style: {
   
   
          // animation: 'animation-line 30s infinite linear'
        }
      },
      arrow: {
   
   
        // d: 'M 0 2 2 1 4 0 2 -1 0 -2 -14 -1 -14 0 -14 1 z',
        d: 'M 0 2 2 1 Q4 0 2 -1 0 -2 -24 -1 -24 0 -24 1 z',
        fill: {
   
   
          type: 'linearGradient',
          stops: [
            {
   
    offset: '0%', color: 'rgba(73, 131, 236,0.7)' },
            {
   
    offset: '30%', color: '#4983EC' },
            {
   
    offset: '100%', color: '#FFFFFF' }
          ]
        },
        filter: {
   
   
          name: 'dropShadow',
          args: {
   
   
            color: '#4983EC',
            dx: 0,
            dy: 0,
            blur: 2,
            opacity: 0.7
          }
        }
<think> 在 AntV X6实现连线动画,可通过配置边的 **连接器(connector)** 和 **动画(animation)** 属性实现。以下是两种主要方式: ### 方案一:流动动画(沿路径移动) ```javascript import { Shape } from '@antv/x6'; // 创建带流动动画的边 graph.addEdge({ source: { cell: 'node1' }, target: { cell: 'node2' }, connector: { name: 'smooth' }, // 平滑曲线连接器 attrs: { line: { stroke: '#3498db', strokeWidth: 2, targetMarker: { name: 'block' } } }, // 关键:添加流动动画 router: { name: 'manhattan' }, // 推荐使用有路由的路径 connector: 'smooth', // 平滑曲线 animation: { enabled: true, // 启用动画 duration: 3000, // 动画时长(ms) repeat: true, // 循环播放 bounce: { // 弹性效果(可选) duration: 1000, count: 10 }, // 自定义动画函数 easing: 'cubic-bezier(0.215, 0.61, 0.355, 1)' } }); ``` ### 方案二:SVG 虚线位移(蚂蚁线效果) ```javascript graph.addEdge({ // ...其他配置 attrs: { line: { stroke: '#52c41a', strokeDasharray: '5, 5', // 虚线样式 strokeWidth: 2, } }, // 虚线动画 animation: { type: 'dash', // 指定为虚线流动动画 duration: 6000, repeat: true, easing: 'linear' } }); ``` ### 高级定制:自定义动画 通过 `edge.animate()` 方法动态控制: ```javascript const edge = graph.addEdge({...}) // 手动触发路径流动动画 edge.animate('attrs/line/stroke', { values: ['transparent', '#ff0000', '#0000ff'], // 颜色变化 duration: 2000, repeat: true }); // 使用CSS动画 edge.animate(null, { delay: 500, duration: 1000, easing: 'easeOutCubic', timing: (time) => time, // 自定义时间函数 // 自定义SVG变换 morph: true, translate: { x: 10, y: 10 } }); ``` > **注意**: > 1. `animation.type` 支持 `'stroke'`(路径流动)和 `'dash'`(虚线流动),默认 `'stroke'` > 2. 动画性能受路径复杂度影响,长路径建议降低 `duration` 值 §§相关问题§§ 1. 如何实现双向箭头连线及其动画效果? 2. 连线动画过程中如何添加自定义事件(如点击暂停)? 3. AntV X6 是否支持贝塞尔曲线路径的动画? 4. 如何根据连线状态(如传输数据量)动态调整动画速度? 5. 怎样在节点移动时保持连线动画的连续性? </think> 在 AntV X6实现连线动画,可通过内置动画配置或自定义 SVG 动画实现。以下是三种核心方案: ### 方案一:内置流动动画(路径描边) ```javascript graph.addEdge({ source: 'node1', target: 'node2', attrs: { line: { stroke: '#1890ff', strokeWidth: 2, targetMarker: { name: 'block' } } }, // 关键动画配置 router: { name: 'manhattan' }, // 确保路径非直线 animation: { enabled: true, // 启用动画 duration: 3000, // 动画时长(ms) repeat: true, // 循环播放 reverse: false, // 是否反向流动 easing: 'linear' // 速度曲线 } }); ``` ### 方案二:虚线位移动画(蚂蚁线效果) ```javascript graph.addEdge({ // ...其他参数 attrs: { line: { stroke: '#ff4d4f', strokeDasharray: '5 3', // 虚线模式 } }, animation: { type: 'dash', // 指定虚线动画类型 duration: 2000, repeat: true, stroke: true // 同时保留描边动画 } }); ``` ### 方案三:高级自定义动画(SVG 变换) ```javascript const edge = graph.addEdge({...}) // 1. 颜色渐变动画 edge.transition('attrs/line/stroke', { duration: 1500, repeat: Infinity, values: ['#722ed1', '#13c2c2', '#fa8c16'] // 颜色循环 }); // 2. 路径形变动画(节点移动时) edge.on('change:vertices', () => { edge.animate(null, { duration: 800, easing: 'easeOutElastic', // 弹性效果 morph: true // 启用路径形变 }); }); // 3. 自定义 SVG 动画元素 edge.appendLabel({ attrs: { text: { text: '●' } }, animation: { duration: 1000, translate: { x: '100%', y: 0 }, // 沿路径移动 repeat: true } }); ``` ### 注意事项: 1. **性能优化**: - 复杂动画使用 `easing: 'linear'` 减少计算 - 超过 50 条动画连线时启用 `batch: true` 2. **动画控制**: ```javascript edge.stopAnimation() // 停止动画 edge.startAnimation() // 重启动画 graph.animateEdges({ duration: 0 }) // 暂停所有连线动画 ``` 3. Canvas 渲染器需额外配置: ```javascript new Graph({ rendering: true }) // 启用 Canvas 动画层 ``` > 提示:使用 `metro` 路由器的曲线路径可获得更流畅的动画效果,避免直线无动画问题。
评论 16
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值