I made a scale animation test:
var btn = document.getElementById('testBtn');
var stopBtn = document.getElementById('testStop');
var runner;
var startSize = 20,
endSize = 80;
const start = startSize === 0 ? 0.001 : startSize / 100;
const end = (endSize / 100) * (1 / start);
var x = 0,
y = 0,
width = 50,
height = 50;
const endss = end * start;
let over = false;
const rect = document.getElementById('svg_1');
const svgEle = SVG(rect);
btn.addEventListener('click', () => {
runner = svgEle.animate().opacity(0).animate().scale(start, start, x, y + height / 2)
.animate().opacity(1)
.animate({
duration: 1500
}).scale(end, end, x, y + height / 2);
})
stopBtn.addEventListener('click', () => {
svgEle.timeline().finish();
setTimeout(() => {
rect.removeAttribute('transform');
}, 200);
})
<script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@3.0.10/dist/svg.min.js"></script>
<svg width="640" height="480" style="background: lightblue">
<rect width="50" x="0" y="0" height="50" fill="#ff0000" id="svg_1"></rect>
</svg>
<div>
<button id="testBtn">start</button>
</div>
<div style="margin-top: 30px;">
<button id="testStop">restore</button>
</div>
that's what I say. when I click 'start' button, the rect scale from 20% to 80%, when I click 'restore' button, restore the rect scale to 1, but when I click 'start' again , the rect become smaller than before, ..... ... loop the operates, the rect become more and more small.
so, why the animation still depends on last's result. how to destory the last animation and start a new scale animation.