Table of Contents
Threejs objects that implement their own raycast
method (meshes, lines, etc) can be interacted with by declaring events on them. We support pointer events, clicks and wheel-scroll. Events contain the browser event as well as the Threejs event data (object, point, distance, etc). You need to polyfill them yourself, if that's a concern.
Additionally, there's a special onUpdate
that is called every time the object gets fresh props, which is good for things like self => (self.verticesNeedUpdate = true)
.
Also notice the onPointerMissed
on the canvas element, which fires on clicks that haven't hit any meshes.
<meshonClick={(e) => console.log("click")}onWheel={(e) => console.log("wheel spins")}onPointerUp={(e) => console.log("up")}onPointerDown={(e) => console.log("down")}onPointerOver={(e) => console.log("over")}onPointerOut={(e) => console.log("out")}onPointerEnter={(e) => console.log("enter")}onPointerLeave={(e) => console.log("leave")}onPointerMove={(e) => console.log("move")}onUpdate={(self) => console.log("props have been updated")}/>
Event data
({...DomEvent // All the original event data...ThreeEvent // All of Three's intersection dataintersections: Intersect[] // All intersectionsobject: Object3D // The object that was actually hiteventObject: Object3D // The object that registered the eventunprojectedPoint: Vector3 // Camera-unprojected pointray: Ray // The ray that was used to strike the objectcamera: Camera // The camera that was used in the raycastersourceEvent: DomEvent // A reference to the host eventdelta: number // Initial-click delta}) => ...
Propagation and capturing
Edit this page on GitHub
onPointerDown={e => {// Only the mesh closest to the camera will be processede.stopPropagation()// You may optionally capture the targete.target.setPointerCapture(e.pointerId)}}onPointerUp={e => {e.stopPropagation()// Optionally release capturee.target.releasePointerCapture(e.pointerId)}}