我花了很长时间试图弄清楚如何使用具有CSS样式缓动功能的Google Maps API在多义线上设置符号动画。我让它工作with this Gist和this Google Maps API example。现在,我尝试每5秒运行一次setInterval(myFunc, 10)
。这是相关的代码段:
function animateCircle(line) {
var count = 0;
let refreshRate = 10;
let countFunc = EasingFunctions.easeInOutCubic;
let perc = 0
function moveCircle() {
count = count < 1 ? (count + 0.005) : 0;
perc = countFunc(count) * 100 <= 100 ? countFunc(count) * 100 : countFunc(count) * 100 - 100
perc = perc >= 0 ? perc : 100 + perc
perc === 0 ? window.clearInterval(moveCircInterval) : ''
// console.log(count + " // " + perc)
var icons = line.get('icons');
icons[0].offset = perc + '%';
line.set('icons', icons);
}
var moveCircInterval = window.setInterval(moveCircle, refreshRate);
window.setInterval(() => moveCircInterval = window.setInterval(moveCircle, refreshRate), 5000)
}
还有一个full JSFiddle to see it in action。
此代码不是很好,但是几乎有效。就我而言,它会随着时间的流逝而加速,尤其是如果您离开标签页然后返回。
答案 0 :(得分:2)
如果我正确理解了您的担忧,我们可以像下面那样调整您的代码:
function animateCircle(line) {
var count = 0;
let refreshRate = 10;
let countFunc = EasingFunctions.easeInOutCubic;
let perc = 0
function moveCircle() {
count = count < 1 ? (count + 0.005) : 0;
perc = countFunc(count) * 100 <= 100 ? countFunc(count) * 100 : countFunc(count) * 100 - 100
perc = perc >= 0 ? perc : 100 + perc
if (perc === 0) {
clearInterval(moveCircInterval);
setTimeout(() => {
moveCircInterval = setInterval(moveCircle, refreshRate);
}, 5000);
}
var icons = line.get('icons');
icons[0].offset = perc + '%';
line.set('icons', icons);
}
var moveCircInterval = setInterval(moveCircle, refreshRate);
}
请尝试一下,让我知道它是否适合您!
答案 1 :(得分:0)
我最终这样做:
function animateCircle(line) {
var remove = window.setInterval(function() {
var count = 0;
let refreshRate = 20;
let countFunc = EasingFunctions.easeInOutQuint;
let perc = 0
var now, before = new Date();
var move = window.setInterval(function() {
now = new Date();
var elapsedTime = (now.getTime() - before.getTime());
var icons = line.get('icons');
if (elapsedTime > refreshRate + 5000) {
// reset if navigate away from tab
count = 0.005
window.clearInterval(move)
} else {
count = count < 1 ? (count + 0.005) : 0;
}
perc = countFunc(count) * 100 <= 100 ? countFunc(count) * 100 : countFunc(count) * 100 - 100
perc = perc >= 0 ? perc : 100 + perc
perc === 0 || perc === 100 ? (window.clearInterval(move)) : ''
icons[0].icon.path = svgTrans(perc)
icons[0].offset = perc + '%';
line.set('icons', icons);
}, refreshRate)
}, 5000)
}