关于如何制作svg动画。

今日发现有同事做一个动画,是把svg放在div里面,然后给div加动画,来做一个loading的效果。 然后这样的话,在重复使用上,应用起来会很麻烦,所以研究了一下在svg里面加动画效果。

1.animateTransform

这个属性是可以用来设置svg动画的。 里面有一个form 和 to 属性。 第一个参数是代表转动的幅度。 第二个和第三个参数是用来代表转动的中心点位置。

<svg width="41" height="41" viewBox="0 0 41 41" fill="none" xmlns="http://www.w3.org/2000/svg">
	<circle cx="20.165" cy="20.165" r="20.165" fill="#F8BBD0"/>
	<path fill-rule="evenodd" clip-rule="evenodd" d="M12 20C12 15.5817 15.5817 12 20 12C24.4183 12 28 15.5817 28 20C28 24.4183 24.4183 28 20 28C15.5817 28 12 24.4183 12 20ZM26.7694 20C26.7694 16.2615 23.7387 13.2308 20.0002 13.2308C16.2616 13.2308 13.231 16.2615 13.231 20C13.231 23.7386 16.2616 26.7693 20.0002 26.7693C23.7387 26.7693 26.7694 23.7386 26.7694 20Z" fill="white"/>
	<path fill-rule="evenodd" clip-rule="evenodd" d="M15.0781 13.6925C16.4354 12.632 18.1437 12 19.9997 12C21.8553 12 23.5634 12.6318 24.9206 13.692L24.078 14.5967C22.9438 13.7393 21.5312 13.2308 19.9999 13.2308C18.4681 13.2308 17.0552 13.7396 15.9208 14.5973L15.0781 13.6925Z" fill="#37519B">
	</path>
</svg>

如果我们要给上面的这个svg 加一个转动的动画。我们可以在第二个path里面加一个animateTransform,让它围绕着这张图的中心点来做360度的旋转。

<svg width="41" height="41" viewBox="0 0 41 41" fill="none" xmlns="http://www.w3.org/2000/svg">
	<circle cx="20.165" cy="20.165" r="20.165" fill="#F8BBD0"/>
	<path fill-rule="evenodd" clip-rule="evenodd" d="M12 20C12 15.5817 15.5817 12 20 12C24.4183 12 28 15.5817 28 20C28 24.4183 24.4183 28 20 28C15.5817 28 12 24.4183 12 20ZM26.7694 20C26.7694 16.2615 23.7387 13.2308 20.0002 13.2308C16.2616 13.2308 13.231 16.2615 13.231 20C13.231 23.7386 16.2616 26.7693 20.0002 26.7693C23.7387 26.7693 26.7694 23.7386 26.7694 20Z" fill="white"/>
	<path fill-rule="evenodd" clip-rule="evenodd" d="M15.0781 13.6925C16.4354 12.632 18.1437 12 19.9997 12C21.8553 12 23.5634 12.6318 24.9206 13.692L24.078 14.5967C22.9438 13.7393 21.5312 13.2308 19.9999 13.2308C18.4681 13.2308 17.0552 13.7396 15.9208 14.5973L15.0781 13.6925Z" fill="#37519B">
		<animateTransform 
		 attributeName="transform" 
		 attributeType="XML" 
		 type="rotate"
		 dur="1s" 
		 from="0 20 20"
		 to="360 20 20" 
		 repeatCount="indefinite" />
	</path>
</svg>

加上这个代码就可以让这个spinner转动起来。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 SVG 动画图片的代码示例,您可以根据您的需求进行修改: ```html <svg width="200" height="200"> <rect x="10" y="10" width="30" height="30" fill="#0077be"> <animate attributeName="x" from="10" to="150" dur="1s" repeatCount="indefinite"></animate> </rect> <circle cx="100" cy="100" r="30" fill="#00ffea"> <animate attributeName="r" from="0" to="50" dur="1s" repeatCount="indefinite"></animate> </circle> <polygon points="130,10 170,40 130,70 90,40" fill="#ffcc00"> <animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0 100 100" to="360 100 100" dur="2s" repeatCount="indefinite"></animateTransform> </polygon> </svg> ``` 解释一下上面代码的含义: - `<rect>` 标签用于定义一个矩形,其中 `x` 和 `y` 属性定义了矩形左上角的坐标,`width` 和 `height` 属性定义了矩形的宽度和高度。`fill` 属性定义了矩形的填充颜色。`<animate>` 标签用于定义动画,其中 `attributeName` 属性定义了要进行动画的属性,这里设置为 `x`。`from` 属性定义了动画的起始值,这里设置为 `10`。`to` 属性定义了动画的结束值,这里设置为 `150`。`dur` 属性定义了动画的持续时间,这里设置为 `1s`。`repeatCount` 属性定义了动画的重复次数,这里设置为 `indefinite`,表示无限循环。 - `<circle>` 标签用于定义一个圆形,其中 `cx` 和 `cy` 属性定义了圆心的坐标,`r` 属性定义了圆的半径。`fill` 属性定义了圆的填充颜色。`<animate>` 标签用于定义动画,其中 `attributeName` 属性定义了要进行动画的属性,这里设置为 `r`。`from` 属性定义了动画的起始值,这里设置为 `0`。`to` 属性定义了动画的结束值,这里设置为 `50`。`dur` 属性定义了动画的持续时间,这里设置为 `1s`。`repeatCount` 属性定义了动画的重复次数,这里设置为 `indefinite`,表示无限循环。 - `<polygon>` 标签用于定义一个多边形,其中 `points` 属性定义了多边形各个顶点的坐标。`fill` 属性定义了多边形的填充颜色。`<animateTransform>` 标签用于定义动画,其中 `attributeName` 属性定义了要进行动画的属性,这里设置为 `transform`,表示变换矩阵。`attributeType` 属性定义了属性类型,这里设置为 `XML`。`type` 属性定义了变换类型,这里设置为 `rotate`,表示旋转变换。`from` 属性定义了动画的起始值,这里设置为 `0 100 100`,表示从原始状态开始旋转。`to` 属性定义了动画的结束值,这里设置为 `360 100 100`,表示旋转一周。`dur` 属性定义了动画的持续时间,这里设置为 `2s`。`repeatCount` 属性定义了动画的重复次数,这里设置为 `indefinite`,表示无限循环。 您可以根据需要修改上述代码中的形状、颜色、动画时间等属性,以达到您想要的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值