animateMotion使用脚本控制

项目需求是机器人在办公室移动了一段轨迹,需要将轨迹以动画的形式展示出来。

首先使用了path画轨迹,animateMotion做机器人移动。

问题:每次轨迹重绘或更新时,animateMotion移动的轨迹不对
解决方案:

  • html
 <rect x="-7" y="-7.5" width="14" height="15" fill="url(#robot_icon)" v-show="!stopAani" >
              <animateMotion :path="pathd" begin="0.5s" :dur="dur+'s'" v-if="pathd" repeatCount="indefinite" id="animate_motion" />  
            </rect>
  • js
document.getElementById("animate_motion").beginElement
path轨迹动画
  • html
<path :d="pathd" class="bg_path trail_1"></path> 
  • js
let path = document.querySelector('.trail_1')
 let length = path.getTotalLength();    
 // 清除之前的动作
 path.style.transition = path.style.WebkitTransition = 'none';
 // 设置起始点
path.style.strokeDasharray =  length + ' ' + length;
path.style.strokeDashoffset = length;
// 获取一个区域,获取相关的样式,让浏览器寻找一个起始点。
path.getBoundingClientRect();
 // 定义动作
path.style.transition = path.style.WebkitTransition = `stroke-dashoffset ${this.dur}s linear`
 // Go!
path.style.strokeDashoffset = '0';  
补充

停止,启动。机器人动画

<svg id="svg_an">
        <defs>
          <pattern id="robot_icon" width="100%" height="100%" patternContentUnits="objectBoundingBox">
            <image width="1" height="1" xlink:href="static/images/robot/jqr.png"/>
          </pattern>  
        </defs> 

 <rect x="-7" y="-7.5" width="14" height="15" fill="url(#robot_icon)" v-show="!stopAani" >
              <animateMotion :path="pathd" begin="0.5s" :dur="dur+'s'" v-if="pathd" repeatCount="indefinite" id="animate_motion" />  
            </rect>

<svg>
var pause = document.getElementById('svg_an'); 
var unPause = document.getElementById('svg_an'); 

function parar(){ 
    pause.pauseAnimations(); 
} 
function voltar(){ 
    unPause.unpauseAnimations(); 
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在前面一篇博客,介绍了 Selenium 的基本用法和爬虫开发过程中经常使用的一些小技巧,利用这些写出一个浏览器爬...
    FifiZhuang阅读 13,794评论 4 81
  • 网络营销名词大全 二、数据分析 DA:(Digital Marketing Analytics)就是数字营销分析。...
    专业摸鱼阅读 4,803评论 0 0
  • 实际工作中,SVG大多数是用 +<defs></defs>(或者symbol)+ + 的组合来使用的,defs ...
    风之化身呀阅读 12,018评论 1 4
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,444评论 1 45
  • 引用CSS方式 内部引用 html文件中写一个 标签,并将样式写入到里面,举例: 外部引用 通过 标签实现,里面有...
    dawsonenjoy阅读 3,514评论 0 0

友情链接更多精彩内容