【发布时间】:2012-05-30 22:40:10
【问题描述】:
我正在使用 Three.js 和 WebGL 渲染器来制作一个在单击 play 链接时全屏显示的游戏。对于动画,我使用requestAnimationFrame。
我是这样发起的:
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 函数时,事情就坏了!不过没有报告错误...
设置基本上是这样的:
- 页面上有
play链接 - 一旦用户点击链接,渲染器的
domElement应该是全屏的,并且确实如此 -
startAnimating方法被调用,渲染器开始渲染东西 - 单击转义后,我注册一个
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();
}
这个版本和工作版本的唯一区别是startAnimating和stopAnimating方法调用在start和stop方法被注释掉了。
【问题讨论】:
标签: javascript html fullscreen webgl three.js