UNPKG

aframe-extras

Version:

Add-ons and examples for A-Frame VR.

95 lines (85 loc) 3.18 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else { var a = factory(); for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; } })(self, () => { return /******/ (() => { // webpackBootstrap var __webpack_exports__ = {}; /*!**************************!*\ !*** ./src/misc/grab.js ***! \**************************/ /* global CANNON */ /** * Based on aframe/examples/showcase/tracked-controls. * * Handles events coming from the hand-controls. * Determines if the entity is grabbed or released. * Updates its position to move along the controller. */ AFRAME.registerComponent('grab', { init: function () { this.system = this.el.sceneEl.systems.physics; this.GRABBED_STATE = 'grabbed'; this.grabbing = false; this.hitEl = /** @type {AFRAME.Element} */ null; this.physics = /** @type {AFRAME.System} */ this.el.sceneEl.systems.physics; this.constraint = /** @type {CANNON.Constraint} */ null; // Bind event handlers this.onHit = this.onHit.bind(this); this.onGripOpen = this.onGripOpen.bind(this); this.onGripClose = this.onGripClose.bind(this); }, play: function () { const el = this.el; el.addEventListener('hit', this.onHit); el.addEventListener('gripdown', this.onGripClose); el.addEventListener('gripup', this.onGripOpen); el.addEventListener('trackpaddown', this.onGripClose); el.addEventListener('trackpadup', this.onGripOpen); el.addEventListener('triggerdown', this.onGripClose); el.addEventListener('triggerup', this.onGripOpen); }, pause: function () { const el = this.el; el.removeEventListener('hit', this.onHit); el.removeEventListener('gripdown', this.onGripClose); el.removeEventListener('gripup', this.onGripOpen); el.removeEventListener('trackpaddown', this.onGripClose); el.removeEventListener('trackpadup', this.onGripOpen); el.removeEventListener('triggerdown', this.onGripClose); el.removeEventListener('triggerup', this.onGripOpen); }, onGripClose: function () { this.grabbing = true; }, onGripOpen: function () { const hitEl = this.hitEl; this.grabbing = false; if (!hitEl) { return; } hitEl.removeState(this.GRABBED_STATE); this.hitEl = undefined; this.system.removeConstraint(this.constraint); this.constraint = null; }, onHit: function (evt) { const hitEl = evt.detail.el; // If the element is already grabbed (it could be grabbed by another controller). // If the hand is not grabbing the element does not stick. // If we're already grabbing something you can't grab again. if (hitEl.is(this.GRABBED_STATE) || !this.grabbing || this.hitEl) { return; } hitEl.addState(this.GRABBED_STATE); this.hitEl = hitEl; this.constraint = new CANNON.LockConstraint(this.el.body, hitEl.body); this.system.addConstraint(this.constraint); } }); /******/ return __webpack_exports__; /******/ })() ; }); //# sourceMappingURL=grab.js.map