javascript - 在 SVG 中显示流运动的最佳方式?

标签 javascript html svg inkscape

我想创建一个网页来显示流动的流体之类的东西。为此,我想使用 SVG 图形,其中(重复)运动的开始和停止是通过 JavaScript 控制的。

这个 Action 可以很容易地通过手绘 GIF 的方式显示:
enter image description here

但是我怎样才能通过简单的方法在 SVG 中实现这样的外观呢?特别是因为它还必须绕过拐 Angular ,即不仅需要线性运动...

最好已经(半自动)在 Inkscape 中创建...

最佳答案

好的,现在我找到了问题“第一”部分的答案,即上层“流”:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   version="1.1"
   width="202"
   height="32"
   id="svg2">
  <g id="layer1">
    <path
       d="m 1,16 200,0"
       id="path1"
       style="stroke:#000000;stroke-width:30" />
    <path
       d="m 1,16 200,0"
       id="path2"
       style="stroke:#ff0000;stroke-width:20" />
    <path
       d="m 1,16 200,0"
       id="path3"
       style="stroke:#000000;stroke-width:16;stroke-dasharray:48, 48;stroke-dashoffset:10.6" />
  </g>
</svg>

这是在 Inkscape 中创建的(+ 之后手动简化以仅发布相关内容)只是通过复制一条具有不同宽度的线,一个非常大的线( id=path1 )为周围的黑色,一个大的线( id=path2 )为红色液体和一个小的虚线液体 (id=path3),稍后将用于动画。

现在剩下要做的就是通过 JavaScript 或 CSS 动画更改 stroke-dashoffset 属性,因为这会移动小条以指示流。例如:

   <style type="text/css">  
      @keyframes move {  
        from {  stroke-dashoffset: 0;  }  
        to   {  stroke-dashoffset: 96;  }  
      }  
      @-moz-keyframes move {  
        from {  stroke-dashoffset: 0;  }  
        to   {  stroke-dashoffset: 96;  }  
      }  
      @-webkit-keyframes move {  
        from {  stroke-dashoffset: 0;  }  
        to   {  stroke-dashoffset: 96;  }  
      }  
  </style>  

然后在 <path id="path3"> 元素中:

  animation-duration: 3s;  
  -moz-animation-duration: 3s;  
  -webkit-animation-duration: 3s;  
  animation-name: move; 
  -moz-animation-name: move; 
  -webkit-animation-name: move; 
  animation-timing-function: linear; 
  -moz-animation-timing-function: linear; 
  -webkit-animation-timing-function: linear; 
  animation-iteration-count: infinite;
  -moz-animation-iteration-count: infinite;
  -webkit-animation-iteration-count: infinite; 

注意:路径可以是任何形状,不需要是直的:)

下流:
通过使用 http://owl3d.com/svg/tubefy/articles/article3.html 的想法,我还找到了“较低流量”的解决方案(更好:解决方法)。这个想法只是多次克隆路径并使用彼此绘制的不同颜色的破折号。动画如上。现在可以在以下位置看到两者:http://jsfiddle.net/pXkvD/2

关于javascript - 在 SVG 中显示流运动的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11065334/

相关文章:

IE8 中的Javascript/CSS

javascript - 带 if 语句的正则表达式

javascript - jquery 动画的替代品?

jQuery fadeIn 不适用于 Z-index

html - SVG 网格(图案)在 Firefox 中行为不当

javascript - 如何使用 javascript 获得 mix-blend-mode 滚动效果?

JavaScript 变量到 ColdFusion 变量

javascript - 选择任何一个单选按钮后,单选按钮选项卡焦点无法正常工作

PHP分隔字符串的各个部分

javascript - 使用带有 CSS 模块的 ReactJS 对 SVG 元素进行样式化