Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/garbage-collector/js/views/title-view.js
1036 views
1
/*globals define*/
2
define([
3
'text!./../../templates/title-view.html',
4
'utils'
5
], function( titleTemplate, Utils ) {
6
'use strict';
7
8
function TitleView( el ) {
9
this.el = document.querySelector( el );
10
if ( !this.el ) {
11
this.el = document.createElement( 'div' );
12
this.el.id = 'title-view';
13
this.el.classList.add( 'title-view' );
14
}
15
this.template = titleTemplate;
16
17
this.childEl = null;
18
19
this.orientationListener = this.onDeviceOrientation.bind( this );
20
this.mousemoveListener = this.onMouseMove.bind( this );
21
22
this.initialize();
23
}
24
25
TitleView.prototype.initialize = function() {
26
this.el.innerHTML = this.template;
27
this.childEl = this.el.querySelector( '.titles' );
28
29
this.el.addEventListener( 'mousemove', this.mousemoveListener );
30
window.addEventListener( 'deviceorientation', this.orientationListener );
31
};
32
33
TitleView.prototype.remove = function() {
34
window.removeEventListener( 'deviceorientation', this.orientationListener );
35
this.el.removeEventListener( 'movemove', this.mousemoveListener );
36
37
this.el.innerHTML = '';
38
39
if ( this.el.parentElement ) {
40
this.el.parentElement.removeChild( this.el );
41
}
42
};
43
44
TitleView.prototype.onDeviceOrientation = function( event ) {
45
var x = event.beta,
46
y = event.gamma;
47
48
x = Utils.clamp( x, -90, 90 );
49
this.setRotation( y, x );
50
};
51
52
TitleView.prototype.onMouseMove = function( event ) {
53
if ( !this.childEl ) {
54
return;
55
}
56
57
var rx = ( 0.5 - ( event.clientY / window.innerHeight ) ) * 90,
58
ry = -( 0.5 - ( event.clientX / window.innerWidth ) ) * 90;
59
60
this.setRotation( rx, ry );
61
};
62
63
TitleView.prototype.setRotation = function( rx, ry ) {
64
var transform = 'rotateX(' + rx + 'deg) rotateY(' + ry + 'deg)';
65
this.childEl.style.webkitTransform = transform;
66
this.childEl.style.transform = transform;
67
};
68
69
return TitleView;
70
});
71
72