javascript - 如何使用 Vivus js 沿 SVG 路径为形状或图像文件设置动画?

标签 javascript svg vivus

我正在使用 vivusjs 库为 SVG 设置动画,我想为图像文件或形状沿动画 SVG 设置动画。如下所示:

enter image description here

白线是动画 SVG 路径,绿笔是我的图像文件。我在 Vivusjs 库中需要它。

最佳答案

无需库,使用原生 JavaScript 即可完成所有工作

创建您自己的 Web 组件 <draw-path> (所有现代浏览器都支持)

使用 JavaScript 制作所有动画。关键是设置pathLength为 1 并使用 getPointAtLength将 SVG 笔放置在正确的位置。

然后是显示所需的所有 HTML(参见下面的 SO 片段):

是:

<draw-path d='M25 50a25 25 0 1 1 80 0a25 25 0 1 1-80 0'></draw-path>
<draw-path d='M25 25h50v50h-50v-50z' stroke='green' stroke-width='5' speed="0.007"></draw-path>
<draw-path stroke='red' stroke-width='5' speed=".01"></draw-path>

window.customElements.define("draw-path", class extends HTMLElement {
    constructor() {
      let template = (id) => document.getElementById(id).content.cloneNode(true);
      super() // super sets and returns this scope
        .attachShadow({mode: "open"}) // sets and returns this.shadowRoot
        .append(template(this.nodeName));
      this.line = this.shadowRoot.querySelector("#line");
      this.line.setAttribute("d", this.getAttribute("d") || "m10 60c30-70 55-70 75 0s55 70 85 0");
      this.line.setAttribute("stroke", this.getAttribute("stroke") || "black");
      this.line.setAttribute("stroke-width", this.getAttribute("stroke-width") || "2");
      this.pen = this.shadowRoot.querySelector("#pen");
      this.onmouseover = (evt) => this.draw();
    }
    connectedCallback() {
      this.draw();
    }
    showpen(state = true, scale) {
      this.pen.style.display = state ? 'initial' : 'none';
    }
    draw() {
      clearInterval(this.drawing);
      this.showpen();
      this.dashoffset = 1;
      this.pathlength = this.line.getTotalLength();
      this.drawing = setInterval(() => this.update(), 50);
    }
    update() {
      this.dashoffset -= this.getAttribute("speed") || 0.02;
      let {x,y} = this.line.getPointAtLength(this.pathlength - this.dashoffset * this.pathlength);
      this.pen.setAttribute("transform", `translate(${x-2} ${y-2})`);
      this.line.style.strokeDashoffset = this.dashoffset;
      if (this.dashoffset <= 0) this.end();
    }
    end() {
      clearInterval(this.drawing);
      this.showpen(false);
      //console.log("end",this.line);
      clearTimeout(this.timeout);
      this.timeout = setTimeout(()=>this.draw(),2000);
    }
  });
<draw-path d='M25 50a25 25 0 1 1 80 0a25 25 0 1 1-80 0'></draw-path>
<draw-path d='M25 25h50v50h-50v-50z' stroke='green' stroke-width='5' speed="0.007"></draw-path>
<draw-path stroke='red' stroke-width='8' speed=".01"></draw-path>

<template id="DRAW-PATH">
  <style>
    :host {  display: inline-block }
    svg { width: 180px; height: 130px; background: beige }
  </style>
  <svg xmlns="http://www.w3.org/2000/svg">
    <path id='line' pathlength='1' stroke-dasharray='1' stroke-dashoffse='1' fill='transparent'/>
    <path id='pen' stroke='black' stroke-width='2' fill='gold' d='m12 19l7-7l3 3l-7 7l-3-3zm6-6l-2-8l-14-3l4 15l7 1l5-5zm-16-11l8 8m-1 1a2 2 0 104 0a2 2 0 10-4 0'/>
  </svg>
</template>

注意:

注意Mm路径中的(移动)创建一个新的 描边,同时绘制,而不是按顺序绘制。

所以 stroke-dash*设置同时应用。

这就是为什么在所有博客中您只会看到单笔使用的简单路径或折线。

关于javascript - 如何使用 Vivus js 沿 SVG 路径为形状或图像文件设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65975967/

相关文章:

javascript - Google map V3 - 删除所有标记

javascript - 如何隐藏 HTML iframe 中的元素?

javascript - 如何在不使用内联 SVG 的情况下重叠两个 SVG 向量?

javascript - jQuery .toggle() 不显示/隐藏 .on 'click' 内的所有内容?

javascript - 如何将 HTML Web 组件与 HTML 文件结合起来?内部 HTML = foo ("myfile.html")

javascript - 如何在 d3 js 中的曲线上绘制 svg 圆圈?

javascript - 在不改变位置的情况下缩放 svg 组

svg - 如何在 svg 中右对齐多个左对齐的 <tspan>?

javascript - vivus.js - 如何按类别选择 SVG