RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 914678
Accepted
Arthur
Arthur
Asked:2020-12-03 04:41:09 +0000 UTC2020-12-03 04:41:09 +0000 UTC 2020-12-03 04:41:09 +0000 UTC

将对象返回到其先前位置 SVG

  • 772

我有一个我正在制作动画的对象animateMotion。单击动画停止按钮后,stop对象应该返回到之前没有相同动画时会渲染的位置,在这种情况下x& y= 0,但是当动画停止时,对象会保留它的位置,这实际上不该是。也许我试图错误地关闭动画,我正在更改xlink:href接受沿路径移动的对象的属性 ( ),即下例中的正方形。也许有一些内置函数SVG,我非常怀疑,我想听听关于这个的易懂的答案。

最小的例子:

document.querySelector('button').onclick = () => {
  document.querySelector('#AM').setAttribute('xlink:href', '  ');
};
<div class="wrapper">
  <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="border: 0.0625rem solid">
    <g class="main-area">
      <path d="M0 75L300 75" fill="none" stroke="red" id="AMP"/>
      <rect x="0" y="0" width="50" height="50" id="TrO" transform="translate(0 -25)"/>
      <animateMotion id="AM" xlink:href="#TrO" begin="0s" dur="3s" repeatCount="indefinite">
        <mpath xlink:href="#AMP"/>
      </animateMotion>
    </g>
  </svg>
  <button>Stop</button>
</div>

javascript
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Stepan Kasyanenko
    2020-12-03T13:03:47Z2020-12-03T13:03:47Z

    当然,这不是最好的选择,但它确实发挥了作用。

    document.querySelector('button').onclick = () => {
      const am = document.querySelector('#AM');
      am.querySelector('mpath').setAttribute('xlink:href', '#AMP1');
      am.setAttribute('repeatCount', '0');
    };
    <div class="wrapper">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="border: 0.0625rem solid">
        <g class="main-area">
          <path d="M0 75L300 75" fill="none" stroke="red" id="AMP"/>
          <path d="M0 75" fill="none" stroke="red" id="AMP1"/>
          <rect x="0" y="0" width="50" height="50" id="TrO" transform="translate(0 -25)"/>
          <animateMotion id="AM" xlink:href="#TrO" begin="0s" dur="3s" repeatCount="indefinite" fill='freeze'>
            <mpath xlink:href="#AMP"/>
          </animateMotion>
        </g>
      </svg>
      <button>Stop</button>
    </div>

    还有一种特殊的方法endElement()可以停止动画。

    document.querySelector('button').onclick = () => {
      document.querySelector('#AM').endElement();
    };
    <div class="wrapper">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="border: 0.0625rem solid">
        <g class="main-area">
          <path d="M0 75L300 75" fill="none" stroke="red" id="AMP"/>
          <path d="M0 75" fill="none" stroke="red" id="AMP1"/>
          <rect x="0" y="0" width="50" height="50" id="TrO" transform="translate(0 -25)"/>
          <animateMotion id="AM" xlink:href="#TrO" begin="0s" dur="3s" repeatCount="indefinite" fill='remove'>
            <mpath xlink:href="#AMP"/>
          </animateMotion>
        </g>
      </svg>
      <button>Stop</button>
    </div>

    • 6
  2. Alexandr_TT
    2020-12-03T13:46:30Z2020-12-03T13:46:30Z

    纯 SVG

    您可以尝试另一种动画,其中“x”属性,即矩形左上角的坐标,将进行动画处理。

    <animate id="AM" attributeName="x" values="0;300" begin="play.click" end="stop.click" dur="3s" repeatCount="indefinite">  
    

    Play可以根据任务移除按钮。

    <div class="wrapper">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="border: 0.0625rem solid">
      
    <polyline points="0,75 300,75" stroke="crimson" />
     <g class="main-area">
      <rect x="0" y="50" width="50" height="50" id="TrO" >
       <animate id="AM" 
        attributeName="x" 
        values="0;300"
        begin="play.click" 
        end="stop.click" 
        dur="3s"
        repeatCount="indefinite">
       </animate>
      </rect>
     </g>
       <g id="stop">
         <rect id="btn1" 
          x="240" y="120" 
          width="50" height="20"
          fill="#d3d3d3" 
          stroke="black"/>
    	<text x="250" y="135" font-size="16">Stop</text>
       </g>
    	<g id="play">
    	 <rect id="btn2"
              x="180" y="120"
              width="50" height="20"
              fill="#d3d3d3" 
              stroke="black"/>
    	    <text x="190" y="135" font-size="16">Play</text>
    	</g>
      
      </svg>
      </div>

    带动画的选项animateMotion

    <div class="wrapper">
      <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="border: 0.0625rem solid">
        <g class="main-area">
          <path d="M0 75L300 75" fill="none" stroke="red" id="AMP"/>
            <rect id="TrO"
             transform="translate(0 -25)"
              x="0" y="0" 
              width="50"
             height="50" />
          <animateMotion id="AM" 
              xlink:href="#TrO" 
    	  begin="play.click" 
    	  end="stop.click" 
    	  dur="3s" 
    	  repeatCount="indefinite">
            <mpath xlink:href="#AMP"/>
          </animateMotion>
        </g>
        
    	<g id="stop">
    	  <rect id="btn1" 
    	   x="240" y="120" 
               width="50" height="20" 
    	  fill="#d3d3d3" 
    	stroke="black"/>
    	<text x="250" y="135" font-size="16">Stop</text>
    	</g>
    	  <g id="play">
           	<rect id="btn2" 
    	      x="180" y="120" 
    	      width="50"
                  height="20" 
                 fill="#d3d3d3"
                 stroke="black"/>
    	<text x="190" y="135" font-size="16">Play</text>
    	</g>
    	</svg>
    
    </div> 

    更新 9.12.2018

    以 CSS 规则样式格式化 SVG 代码,以便于感知

    • 5

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5