我正在使用Three.js与WebGL渲染器制作一个游戏,当点击“play”链接时,全屏显示。为了动画效果,我使用requestAnimationFrame。
我这样启动它:
当我想要时,我会调用
设置基本上像这样:
- 页面上有一个
我相信我的其他代码都没问题,而我可能以一种错误的方式停止了
这是版本,其中我不尝试调用动画方法,并且全屏进出正常工作:http://banehq.com/Correct/Placeholdername/main.html。
一旦第一次单击
以下是它的外观:
这个版本和正常工作的版本唯一的区别是,在
我这样启动它:
self.animate = function()
{
self.camera.lookAt(self.scene.position);
self.renderer.render(self.scene, self.camera);
if (self.willAnimate)
window.requestAnimationFrame(self.animate, self.renderer.domElement);
}
self.startAnimating = function()
{
self.willAnimate = true;
self.animate();
}
self.stopAnimating = function()
{
self.willAnimate = false;
}
当我想要时,我会调用
startAnimating
方法,它确实按预期工作。但是,当我调用 stopAnimating
函数时,出现问题!尽管没有报告任何错误...设置基本上像这样:
- 页面上有一个
播放
链接
- 一旦用户单击链接,渲染器的domElement
应全屏显示,并且实际上确实全屏了
- 调用 startAnimating
方法,渲染器开始渲染内容
- 一旦按下ESC键,我注册一个 fullscreenchange
事件并执行 stopAnimating
方法
- 页面尝试退出全屏,成功退出,但整个文档完全空白我相信我的其他代码都没问题,而我可能以一种错误的方式停止了
requestAnimationFrame
。我的解释可能很糟糕,因此我将代码上传到了我的网站,你可以在这里看到它的运行情况:http://banehq.com/Placeholdername/main.html。这是版本,其中我不尝试调用动画方法,并且全屏进出正常工作:http://banehq.com/Correct/Placeholdername/main.html。
一旦第一次单击
play
,游戏就会初始化并执行其 start
方法。一旦全屏退出,游戏的 stop
方法就会执行。每次单击 play
,游戏只会执行其 start
方法,因为没有必要再次初始化。以下是它的外观:
var playLinkHasBeenClicked = function()
{
if (!started)
{
started = true;
game = new Game(container); //"container" is an empty div
}
game.start();
}
以下是start
和stop
方法的样例代码:
self.start = function()
{
self.container.appendChild(game.renderer.domElement); //Add the renderer's domElement to an empty div
THREEx.FullScreen.request(self.container); //Request fullscreen on the div
self.renderer.setSize(screen.width, screen.height); //Adjust screensize
self.startAnimating();
}
self.stop = function()
{
self.container.removeChild(game.renderer.domElement); //Remove the renderer from the div
self.renderer.setSize(0, 0); //I guess this isn't needed, but welp
self.stopAnimating();
}
这个版本和正常工作的版本唯一的区别是,在
start
和 stop
方法中,startAnimating
和 stopAnimating
方法调用被注释掉了。
cancelRequestAnimationFrame
。你可以在Chrome中打开控制台,输入webkitCancel
,会发现在Chrome 33版本中仍然存在webkitCancelRequestAnimationFrame
。 - gmansetTimeout(function(){window.cancelAnimationFrame(requestId);},0);
- Legends