人物行迹图/SVG的描点动画

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 10px;
        height: 10px;
        background: red;
        border-radius: 50%;
        offset-path: path(
          "M 200 100 Q 300 50 400 100 Q 450 150 400 200 Q 200 200 200 200"
        );
        animation: move 5s linear;
        animation-fill-mode: forwards;
      }

      @keyframes move {
        100% {
          offset-distance: 100%;
        }
      }

      svg {
        width: 500px;
        height: 500px;
      }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <svg>
      <path
        d="M 200 100 Q 300 50 400 100 Q 450 150 400 200 Q 200 200 200 200"
        stroke="green"
        stroke-width="2"
        fill="none"
      />
    </svg>
  </body>
</html>
<script>
  let num1 = false;
  let num2 = false;
  let num3 = false;
  function getPoint() {
    // 用于在浏览器中优化动画效果的函数。它会在下一次重绘之前调用指定的动画函数,以确保动画在浏览器的优化渲染周期内运行。这有助于提高动画的性能和流畅度。
    requestAnimationFrame(getPoint);
    const box = document.querySelector(".box");
    // 获取元素位置和尺寸信息,包括左边距、上边距、宽度和高度。
    const boxRect = box.getBoundingClientRect();
    // 运动到某个点时,暂停动画
    if (
      Math.floor(boxRect.left) === 400 &&
      Math.floor(boxRect.top) === 100 &&
      !num1
    ) {
      box.style.animationPlayState = "paused";
      setTimeout(() => {
        num1 = true;
        box.style.animationPlayState = "running";
      }, 3000);
    }
    if (
      Math.floor(boxRect.left) === 390 &&
      Math.floor(boxRect.top) === 203 &&
      !num2
    ) {
      box.style.animationPlayState = "paused";
      setTimeout(() => {
        num2 = true;
        box.style.animationPlayState = "running";
      }, 3000);
    }
    console.log(boxRect.left, boxRect.top);
  }

  getPoint();
</script>

这是一个用于SVG的描点动画,也用于做展示屏幕上的行迹图

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值