項目需求是機器人在辦公室移動了一段軌跡,需要將軌跡以動畫的形式展示出來。
首先使用了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';
// 設(shè)置起始點
path.style.strokeDasharray = length + ' ' + length;
path.style.strokeDashoffset = length;
// 獲取一個區(qū)域,獲取相關(guān)的樣式,讓瀏覽器尋找一個起始點。
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();
}