Type of arbitrary data associated with this interactive object. Used for identifying specific buttons in multi-button scenarios.
Creates a new ButtonInteractionHandler instance.
Configuration object containing view and optional material set
Initializes the interaction handler with the specified display object and optional material set. The handler immediately applies the initial material state upon creation.
This constructor is typically not called directly by end users. Instead, it is invoked internally by display object constructors (ClickableMesh, ClickableSprite) or conversion utility functions (convertToClickableMesh, convertToClickableSprite).
// Example 1: Creating clickable object from geometry and materials using ClickableMesh constructor
import { ClickableMesh, StateMaterialSet } from '@masatomakino/threejs-interactive-object';
import { BoxGeometry, MeshBasicMaterial } from 'three';
const materials = new StateMaterialSet({
normal: new MeshBasicMaterial({ color: 0x0000ff }),
over: new MeshBasicMaterial({ color: 0x00ff00 }),
down: new MeshBasicMaterial({ color: 0xff0000 })
});
const clickableMesh = new ClickableMesh({
geo: new BoxGeometry(1, 1, 1),
material: materials
});
clickableMesh.interactionHandler.value = { id: 'button1', action: 'save' };
// Example 2: Converting existing Mesh to clickable using convertToClickableMesh
import { convertToClickableMesh } from '@masatomakino/threejs-interactive-object';
import { Mesh } from 'three';
const existingMesh = new Mesh(new BoxGeometry(1, 1, 1), new MeshBasicMaterial());
const convertedClickable = convertToClickableMesh<string>(existingMesh);
convertedClickable.interactionHandler.value = 'converted-button';
// Listen for click events
convertedClickable.interactionHandler.on('click', (event) => {
console.log('Button clicked:', convertedClickable.interactionHandler.value);
});
For most use cases, prefer using ClickableMesh/ClickableSprite constructors for new objects or convertToClickable* utility functions for existing objects, rather than instantiating ButtonInteractionHandler directly.
Protected
Internal
_Internal alpha multiplier for opacity control.
Protected
Internal
_Internal state tracking enabled/disabled status.
Protected
Optional
Internal
_Internal reference to the material set.
Protected
Internal
hoverSet of pointer IDs currently hovering over the object.
Controls whether this object is scannable by MouseEventManager during interaction detection.
Protected
Internal
pressSet of pointer IDs currently in pressed state.
Current visual interaction state of the object.
Arbitrary data associated with this interactive object.
Readonly
viewThe display object being managed by this interaction handler.
Static
prefixedSets the opacity alpha multiplier for the material set.
Alpha multiplier value (minimum 0.0 for transparency, no maximum limit)
Controls the overall opacity of the interactive object by setting an alpha multiplier that is applied to all materials in the material set. This allows for fade effects while preserving the relative opacity differences between different interaction states.
Values above 1.0 are permitted to allow compensation for materials with low base opacity (e.g., alpha=5.0 with material opacity=0.2 results in final opacity=1.0). Negative values are clamped to 0.0 to prevent invalid opacity states.
Indicates whether the object is currently enabled for interactions.
True if the object is enabled, false if disabled
This getter provides read-only access to the internal enabled state. When enabled, the object can respond to pointer interactions; when disabled, the object becomes non-interactive and displays in disabled visual state.
Temporarily prevents interaction handling while maintaining current visual state.
Indicates whether any pointer is currently hovering over the object.
True if one or more pointers are over the object, false otherwise
Indicates whether any pointer is currently pressed down on the object.
True if one or more pointers are pressed down, false otherwise
Gets the current StateMaterialSet managing visual states.
The current material set, or undefined if none is assigned
Sets the StateMaterialSet for managing visual states.
The material set to assign, or undefined to remove
Optional
context: anyProtected
calculateProtected
Calculates the appropriate interaction state based on current conditions.
The calculated interaction state
Protected
checkProtected
Checks if the object is currently active and can respond to interactions.
True if the object is enabled and not frozen, false otherwise
Disables the interactive object, preventing response to pointer interactions.
Calls each of the listeners registered for a given event.
Enables the interactive object, allowing it to respond to pointer interactions.
Sets the object to an enabled state, making it responsive to pointer interactions
and updating the interaction state based on current pointer conditions ("over" or "normal").
This is equivalent to calling switchEnable(true)
and, on enable, clears pressed-pointer
state while preserving hover state.
Return an array listing the events for which the emitter has registered listeners.
Return the number of listeners listening to a given event.
Return the listeners registered for a given event.
Optional
fn: (Optional
context: anyOptional
once: booleanAdd a listener for a given event.
Optional
context: anyAdd a one-time listener for a given event.
Optional
context: anyHandles pointer down events and transitions to pressed state.
The pointer down event containing interaction details
Handles pointer out events.
The pointer out event containing interaction details
Handles pointer over events.
The pointer over event containing interaction details
Handles pointer up events and manages click detection logic with multi-touch suppression.
The pointer up event containing interaction details
Processes pointer up events by checking for same-pointer-ID press state, removing the pointer from the pressed set, determining the next visual state based on hover status, and emitting the up event. Click events are only triggered when the same pointer ID was previously pressed down (same-ID click detection).
Multi-touch Click Suppression: When a click is triggered, all remaining pressed pointers are cleared to prevent subsequent click events. This matches native browser behavior where multi-touch gestures suppress synthetic click events.
Browser behavior investigation on iPad revealed that multi-touch interactions naturally suppress click events. This implementation replicates that behavior by clearing the press map after the first successful click, preventing additional pointers from triggering subsequent clicks during the same multi-touch interaction.
Remove all listeners, or those of the specified event.
Optional
event: keyof ThreeMouseEventMap<Value>Remove the listeners of a given event.
Optional
fn: (Optional
context: anyOptional
once: booleanSwitches the interactive object between enabled and disabled states.
True to enable, false to disable
Changes the enabled state of the interactive object and immediately updates the interaction state and material. When disabled, all pointer states are cleared to prevent stale multitouch interactions. When enabled, only press states are cleared while hover states are preserved to maintain visual feedback.
Protected
updateProtected
Updates the display object's material based on current state and settings.
Protected
updateProtected
Updates the current interaction state and refreshes the visual representation.
The new interaction state to apply
Core interaction handler for pointer-interactive Three.js objects with button-like behavior.
Description
ButtonInteractionHandler provides comprehensive interaction management for Three.js display objects, handling pointer events, state transitions, material updates, and hover event duplicate management. This class was designed as a separate handler rather than extending display objects directly due to EventEmitter3's type system limitations that prevent proper event type extension in inheritance chains.
The handler manages four primary interaction states (see state property for details).
Hover Event Duplicate Management: Each handler instance manages its own over/out event duplicate suppression using asymmetric processing - over events are filtered for duplicates while out events are processed unconditionally for fail-safe cleanup.
Note: When the handler is inactive (disabled or frozen), "out" events still perform internal cleanup (e.g., clearing hover/press sets) but are not emitted.
Multi-touch Click Suppression: When multiple pointers interact with the same object, only the first pointer to complete a down-up sequence triggers a click event. Subsequent pointer releases are automatically suppressed to match native browser behavior where multi-touch gestures prevent synthetic click events.
Fires
click - Emitted when a complete click interaction occurs (down followed by up). In multi-touch scenarios, only the first completing pointer triggers this event.
Fires
down - Emitted when pointer is pressed down on the object
Fires
up - Emitted when pointer is released after being pressed
Fires
over - Emitted when pointer enters the object area
Fires
out - Emitted when pointer leaves the object area
Example
See