SVG SMIL 轨迹 Path 动画(animateMotion)

本文介绍了一个使用SVG(可缩放矢量图形)实现动画路径的例子。通过定义特定的路径并结合animateMotion元素,实现了矩形沿指定路径移动的动画效果。此示例展示了如何设置动画属性,如duration、重复次数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 效果

    temp.gif

  • 代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
        html,body {
          margin: 0;
          padding: 0;
          background-color: #001122;
          /* 全屏 */
          width: 100%;
          height: 100%;
          /* 清空字号,免得影响 svg */
          line-height: 0;
          font-size: 0;
        }
      </style>
    </head>
    <body>
      <!-- animateMotion
        attributeType: 标签类型
        attributeName: 需要支持动画的属性
        type: 子属性
        begin: 动画开始时机
        dur: 动画时间
        fill: freeze(停留在最后)、none(默认,回到起点)
        repeatCount: 动画次数,indefinite(无限次),默认 1
        path: 运行轨迹
        rotate: auto(自动根据轨迹调整好角度)、none(默认,固定角度)
        -->
      <!-- svg -->
      <svg width="100%" height="100%" viewBox="-400 -400 800 800">
        <!-- 需要执行动画的元素 -->
        <rect x="0" y="0" width="40" height="40" fill="rgba(0, 255, 255, 0.5)">
    
          <!-- 方式一 -->
          <!-- <animateMotion
            path="M 0 0 L 100 100 A 200 200 0 1 0 0 -100"
            dur="3s"
            rotate="auto"/> -->
    
          <!-- 方式二 -->
          <animateMotion
            dur="3s"
            rotate="auto">
            <mpath xlink:href="#motion-path"/>
          </animateMotion>
        </rect>
        <!-- 轨迹 -->
        <path id="motion-path" d="M 0 0 L 100 100 A 200 200 0 1 0 0 -100" fill="none" stroke="green"/>
      </svg>
    </body>
    </html>
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡尔特斯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值