WebVR.js 2.3 KB
Newer Older
M
Mr.doob 已提交
1 2 3 4 5 6 7
/**
 * @author mrdoob / http://mrdoob.com
 * Based on @tojiro's vr-samples-utils.js
 */

var WEBVR = {

8
	isLatestAvailable: function () {
M
Mr.doob 已提交
9

M
Mr.doob 已提交
10 11
		console.warn( 'WEBVR: isLatestAvailable() is being deprecated. Use .isAvailable() instead.' );
		return this.isAvailable();
M
Mr.doob 已提交
12 13 14

	},

15 16
	isAvailable: function () {

M
Mr.doob 已提交
17 18 19 20
		return navigator.getVRDisplays !== undefined;

	},

M
Mr.doob 已提交
21
	getMessage: function () {
M
Mr.doob 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

		var message;

		if ( navigator.getVRDisplays ) {

			navigator.getVRDisplays().then( function ( displays ) {

				if ( displays.length === 0 ) message = 'WebVR supported, but no VRDisplays found.';

			} );

		} else {

			message = 'Your browser does not support WebVR. See <a href="http://webvr.info">webvr.info</a> for assistance.';

		}

		if ( message !== undefined ) {

			var container = document.createElement( 'div' );
			container.style.position = 'absolute';
			container.style.left = '0';
			container.style.top = '0';
			container.style.right = '0';
			container.style.zIndex = '999';
			container.align = 'center';

			var error = document.createElement( 'div' );
			error.style.fontFamily = 'sans-serif';
			error.style.fontSize = '16px';
			error.style.fontStyle = 'normal';
			error.style.lineHeight = '26px';
			error.style.backgroundColor = '#fff';
			error.style.color = '#000';
			error.style.padding = '10px 20px';
57
			error.style.margin = '50px';
M
Mr.doob 已提交
58 59 60 61
			error.style.display = 'inline-block';
			error.innerHTML = message;
			container.appendChild( error );

M
Mr.doob 已提交
62 63
			return container;

M
Mr.doob 已提交
64 65
		}

M
Mr.doob 已提交
66 67 68 69 70 71
	},

	getButton: function ( effect ) {

		var button = document.createElement( 'button' );
		button.style.position = 'absolute';
M
Mr.doob 已提交
72
		button.style.left = 'calc(50% - 50px)';
M
Mr.doob 已提交
73
		button.style.bottom = '20px';
M
Mr.doob 已提交
74
		button.style.width = '100px';
M
Mr.doob 已提交
75 76 77 78 79 80 81 82
		button.style.border = '0';
		button.style.padding = '8px';
		button.style.cursor = 'pointer';
		button.style.backgroundColor = '#000';
		button.style.color = '#fff';
		button.style.fontFamily = 'sans-serif';
		button.style.fontSize = '13px';
		button.style.fontStyle = 'normal';
M
Mr.doob 已提交
83
		button.style.textAlign = 'center';
M
Mr.doob 已提交
84 85
		button.style.zIndex = '999';
		button.textContent = 'ENTER VR';
M
Mr.doob 已提交
86
		button.onclick = function () {
M
Mr.doob 已提交
87

M
Mr.doob 已提交
88
			effect.isPresenting ? effect.exitPresent() : effect.requestPresent();
M
Mr.doob 已提交
89 90 91

		};

M
Mr.doob 已提交
92
		window.addEventListener( 'vrdisplaypresentchange', function () {
M
Mr.doob 已提交
93 94 95 96 97

			button.textContent = effect.isPresenting ? 'EXIT VR' : 'ENTER VR';

		}, false );

M
Mr.doob 已提交
98 99
		return button;

M
Mr.doob 已提交
100 101 102
	}

};