項(xiàng)目需求是機(jī)器人在辦公室移動(dòng)了一段軌跡,需要將軌跡以動(dòng)畫的形式展示出來。
首先使用了path畫軌跡,animateMotion做機(jī)器人移動(dòng)。
問題:每次軌跡重繪或更新時(shí),animateMotion移動(dòng)的軌跡不對
解決方案:
- 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軌跡動(dòng)畫
- html
<path :d="pathd" class="bg_path trail_1"></path>
- js
let path = document.querySelector('.trail_1')
let length = path.getTotalLength();
// 清除之前的動(dòng)作
path.style.transition = path.style.WebkitTransition = 'none';
// 設(shè)置起始點(diǎn)
path.style.strokeDasharray = length + ' ' + length;
path.style.strokeDashoffset = length;
// 獲取一個(gè)區(qū)域,獲取相關(guān)的樣式,讓瀏覽器尋找一個(gè)起始點(diǎn)。
path.getBoundingClientRect();
// 定義動(dòng)作
path.style.transition = path.style.WebkitTransition = `stroke-dashoffset ${this.dur}s linear`
// Go!
path.style.strokeDashoffset = '0';
補(bǔ)充
停止,啟動(dòng)。機(jī)器人動(dòng)畫
<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();
}