{"size": 1258, "ext": "js", "max_stars_repo_path": "src/components/mouse-shim.js", "max_stars_repo_name": "vrlandio/aframe-material-collection", "max_stars_repo_stars_event_min_datetime": "2020-07-03T13:09:10.000Z", "max_stars_repo_stars_event_max_datetime": "2020-07-03T13:09:10.000Z", "max_issues_repo_path": "src/components/mouse-shim.js", "max_issues_repo_name": "vrlandio/aframe-material-collection", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/mouse-shim.js", "max_forks_repo_name": "vrlandio/aframe-material-collection", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.6666666667, "max_line_length": 108, "alphanum_fraction": 0.6820349762}
/* global AFRAME */
/**
 * A component shim the mouse move event for the AFRAME cursor raycaster.
 * @namespace aframe-material-collection
 * @component ui-mouse-shim
 * @author Shane Harris
 */
module.exports = AFRAME.registerComponent( "ui-mouse-shim", {
	schema: {
		fps: { type: "number", default: 45 }
	},
	init() {

		if ( ! this.el.components.raycaster ) {

			throw "ui-mouse-move component needs the raycaster component to be added.";

		}
		// Add support for mouse wheel
		this.el.sceneEl.renderer.domElement.addEventListener( "wheel", this.onMouseWheel.bind( this ), false );

	},
	onMouseWheel( e ) {

		this.emitMouseEvent( "ui-mousewheel", e );

	},
	tick() {

		if ( new Date().getTime() - this.lastMouseMoveTime < 1000 / this.data.fps ) return;
		this.emitMouseEvent( "ui-mousemove" );
		this.lastMouseMoveTime = new Date().getTime();

	},
	emitMouseEvent( eventType, event ) {

		// Get current intersections from raycaster component.
		this.el.components.raycaster.intersections.forEach( intersection => {

			if ( intersection.object.el ) {

				// Emit custom mouse move event ont he intersected element.
				intersection.object.el.emit( eventType, { cursorEl: this.el, intersection: intersection, evt: event } );

			}

		} );

	}
} );
