0.1.2 • Published 2 months ago

svg-motion-path v0.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

路径动画

基于 gsap 动画库封装的 SVG 路径动画绘制工具。更多关于路径动画的使用方法及案例,可以参考 gsap MotionPath

创建实例

<div id="svg-container"></div>
import SVGMotionPath from 'SVGMotionPath.js'
// 配置参数
const options = {
  svg: {
    width: 1920,
    height: 1080
  },
  motion: {
    // ease: 'none',
    mobile: {
      ease: 'power1.inOut',
      // durations: []
      // duration: 10,
      speed: 200,
      repeat: -1,
      yoyo: true,
      autoRotate: true,
    },
    trace: {
      duration: 1
    }
  },
  style: {
    line: {
      width: 20,
      color: 'black',
      opacity: 0.4,
      // dash: '2 4'
    },
    trace: {
      width: 2,
      color: 'white',
      // opacity: 1,
      dash: '20 40', // 配合动画使用时,传入偶数个数的参数,例如:'20 40' / '10 20 30 40'
    },
    node: {
      // images: ['xxx.png', 'yyy.png'], // 节点数量大于提供的图片数量时,则正序重复使用数组中的图片
      radius: 4,
      offsetX: 0,
      offsetY: 0,
      fill: 'gold',
      borderWidth: 2,
      borderColor: 'transparent',
    },
    mobile: {
      // image: 'xxx.png',
      size: [20, 20],
      fill: 'gold',
      borderWidth: 2,
      borderColor: 'black',
    }
  }
}
// 实例化
const motionPath = new SVGMotionPath('#svg-container', data, options)
// 事件监听
motionPath.addEventListener('pass', nodePass)
function nodePass(i) {
  ...
}
// 播放动画
motionPath.mobile.play()

数据源结构(data)

const data = [ // 支持传入坐标或 svg 路径命令,坐标原点为父容器左上角
  { x: 0, y: 0 },
  { x: 100, y: 100 },
  'M100,100 C200,200 400,200 600,100',
]

配置参数(options)

NameTypeDefaultDescription
svgobjectSVG 参数设置。
svg.widthnumber0SVG 视口宽度。
svg.heightnumber0SVG 视口高度。
motionobject路径动画。
motion.easestringnone整段运动路径动画的速率。 更多类型可参阅: https://greensock.com/docs/v3/Eases
motion.mobileobject路径上移动物体的动画。
motion.mobile.easestringnone每段运动路径动画的速率。
motion.mobile.durationsnumber[][]单独设置每一段路径的运动时长,单位 s。
motion.mobile.durationnumber运动所需的总时长,单位 s。优先级低于 durations。
motion.mobile.speednumber100运动速度,表示每秒经过的像素点。优先级低于 duration 和 durations。
motion.mobile.autoRotatebooleanfalse运动方向是否跟随路径方向同步旋转。
motion.mobile.repeatnumber0重复运动。
motion.mobile.yoyobooleanfalse往复运动。
motion.flowobject路径流的动画,默认无限重复运动。
motion.flow.durationnumber0调节路径流动画的快慢,单位 s。
styleobject路径样式。
style.lineobject线条样式。
style.line.widthnumber1线条宽度。
style.line.colorstringblack线条颜色。
style.line.opacitynumber1线条透明度。
style.line.dashstring0线条虚线,可参考 stroke-dasharray(SVG) 属性。
style.flowobject路径流样式(可理解为叠加在 line 上的另一条线),不传此参数时默认隐藏。
style.flow.widthnumber1路径流宽度。
style.flow.colorstringwhite路径流颜色。
style.flow.opacitynumber1路径流透明度。
style.flow.dashstring20 40路径流长度,如果需要配合动画使用,此参数必须为偶数,否则动画无法衔接。
style.nodeobject节点样式,整段路径的起点与终点和每段路径的连接处视为节点。
style.node.imagesstring[][]当数组不为空时,节点以数组中的图片资源重复循环渲染,默认节点隐藏。
style.node.offsetXnumber5节点在水平方向上的偏移量。
style.node.offsetYnumber5节点在垂直方向上的偏移量。
style.node.radiusnumber5节点的半径大小。
style.node.fillstringwhite节点的填充颜色。
style.node.borderColorstringblack节点的外轮廓颜色。
style.node.borderWidthnumber1节点的外轮廓宽度。
style.mobileobject路径上的移动物体样式,移动物体默认方向为从左向右。
style.mobile.imagestring图片替代移动物体,传入此参数后默认移动物体隐藏。
style.mobile.sizearray16, 16移动物体的大小。
style.mobile.fillstringwhite移动物体的填充颜色。
style.mobile.borderColornumber1移动物体的外轮廓颜色。
style.mobile.borderWidthnumber1移动物体的外轮廓宽度。

属性(attributes)

NameTypeDescription
mobileobject路径上运动物体的动画实例,动画的播放、暂停等方法暴露在此对象上。
flowobject路径流的动画实例,动画的播放、暂停等方法暴露在此对象上。
nodesHTMLElement[]路径上所有的节点元素。

方法(methods)

NameParametersDescription
addEventListener()event,function注册事件监听器。
removeEventListener()event,function注销事件监听器。
kill()实例销毁。
seek()position动画跳转到指定位置(参数为节点下标),不影响动画的播放或暂停,仅在不设置 ease 时有效。
mobile.play()from,suppressEvents动画播放。
mobile.pause()atTime,suppressEvents动画暂停。
mobile.resume()动画继续播放。
mobile.restart()includeDelay,suppressEvents动画重新播放。
mobile.reverse()from,suppressEvents动画反转。
mobile.seek()time,suppressEvents动画跳转到指定位置(参数为动画时间),不影响动画的播放或暂停。

关于更多动画的属性或方法及入参说明,可参阅 https://greensock.com/docs/v3/GSAP/Tween

事件(events)

NameParametersDescription
start移动物体动画开始播放时触发。
end移动物体动画结束播放时触发。
passindex移动物体每经过一个节点时触发,返回参数为当前经过的节点位置。
repeat移动物体动画重复播放时触发。

在 Path 实例上通过 addEventListener 方法监听某个事件类型,也可通过 removeEventListener 对事件监听进行移除。

效果图

npm.io

0.1.2

2 months ago

0.1.1

2 months ago

0.1.0

2 months ago