Skip to content

Commit

Permalink
[WebVR] Fixes minor bugs in demo
Browse files Browse the repository at this point in the history
  • Loading branch information
paullewis committed Dec 8, 2016
1 parent ceb0a28 commit 34745e0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
31 changes: 15 additions & 16 deletions web-vr/hello-world/demo/demo-vr.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ class DemoVR extends Demo {
constructor () {
super();

this._onResize = this._onResize.bind(this);

this._disabled = false;
if (typeof VRFrameData === 'undefined') {
this._disabled = true;
this._showWebVRNotSupportedError();
return;
}

this._firstVRFrame = false;
this._button = undefined;
this._vr = {
display: null,
Expand All @@ -41,11 +44,11 @@ class DemoVR extends Demo {
}

_addVREventListeners () {
window.addEventListener('vrdisplayactivate', evt => {
window.addEventListener('vrdisplayactivate', _ => {
this._activateVR();
});

window.addEventListener('vrdisplaydeactivate', evt => {
window.addEventListener('vrdisplaydeactivate', _ => {
this._deactivateVR();
});
}
Expand Down Expand Up @@ -82,18 +85,11 @@ class DemoVR extends Demo {
_createPresentationButton () {
this._button = document.createElement('button');
this._button.classList.add('vr-toggle');
this._button.textContent = 'Enable VR';
this._button.addEventListener('click', _ => {
this._toggleVR();
});
document.body.appendChild(this._button);

this._updateButtonLabel();
}

_updateButtonLabel () {
this._button.textContent = this._vr.display.isPresenting ?
'Disable VR' :
'Enable VR';
}

_deactivateVR () {
Expand All @@ -102,12 +98,10 @@ class DemoVR extends Demo {
}

if (!this._vr.display.isPresenting) {
this._updateButtonLabel();
return;
}

this._vr.display.exitPresent();
this._updateButtonLabel();
return;
}

Expand All @@ -119,12 +113,8 @@ class DemoVR extends Demo {
this._vr.display.requestPresent([{
source: this._renderer.domElement
}])
.then(_ => {
this._updateButtonLabel();
})
.catch(e => {
console.error(`Unable to init VR: ${e}`);
this._updateButtonLabel();
});
}

Expand All @@ -146,6 +136,15 @@ class DemoVR extends Demo {
return super._render();
}

// When this is called the first time, it will be using the standard
// window.requestAnimationFrame API, which will throw a warning when we call
// display.submitFrame. So for the first frame that this is called we will
// exit early and request a new frame from the VR device instead.
if (this._firstVRFrame) {
this._firstVRFrame = false;
return this._vr.display.requestAnimationFrame(this._update);
}

const EYE_WIDTH = this._width * 0.5;
const EYE_HEIGHT = this._height;

Expand Down
1 change: 1 addition & 0 deletions web-vr/hello-world/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
html, body {
padding: 0;
margin: 0;
user-select: none;
}

.vr-toggle {
Expand Down

0 comments on commit 34745e0

Please sign in to comment.