我正在使用 WebGL 的 Globe API 及其旋转动画功能,但我希望用户能够通过双击地球来停止和重新启动动画。
目前我有以下运行onload的动画代码:
function performAnimation() {
requestAnimationFrame(function animate(now) {
if (animated) {
var c = earth.getPosition();
var elapsed = before? now - before: 0;
before = now;
earth.setCenter([c[0], c[1] + 0.1*(elapsed/30)]);
requestAnimationFrame(animate);
}
});
}
以下代码在双击时停止并重新启动动画:
performAnimation()
var animated = true;
var touchtime = 0;
$('#globe').on('click', function() {
if(touchtime == 0) {
touchtime = new Date().getTime();
} else {
if(((new Date().getTime()) - touchtime) < 200) {
if (animated) {
animated = false;
} else {
animated = true;
performAnimation()
}
console.log(animated);
//alert(animated);
touchtime = 0;
} else {
touchtime = 0;
}
}
});
停止和停止在双击时起作用。问题是当动画停止时,时间仍然过去,当动画重新开始时,地球的中心会立即移动到它没有停止时的位置。
如何确保当地球动画重新启动时,它从当前中心而不是假设中心重新启动?