Path: blob/main/projects/HexGL/bkcore.coffee/controllers/OrientationController.js
4627 views
// Generated by CoffeeScript 1.4.012/*3OrientationController (Orientation + buttons) for touch devices45@class bkcore.OrientationController6@author Thibaut 'BKcore' Despoulain <http://bkcore.com>7*/8910(function() {11var OrientationController, exports, _base;1213OrientationController = (function() {1415OrientationController.isCompatible = function() {16return 'DeviceOrientationEvent' in window;17};1819/*20Creates a new OrientationController2122@param dom DOMElement The element that will listen to touch events23@param registerTouch bool Enable touch detection24@param touchCallback function Callback for touches25*/262728function OrientationController(dom, registerTouch, touchCallback) {29var _this = this;30this.dom = dom;31this.registerTouch = registerTouch != null ? registerTouch : true;32this.touchCallback = touchCallback != null ? touchCallback : null;33this.active = true;34this.alpha = 0.0;35this.beta = 0.0;36this.gamma = 0.0;37this.dalpha = null;38this.dbeta = null;39this.dgamma = null;40this.touches = null;41window.addEventListener('deviceorientation', (function(e) {42return _this.orientationChange(e);43}), false);44if (this.registerTouch) {45this.dom.addEventListener('touchstart', (function(e) {46return _this.touchStart(e);47}), false);48this.dom.addEventListener('touchend', (function(e) {49return _this.touchEnd(e);50}), false);51}52}5354/*55@private56*/575859OrientationController.prototype.orientationChange = function(event) {60if (!this.active) {61return;62}63if (this.dalpha === null) {64console.log("calbrate", event.beta);65this.dalpha = event.alpha;66this.dbeta = event.beta;67this.dgamma = event.gamma;68}69this.alpha = event.alpha - this.dalpha;70this.beta = event.beta - this.dbeta;71this.gamma = event.gamma - this.dgamma;72return false;73};7475/*76@private77*/787980OrientationController.prototype.touchStart = function(event) {81var touch, _i, _len, _ref;82if (!this.active) {83return;84}85_ref = event.changedTouches;86for (_i = 0, _len = _ref.length; _i < _len; _i++) {87touch = _ref[_i];88if (typeof this.touchCallback === "function") {89this.touchCallback(true, touch, event);90}91}92this.touches = event.touches;93return false;94};9596/*97@private98*/99100101OrientationController.prototype.touchEnd = function(event) {102var touch, _i, _len, _ref;103if (!this.active) {104return;105}106_ref = event.changedTouches;107for (_i = 0, _len = _ref.length; _i < _len; _i++) {108touch = _ref[_i];109if (typeof this.touchCallback === "function") {110this.touchCallback(true, touch, event);111}112}113this.touches = event.touches;114return false;115};116117return OrientationController;118119})();120121exports = exports != null ? exports : this;122123exports.bkcore || (exports.bkcore = {});124125(_base = exports.bkcore).controllers || (_base.controllers = {});126127exports.bkcore.controllers.OrientationController = OrientationController;128129}).call(this);130131132