Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves canvas initialization logic from a component to the scene. This… #2985

Merged
merged 1 commit into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require('./visible');
require('./vive-controls');
require('./wasd-controls');

require('./scene/canvas');
require('./scene/debug');
require('./scene/embedded');
require('./scene/inspector');
Expand Down
43 changes: 0 additions & 43 deletions src/components/scene/canvas.js

This file was deleted.

51 changes: 44 additions & 7 deletions src/core/scene/a-scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ module.exports.AScene = registerElement('a-scene', {
prototype: Object.create(AEntity.prototype, {
defaultComponents: {
value: {
'canvas': '',
'inspector': '',
'keyboard-shortcuts': '',
'screenshot': '',
Expand Down Expand Up @@ -68,10 +67,9 @@ module.exports.AScene = registerElement('a-scene', {
this.isPlaying = false;
this.originalHTML = this.innerHTML;
this.renderTarget = null;
this.addEventListener('render-target-loaded', function () {
this.setupRenderer();
this.resize();
});
setupCanvas(this);
this.setupRenderer();
this.resize();
this.addFullScreenStyles();
initPostMessageAPI(this);
},
Expand Down Expand Up @@ -224,13 +222,14 @@ module.exports.AScene = registerElement('a-scene', {
enterVR: {
value: function (fromExternal) {
var self = this;
var effect = this.effect;

// Don't enter VR if already in VR.
if (this.is('vr-mode')) { return Promise.resolve('Already in VR.'); }

// Enter VR via WebVR API.
if (!fromExternal && (this.checkHeadsetConnected() || this.isMobile)) {
return this.effect.requestPresent().then(enterVRSuccess, enterVRFailure);
return effect && effect.requestPresent().then(enterVRSuccess, enterVRFailure) || Promise.reject(new Error('VREffect not initialized'));
}

// Either entered VR already via WebVR API or VR not supported.
Expand Down Expand Up @@ -431,7 +430,9 @@ module.exports.AScene = registerElement('a-scene', {

setupRenderer: {
value: function () {
var renderer = this.renderer = new THREE.WebGLRenderer({
var renderer;

renderer = this.renderer = new THREE.WebGLRenderer({
canvas: this.canvas,
antialias: shouldAntiAlias(this),
alpha: true
Expand Down Expand Up @@ -659,4 +660,40 @@ function shouldAntiAlias (sceneEl) {
// Default not AA for mobile.
return !sceneEl.isMobile;
}

function setupCanvas (sceneEl) {
var canvasEl;

canvasEl = document.createElement('canvas');
canvasEl.classList.add('a-canvas');
// Mark canvas as provided/injected by A-Frame.
canvasEl.dataset.aframeCanvas = true;
sceneEl.appendChild(canvasEl);

document.addEventListener('fullscreenchange', onFullScreenChange);
document.addEventListener('mozfullscreenchange', onFullScreenChange);
document.addEventListener('webkitfullscreenchange', onFullScreenChange);

// Prevent overscroll on mobile.
canvasEl.addEventListener('touchmove', function (event) { event.preventDefault(); });

// Set canvas on scene.
sceneEl.canvas = canvasEl;
sceneEl.emit('render-target-loaded', {target: canvasEl});
// For unknown reasons a synchronous resize does not work on desktop when
// entering/exiting fullscreen.
setTimeout(bind(sceneEl.resize, sceneEl), 0);

function onFullScreenChange () {
var fullscreenEl =
document.fullscreenElement ||
document.mozFullScreenElement ||
document.webkitFullscreenElement;
// No fullscren element === exit fullscreen
if (!fullscreenEl) { sceneEl.exitVR(); }
document.activeElement.blur();
document.body.focus();
}
}

module.exports.shouldAntiAlias = shouldAntiAlias; // For testing.
13 changes: 0 additions & 13 deletions tests/components/scene/canvas.test.js

This file was deleted.

18 changes: 16 additions & 2 deletions tests/core/scene/a-scene.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,16 @@ suite('a-scene (without renderer)', function () {
});
});

suite('reload', function () {
suite.only('reload', function () {
test('reload scene innerHTML to original value', function () {
var canvasEl;
var sceneEl = this.el;
sceneEl.innerHTML = 'NEW';
sceneEl.reload();
assert.equal(sceneEl.innerHTML, '');
assert.equal(sceneEl.children.length, 1);
canvasEl = sceneEl.querySelector('canvas');
assert.equal(canvasEl.getAttribute('class'), 'a-canvas');
assert.equal(canvasEl.getAttribute('data-aframe-canvas'), 'true');
});

test('reloads the scene and pauses', function () {
Expand Down Expand Up @@ -605,6 +609,16 @@ suite('scenes', function () {
});
});

suite('setupCanvas', function () {
test('adds canvas to a-scene element', function () {
var el = this.sceneEl = document.createElement('a-scene');
el.canvas = undefined;
assert.notOk(el.canvas);
el.setupCanvas();
assert.ok(el.canvas);
});
});

suite('shouldAntiAlias', function () {
var sceneEl;

Expand Down