aframe-shake2show-component
Version:
Using shake.js to display toggle UI modal
1 lines • 4.11 kB
JavaScript
!function(t){function e(s){if(i[s])return i[s].exports;var o=i[s]={exports:{},id:s,loaded:!1};return t[s].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){var s=i(1);!function(t){function e(s){if(i[s])return i[s].exports;var o=i[s]={exports:{},id:s,loaded:!1};return t[s].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e){if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");AFRAME.registerComponent("shake2show",{schema:{trigger:{default:"shake"},threshold:{default:5},timeout:{default:500},zpos:{default:-.85},xoffset:{default:0},yoffset:{default:0}},init:function(){var t=new s({threshold:this.data.threshold,timeout:this.data.timeout});t.start(),"shake"===this.data.trigger?window.addEventListener(this.data.trigger,this.eventHandler.bind(this)):"click"===this.data.trigger?document.querySelector("a-scene").addEventListener(this.data.trigger,this.eventHandler.bind(this)):console.log("Trigger not supported. choose either 'shake' or 'click'!"),this.cameraEl=document.querySelector("a-entity[camera]"),this.cameraEl?(this.initHeight=Math.round(100*this.cameraEl.object3D.getWorldPosition().y)/100,this.yaxis=new THREE.Vector3(0,1,0),this.zaxis=new THREE.Vector3(0,0,1),this.pivot=new THREE.Object3D,this.el.object3D.position.set(this.data.xoffset,this.initHeight+this.data.yoffset,this.data.zpos),this.el.sceneEl.object3D.add(this.pivot),this.pivot.add(this.el.object3D)):console.log("Please add a camera to your scene.")},eventHandler:function(t){if(this.el.getAttribute("visible")===!1){var e=this.zaxis.clone();e.applyQuaternion(this.cameraEl.object3D.quaternion);var i=this.yaxis.clone().multiplyScalar(e.dot(this.yaxis));e.sub(i),e.normalize(),this.pivot.quaternion.setFromUnitVectors(this.zaxis,e);var s=this.cameraEl.object3D.getWorldPosition().x,o=Math.round(100*this.cameraEl.object3D.getWorldPosition().y)/100,n=this.cameraEl.object3D.getWorldPosition().z;this.initHeight===o&&0!==this.initHeight?o=0:o-=this.initHeight,this.pivot.position.set(s,o,n),this.el.setAttribute("scale","1 1 1"),this.el.setAttribute("visible",!0)}else this.el.getAttribute("visible")===!0&&(this.el.setAttribute("scale","0.00001 0.00001 0.00001"),this.el.setAttribute("visible",!1))},update:function(t){},remove:function(){}})}])},function(t,e,i){var s;!function(o,n){s=function(){return n(o,o.document)}.call(e,i,e,t),!(void 0!==s&&(t.exports=s))}("undefined"!=typeof window?window:this,function(t,e){"use strict";function i(i){if(this.hasDeviceMotion="ondevicemotion"in t,this.options={threshold:15,timeout:1e3},"object"==typeof i)for(var s in i)i.hasOwnProperty(s)&&(this.options[s]=i[s]);if(this.lastTime=new Date,this.lastX=null,this.lastY=null,this.lastZ=null,"function"==typeof e.CustomEvent)this.event=new e.CustomEvent("shake",{bubbles:!0,cancelable:!0});else{if("function"!=typeof e.createEvent)return!1;this.event=e.createEvent("Event"),this.event.initEvent("shake",!0,!0)}}return i.prototype.reset=function(){this.lastTime=new Date,this.lastX=null,this.lastY=null,this.lastZ=null},i.prototype.start=function(){this.reset(),this.hasDeviceMotion&&t.addEventListener("devicemotion",this,!1)},i.prototype.stop=function(){this.hasDeviceMotion&&t.removeEventListener("devicemotion",this,!1),this.reset()},i.prototype.devicemotion=function(e){var i,s,o=e.accelerationIncludingGravity,n=0,a=0,r=0;return null===this.lastX&&null===this.lastY&&null===this.lastZ?(this.lastX=o.x,this.lastY=o.y,void(this.lastZ=o.z)):(n=Math.abs(this.lastX-o.x),a=Math.abs(this.lastY-o.y),r=Math.abs(this.lastZ-o.z),(n>this.options.threshold&&a>this.options.threshold||n>this.options.threshold&&r>this.options.threshold||a>this.options.threshold&&r>this.options.threshold)&&(i=new Date,s=i.getTime()-this.lastTime.getTime(),s>this.options.timeout&&(t.dispatchEvent(this.event),this.lastTime=new Date)),this.lastX=o.x,this.lastY=o.y,void(this.lastZ=o.z))},i.prototype.handleEvent=function(t){if("function"==typeof this[t.type])return this[t.type](t)},i})}]);