Path: blob/main/public/games/files/garbage-collector/js/views/title-view.js
1036 views
/*globals define*/1define([2'text!./../../templates/title-view.html',3'utils'4], function( titleTemplate, Utils ) {5'use strict';67function TitleView( el ) {8this.el = document.querySelector( el );9if ( !this.el ) {10this.el = document.createElement( 'div' );11this.el.id = 'title-view';12this.el.classList.add( 'title-view' );13}14this.template = titleTemplate;1516this.childEl = null;1718this.orientationListener = this.onDeviceOrientation.bind( this );19this.mousemoveListener = this.onMouseMove.bind( this );2021this.initialize();22}2324TitleView.prototype.initialize = function() {25this.el.innerHTML = this.template;26this.childEl = this.el.querySelector( '.titles' );2728this.el.addEventListener( 'mousemove', this.mousemoveListener );29window.addEventListener( 'deviceorientation', this.orientationListener );30};3132TitleView.prototype.remove = function() {33window.removeEventListener( 'deviceorientation', this.orientationListener );34this.el.removeEventListener( 'movemove', this.mousemoveListener );3536this.el.innerHTML = '';3738if ( this.el.parentElement ) {39this.el.parentElement.removeChild( this.el );40}41};4243TitleView.prototype.onDeviceOrientation = function( event ) {44var x = event.beta,45y = event.gamma;4647x = Utils.clamp( x, -90, 90 );48this.setRotation( y, x );49};5051TitleView.prototype.onMouseMove = function( event ) {52if ( !this.childEl ) {53return;54}5556var rx = ( 0.5 - ( event.clientY / window.innerHeight ) ) * 90,57ry = -( 0.5 - ( event.clientX / window.innerWidth ) ) * 90;5859this.setRotation( rx, ry );60};6162TitleView.prototype.setRotation = function( rx, ry ) {63var transform = 'rotateX(' + rx + 'deg) rotateY(' + ry + 'deg)';64this.childEl.style.webkitTransform = transform;65this.childEl.style.transform = transform;66};6768return TitleView;69});707172