UNPKG

@adoratorio/apollo

Version:

A JS library for custom cursor

118 lines 5.43 kB
import { VISIBILITY_CHECK } from "./declarations"; import SingleTarget from "./SingleTarget"; import { isInRect, isVisible, emitEvent } from './utils'; class TargetsDetection { context = null; options; _targets = []; name = 'TargetsDetection'; activeMouseTarget = null; activeCursorTarget = null; static VISIBILITY_CHECK = VISIBILITY_CHECK; static EVENTS = { MOUSE_ENTER: 'apollo-mouse-enter', MOUSE_LEAVE: 'apollo-mouse-leave', CURSOR_ENTER: 'apollo-cursor-enter', CURSOR_LEAVE: 'apollo-cursor-leave', }; constructor(options) { const defaults = { targets: [], emitGlobal: true, }; this.options = { ...defaults, ...options }; this.options.targets.forEach((target) => { target.elements.forEach((element, index) => { this._targets.push(new SingleTarget(element, target, index)); }); }); } register(context) { this.context = context; } preFrame(context, delta) { this._targets.forEach(target => target.frame(delta)); this.checkTargets(); } checkTargets() { if (!this.context) return; if (this.activeMouseTarget !== null && !isInRect(this.context.mouse, this.activeMouseTarget.boundings)) { emitEvent(TargetsDetection.EVENTS.MOUSE_LEAVE, { target: this.activeMouseTarget }); this.activeMouseTarget.descriptor.callback(this.activeMouseTarget, TargetsDetection.EVENTS.MOUSE_LEAVE); this.activeMouseTarget = null; } if (this.activeCursorTarget !== null && !isInRect(this.context.coords, this.activeCursorTarget.boundings)) { emitEvent(TargetsDetection.EVENTS.CURSOR_LEAVE, { target: this.activeCursorTarget }); this.activeCursorTarget.descriptor.callback(this.activeCursorTarget, TargetsDetection.EVENTS.CURSOR_LEAVE); this.activeCursorTarget = null; } let matchedOneMouse = false; let matchedOneCursor = false; for (let i = 0; i < this._targets.length; i++) { const target = this._targets[i]; if (target && isVisible(target)) { if (isInRect(this.context.mouse, target.boundings) && !matchedOneMouse) { if (this.activeMouseTarget === null || this.activeMouseTarget.id !== target.id) { if (this.activeMouseTarget !== null) { if (this.options.emitGlobal) emitEvent(TargetsDetection.EVENTS.MOUSE_LEAVE, { target: this.activeMouseTarget }); this.activeMouseTarget.descriptor.callback(this.activeMouseTarget, TargetsDetection.EVENTS.MOUSE_LEAVE); } this.activeMouseTarget = target; if (this.options.emitGlobal) emitEvent(TargetsDetection.EVENTS.MOUSE_ENTER, { target: this.activeMouseTarget }); this.activeMouseTarget.descriptor.callback(this.activeMouseTarget, TargetsDetection.EVENTS.MOUSE_ENTER); } matchedOneMouse = true; } if (isInRect(this.context.coords, target.boundings) && !matchedOneCursor) { if (this.activeCursorTarget === null || this.activeCursorTarget.id !== target.id) { if (this.activeCursorTarget !== null) { if (this.options.emitGlobal) emitEvent(TargetsDetection.EVENTS.CURSOR_LEAVE, { target: this.activeCursorTarget }); this.activeCursorTarget.descriptor.callback(this.activeCursorTarget, TargetsDetection.EVENTS.CURSOR_LEAVE); } this.activeCursorTarget = target; if (this.options.emitGlobal) emitEvent(TargetsDetection.EVENTS.CURSOR_ENTER, { target: this.activeCursorTarget }); this.activeCursorTarget.descriptor.callback(this.activeCursorTarget, TargetsDetection.EVENTS.CURSOR_ENTER); } matchedOneCursor = true; } } } } render(context, delta) { this._targets.forEach(target => target.render(delta)); } afterRender(context, delta) { this._targets.forEach(target => target.postRender(delta)); } addTarget(target) { target.elements.forEach((element, index) => { if (element._apolloId !== '-1') return; this._targets.push(new SingleTarget(element, target, index)); }); } removeTarget(id) { for (let index = this._targets.length - 1; index >= 0; index--) { const target = this._targets[index]; if (target && target.descriptor.id === id) { this._targets.splice(index, 1); } } } pullFromTarget(element) { for (let index = this._targets.length - 1; index >= 0; index--) { const target = this._targets[index]; if (target && target.id === element._apolloId) { element._apolloId = '-1'; this._targets.splice(index, 1); } } } } export default TargetsDetection; //# sourceMappingURL=index.js.map