All Projects → markuslerner → THREE.Interactive

markuslerner / THREE.Interactive

Licence: MIT License
Fast and simple interaction manager for three.js for enabling mouse and touch events on 3D objects

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to THREE.Interactive

Tweetscape
A WebVR experience displaying tweets in real-time along a 3D timeline
Stars: ✭ 132 (+169.39%)
Mutual labels:  threejs, interactive
getting-touchy-presentation
Everything you (n)ever wanted to know about touch and pointer events
Stars: ✭ 42 (-14.29%)
Mutual labels:  touch-events, pointer-events
Zalo.github.io
A home for knowledge that is hard to find elsewhere
Stars: ✭ 143 (+191.84%)
Mutual labels:  threejs, interactive
Tween.js
JavaScript/TypeScript animation engine
Stars: ✭ 8,409 (+17061.22%)
Mutual labels:  interactive, interaction
use-long-press
React hook for detecting click (or tap) and hold event
Stars: ✭ 97 (+97.96%)
Mutual labels:  touch-events, mouse-events
InViewer
A Three.js-based viewer for visualizing OGC IndoorGML data
Stars: ✭ 18 (-63.27%)
Mutual labels:  threejs
stereo-panorama-viewer
View stereoscopic panoramas in your browser!
Stars: ✭ 23 (-53.06%)
Mutual labels:  threejs
rllib.js
Reinforcement learning library with JavaScript.
Stars: ✭ 23 (-53.06%)
Mutual labels:  threejs
gzweb
Web client for Gazebo classic simulation
Stars: ✭ 36 (-26.53%)
Mutual labels:  threejs
networkdays
Networkdays functions ... including `networkdays` excel like function with no dependencies (no NumPy)
Stars: ✭ 22 (-55.1%)
Mutual labels:  events
it52-rails
Сайт нижегородского IT-сообщества
Stars: ✭ 17 (-65.31%)
Mutual labels:  events
ShadowEditor-examples
ShadowEditor Demo. https://tengge1.github.io/ShadowEditor-examples/
Stars: ✭ 33 (-32.65%)
Mutual labels:  threejs
rel-events
The relevant React Events Library.
Stars: ✭ 20 (-59.18%)
Mutual labels:  events
jetemit
jetemit is an event manager for React, React Native, Vue, Angular, and all javascript project
Stars: ✭ 44 (-10.2%)
Mutual labels:  events
NYC Taxi Pipeline
Design/Implement stream/batch architecture on NYC taxi data | #DE
Stars: ✭ 16 (-67.35%)
Mutual labels:  events
global-event-system-ue4
Loosely coupled internal event system plugin for the Unreal Engine.
Stars: ✭ 122 (+148.98%)
Mutual labels:  event-system
event-worker
A simpler way of dealing with Web Workers
Stars: ✭ 18 (-63.27%)
Mutual labels:  events
CQELight
CQELight is an entreprise grade extensible and customisable framework for creating software with CQRS, DDD & Event Sourcing patterns
Stars: ✭ 21 (-57.14%)
Mutual labels:  events
telephone-ts
Telephone-ts: The "Event Emitter-less" TypeScript Event Architecture.
Stars: ✭ 22 (-55.1%)
Mutual labels:  events
e
A library which combines a eventBus/emitter, DOM events management, delegated events, and event-based utils into a single lightweight and performant library.
Stars: ✭ 37 (-24.49%)
Mutual labels:  events

THREE.Interactive

NPM Package

Fast and simple interaction manager for THREE.js for enabling mouse and touch events on 3D objects.

Note: When using ReactJS I can highly recommend react-three-fiber, which has built-in interaction support. For pure THREE.js projects, this little library can be very useful though.

Hot it works:

  • Interactive Objects (THREE.Object3D) are added to the InteractionManager, which fires instances of InteractiveEvent.

  • Differenciates between mouseover/mouseout (closest objects) and mouseenter/mouseleave (all objects) events.

  • Intersections are sorted by distance to the camera and the events are dispatched in that order (closest first). If InteractiveEvent.stopPropagation() is called, the event won't fire again on other objects.

Alternative to three.interaction.

Collaborations and improvements are welcome.

Examples

  • Simple: Basic example
  • Depth: Overlapping objects example
  • glTF: Hover/click gltf objects example

Usage

  1. Include script:
import { InteractionManager } from "three.interactive";
  1. Create an InteractionManager instance
const interactionManager = new InteractionManager(
  renderer,
  camera,
  renderer.domElement
);
  1. Add object to InteractionManager
interactionManager.add(cube);
  1. Add event listener to object
cube.addEventListener("click", (event) => {
});
  1. Call InteractionManager.update() on each render
interactionManager.update();

Simple example

import * as THREE from "three";
import { InteractionManager } from "three.interactive";

const container = document.createElement("div");
container.setAttribute("id", "container");
document.body.appendChild(container);

const renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);

const scene = new THREE.Scene();

const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0.0, 0.0, 10.0);

const interactionManager = new InteractionManager(
  renderer,
  camera,
  renderer.domElement
);

const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial();

const cube = new THREE.Mesh(geometry, material);
cube.addEventListener("mouseover", (event) => {
  event.target.material.color.set(0xff0000);
  document.body.style.cursor = "pointer";
});
cube.addEventListener("mouseout", (event) => {
  event.target.material.color.set(0xffffff);
  document.body.style.cursor = "default";
});
cube.addEventListener("mousedown", (event) => {
  event.target.scale.set(1.1, 1.1, 1.1);
});
cube.addEventListener("click", (event) => {
  event.target.scale.set(1.0, 1.0, 1.0);
});
scene.add(cube);
interactionManager.add(cube);

const animate = (time) => {
  requestAnimationFrame(animate);

  interactionManager.update();

  renderer.render(scene, camera);
};

animate();

API

InteractionManager class

new InteractionManager(renderer, camera, renderer.domElement [, dontBindEventsOnBody]) – constructor InteractionManager instance

Members:

treatTouchEventsAsMouseEvents (boolean) – wether touch events should fire as mouse events, default: true

Methods:

interactionManager.add(object, childNames = []) – add object(s), optionally select only child objects by names

interactionManager.remove(object, childNames = []) – remove object(s)

interactionManager.update() – update InteractionManager on each render

interactionManager.dispose() – dispose InteractionManager

InteractiveEvent class

Members:

cancelBubble (boolean) – wether events should continue to bubble, default: false

coords (THREE.Vector2) – Mouse/touch coords

distance (number) – Distance of intersected point from camera

intersected (boolean) – Whether object is still intersected

originalEvent (Event object) – Original event, if available (MouseEvent, TouchEvent or PointerEvent)

target (THREE.Object3D) – Target object

type (string) – event type: 'click', 'mouseover', 'mouseout', 'mouseenter', 'mouseleave', 'mousedown', 'mousemove', 'mouseup', 'touchstart', 'touchmove', 'touchend', 'pointerdown', 'pointerup'

Methods:

stopPropagation – stop bubbling of event (cancelBubble), e.g. when only the object closest to the camera is supposed to fire an event

License

MIT licensed

Created by Markus Lerner & contributors

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].