【发布时间】:2019-08-02 09:20:43
【问题描述】:
我正在尝试使用 MapBoxGL 为相机旋转设置动画,同时提供暂停旋转并使用复选框回调重新启动旋转的选项。 “暂停”/“停止”旋转工作正常,但“重新启动”似乎会在动画从未暂停的情况下拾取它应该在的位置,而不是拾取动画停止的位置。
var animation;
function rotateCamera(timestamp) {
map.rotateTo((timestamp / 600) % 360, {duration: 0});
animation = requestAnimationFrame(rotateCamera);
}
当地图加载时,动画被调用:
animation = rotateCamera(0);
回调如下所示:
d3.selectAll("input[name='camerarotation-selection']").on("change", function() {
if (d3.select("input[name='selection']").property("checked")) {
rotateCamera(map.getBearing());
} else {
cancelAnimationFrame(animation);
}
});
如果我 console.log rotateCamera 函数内的时间戳变量,我可以看到尽管有 cancelAnimationFrame 调用,它仍会继续递增。我曾尝试在重新启动时声明 animation 未定义,但这似乎也不起作用。我难住了!感谢您的帮助。
【问题讨论】:
-
请提供minimal reproducible example。您是说您的
rotateCamera循环在您更改输入值后仍在运行?你能在this fiddle 中看到这种行为吗? -
@Kaiido 哇,是这样的。谢谢,你在这方面比我好得多。所以是的,在我的例子中,
map.rotateTo()停止运行,但内部的timestamp不断增加,就像在你的小提琴中一样。
标签: javascript requestanimationframe