File tree Expand file tree Collapse file tree 2 files changed +30
-6
lines changed Expand file tree Collapse file tree 2 files changed +30
-6
lines changed Original file line number Diff line number Diff line change @@ -402,13 +402,22 @@ var AScene = module.exports = registerElement('a-scene', {
402402
403403 setupCanvas : {
404404 value : function ( ) {
405- var canvas = this . canvas = document . createElement ( 'canvas' ) ;
405+ var canvasSelector = this . getAttribute ( 'canvas' ) ;
406+ var canvas ;
407+
408+ if ( canvasSelector ) {
409+ canvas = this . canvas = document . querySelector ( canvasSelector ) ;
410+ } else {
411+ canvas = this . canvas = document . createElement ( 'canvas' ) ;
412+ this . appendChild ( canvas ) ;
413+ }
414+
406415 canvas . classList . add ( 'a-canvas' ) ;
407416 // Prevents overscroll on mobile devices.
408417 canvas . addEventListener ( 'touchmove' , function ( evt ) {
409418 evt . preventDefault ( ) ;
410419 } ) ;
411- document . body . appendChild ( canvas ) ;
420+
412421 window . addEventListener ( 'resize' , this . resizeCanvas . bind ( this ) , false ) ;
413422 }
414423 } ,
Original file line number Diff line number Diff line change @@ -68,13 +68,28 @@ suite('a-scene (without renderer)', function () {
6868 } ) ;
6969
7070 suite ( 'setupCanvas' , function ( ) {
71- test ( 'adds canvas' , function ( done ) {
72- assert . notOk ( document . querySelector ( 'canvas' ) ) ;
71+ test ( 'adds canvas to a-scene element by default ' , function ( done ) {
72+ assert . notOk ( this . el . querySelector ( 'canvas' ) ) ;
7373 this . el . setupCanvas ( ) ;
7474 process . nextTick ( function ( ) {
75- assert . ok ( document . querySelector ( 'canvas' ) ) ;
75+ assert . ok ( this . el . querySelector ( 'canvas' ) ) ;
7676 done ( ) ;
77- } ) ;
77+ } . bind ( this ) ) ;
78+ } ) ;
79+
80+ test ( 'reuses existing canvas when selector is given' , function ( done ) {
81+ var canvas = document . createElement ( 'canvas' ) ;
82+ canvas . setAttribute ( 'id' , 'canvas' ) ;
83+ document . body . appendChild ( canvas ) ;
84+ assert . notOk ( canvas . classList . contains ( 'a-canvas' ) ) ;
85+
86+ this . el . setAttribute ( 'canvas' , '#canvas' ) ;
87+ this . el . setupCanvas ( ) ;
88+ process . nextTick ( function ( ) {
89+ assert . ok ( canvas . classList . contains ( 'a-canvas' ) ) ;
90+ assert . notOk ( this . el . querySelector ( 'canvas' ) ) ;
91+ done ( ) ;
92+ } . bind ( this ) ) ;
7893 } ) ;
7994 } ) ;
8095
You can’t perform that action at this time.
0 commit comments