/
tquery.world.boilerplate.js
141 lines (124 loc) · 4.04 KB
/
tquery.world.boilerplate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// backward compatibility only
tQuery.World.register('fullpage', function(){
console.log("world.fullpage() is obsolete. use world.boilerplate() instead.");
return this.boilerplate();
});
tQuery.World.register('boilerplate', function(opts){
// put renderer fullpage
var domElement = document.body;
domElement.style.margin = "0";
domElement.style.padding = "0";
domElement.style.overflow = 'hidden';
this.appendTo(domElement);
this._renderer.setSize( domElement.offsetWidth, domElement.offsetHeight );
// add the boilerplate
this.addBoilerplate(opts);
// for chained API
return this;
});
/**
* Define a page title
*/
tQuery.World.register('pageTitle', function(element){
// handle parameters polymorphism
if( typeof(element) === 'string' ){
var element = document.querySelector(element);
}
// sanity check
console.assert( element instanceof HTMLElement);
// set element.style
element.style.position = "absolute";
element.style.width = "100%";
element.style.textAlign = "center";
element.style.textAlign = "center";
element.style.fontWeight= "bolder";
element.style.fontSize = "120%";
element.style.fontFamily= "arial";
// for chained API
return this;
});
tQuery.World.register('addBoilerplate', function(opts){
var _this = this;
// sanity check - no boilerplate is already installed
console.assert( this.hasBoilerplate() !== true );
// handle parameters
opts = tQuery.extend(opts, {
stats : true,
cameraControls : true,
windowResize : true,
screenshot : true,
fullscreen : true
});
// get the context
var ctx = {};
// make tRenderer.domElement style "display: block" - by default it is inline-block
// - so it is affected by line-height and create a white line at the bottom
this.tRenderer().domElement.style.display = "block"
// create the context
tQuery.data(this, '_boilerplateCtx', ctx);
// get some variables
var tCamera = this.tCamera();
var tRenderer = this.tRenderer();
// add Stats.js - https://github.com/mrdoob/stats.js
if( opts.stats ){
ctx.stats = new Stats();
ctx.stats.domElement.style.position = 'absolute';
ctx.stats.domElement.style.bottom = '0px';
tRenderer.domElement.parentNode && tRenderer.domElement.parentNode.appendChild( ctx.stats.domElement );
ctx.loopStats = function(){
ctx.stats.update();
};
this.loop().hook(ctx.loopStats);
}
// create a camera contol
if( opts.cameraControls ){
ctx.cameraControls = new THREEx.DragPanControls(tCamera);
this.setCameraControls(ctx.cameraControls);
}
// transparently support window resize
if( opts.windowResize ){
ctx.windowResize = THREEx.WindowResize.bind(tRenderer, tCamera);
}
// allow 'p' to make screenshot
if( opts.screenshot ){
ctx.screenshot = THREEx.Screenshot.bindKey(tRenderer);
}
// allow 'f' to go fullscreen where this feature is supported
if( opts.fullscreen && THREEx.FullScreen.available() ){
ctx.fullscreen = THREEx.FullScreen.bindKey();
}
// bind 'destroy' event on tQuery.world
ctx._$onDestroy = this.bind('destroy', function(){
if( this.hasBoilerplate() === false ) return;
this.removeBoilerplate();
});
// for chained API
return this;
});
tQuery.World.register('hasBoilerplate', function(){
// get the context
var ctx = tQuery.data(this, "_boilerplateCtx")
// return true if ctx if defined, false otherwise
return ctx === undefined ? false : true;
});
tQuery.World.register('removeBoilerplate', function(){
// get context
var ctx = tQuery.data(this, '_boilerplateCtx');
// if not present, return now
if( ctx === undefined ) return this;
// remove the context from this
tQuery.removeData(this, '_boilerplateCtx');
// unbind 'destroy' for tQuery.World
this.unbind('destroy', this._$onDestroy);
// remove stats.js
ctx.stats && document.body.removeChild(ctx.stats.domElement );
ctx.stats && this.loop().unhook(ctx.loopStats);
// remove camera
ctx.cameraControls && this.removeCameraControls()
// stop windowResize
ctx.windowResize && ctx.windowResize.stop();
// unbind screenshot
ctx.screenshot && ctx.screenshot.unbind();
// unbind fullscreen
ctx.fullscreen && ctx.fullscreen.unbind();
});