11import { Object3D , type Intersection } from "three"
2- import type { CanvasProps } from "./canvas.tsx"
32import { $S3C } from "./constants.ts"
43import type { Context , EventName , Instance , ThreeEvent } from "./types.ts"
54import { isInstance } from "./utils.ts"
@@ -97,14 +96,11 @@ function createThreeEvent<TEvent extends Event>(
9796function raycast < TNativeEvent extends MouseEvent | WheelEvent > (
9897 context : Context ,
9998 registry : Object3D [ ] ,
100- nativeEvent : TNativeEvent ,
99+ event : TNativeEvent ,
101100) : Intersection < Instance < Object3D > > [ ] {
102- context . setPointer ( pointer => {
103- pointer . x = ( nativeEvent . offsetX / context . bounds . width ) * 2 - 1
104- pointer . y = - ( nativeEvent . offsetY / context . bounds . height ) * 2 + 1
105- return pointer
106- } )
107- context . raycaster . setFromCamera ( context . pointer , context . camera )
101+ if ( "update" in context . raycaster ) {
102+ context . raycaster . update ( event , context )
103+ }
108104
109105 const nodeSet = new Set < Object3D > ( )
110106 const visitedSet = new Set < Object3D > ( )
@@ -138,7 +134,6 @@ function raycast<TNativeEvent extends MouseEvent | WheelEvent>(
138134function createMissableEventRegistry (
139135 type : "onClick" | "onDoubleClick" | "onContextMenu" ,
140136 context : Context ,
141- props : CanvasProps ,
142137) {
143138 const registry = createRegistry < Object3D > ( )
144139
@@ -169,13 +164,11 @@ function createMissableEventRegistry(
169164 // Call the respective canvas event-handler
170165 // if event propagated all the way down
171166 if ( ! event . stopped ) {
172- props [ type ] ?.( event )
167+ context . props [ type ] ?.( event )
173168 }
174169
175170 // Phase #2 - Raycast remaining missed objects
176171 for ( const remainingObject of missedObjects ) {
177- // Perform raycast on unvisited missed objects
178- context . raycaster . setFromCamera ( context . pointer , context . camera )
179172 const intersections = context . raycaster . intersectObject ( remainingObject , true )
180173
181174 // Bubble down intersections
@@ -202,7 +195,7 @@ function createMissableEventRegistry(
202195 }
203196
204197 if ( visitedObjects . size > 0 ) {
205- props [ `${ type } Missed` ] ?.( missedEvent )
198+ context . props [ `${ type } Missed` ] ?.( missedEvent )
206199 }
207200 } )
208201
@@ -226,7 +219,7 @@ function createMissableEventRegistry(
226219 * - `onPointerMove`
227220 * - `onPointerLeave`
228221 */
229- function createHoverEventRegistry ( type : "Mouse" | "Pointer" , context : Context , props : CanvasProps ) {
222+ function createHoverEventRegistry ( type : "Mouse" | "Pointer" , context : Context ) {
230223 const registry = createRegistry < Object3D > ( )
231224 let hoveredSet = new Set < Object3D > ( )
232225 let intersections : Intersection < Instance < Object3D > > [ ] = [ ]
@@ -254,7 +247,7 @@ function createHoverEventRegistry(type: "Mouse" | "Pointer", context: Context, p
254247 }
255248
256249 if ( hoveredCanvas === false ) {
257- props [ `on${ type } Enter` ] ?.( enterEvent )
250+ context . props [ `on${ type } Enter` ] ?.( enterEvent )
258251 hoveredCanvas = true
259252 }
260253
@@ -282,7 +275,7 @@ function createHoverEventRegistry(type: "Mouse" | "Pointer", context: Context, p
282275 }
283276
284277 if ( ! moveEvent . stopped ) {
285- props [ `on${ type } Move` ] ?.( moveEvent )
278+ context . props [ `on${ type } Move` ] ?.( moveEvent )
286279 }
287280
288281 // Handle leave-event
@@ -299,7 +292,7 @@ function createHoverEventRegistry(type: "Mouse" | "Pointer", context: Context, p
299292
300293 context . canvas . addEventListener ( eventNameMap [ `on${ type } Leave` ] , nativeEvent => {
301294 const leaveEvent = createThreeEvent ( nativeEvent , false )
302- props [ `on${ type } Leave` ] ?.( leaveEvent )
295+ context . props [ `on${ type } Leave` ] ?.( leaveEvent )
303296 hoveredCanvas = false
304297
305298 for ( const object of hoveredSet ) {
@@ -330,7 +323,6 @@ function createHoverEventRegistry(type: "Mouse" | "Pointer", context: Context, p
330323function createDefaultEventRegistry (
331324 type : "onMouseDown" | "onMouseUp" | "onPointerDown" | "onPointerUp" | "onWheel" ,
332325 context : Context ,
333- props : CanvasProps ,
334326 options ?: AddEventListenerOptions ,
335327) {
336328 const registry = createRegistry < Object3D > ( )
@@ -356,7 +348,7 @@ function createDefaultEventRegistry(
356348
357349 if ( ! event . stopped ) {
358350 // @ts -expect-error TODO: fix type-error
359- props [ type ] ?.( event )
351+ context . props [ type ] ?.( event )
360352 }
361353 } ,
362354 options ,
@@ -374,27 +366,27 @@ function createDefaultEventRegistry(
374366/**
375367 * Initializes and manages event handling for all `Instance<Object3D>`.
376368 */
377- export function createEvents ( context : Context , props : CanvasProps ) {
369+ export function createEvents ( context : Context ) {
378370 // onMouseMove/onMouseEnter/onMouseLeave
379- const hoverMouseRegistry = createHoverEventRegistry ( "Mouse" , context , props )
371+ const hoverMouseRegistry = createHoverEventRegistry ( "Mouse" , context )
380372 // onPointerMove/onPointerEnter/onPointerLeave
381- const hoverPointerRegistry = createHoverEventRegistry ( "Pointer" , context , props )
373+ const hoverPointerRegistry = createHoverEventRegistry ( "Pointer" , context )
382374
383375 // onClick/onClickMissed
384- const missableClickRegistry = createMissableEventRegistry ( "onClick" , context , props )
376+ const missableClickRegistry = createMissableEventRegistry ( "onClick" , context )
385377 // onContextMenu/onContextMenuMissed
386- const missableContextMenuRegistry = createMissableEventRegistry ( "onContextMenu" , context , props )
378+ const missableContextMenuRegistry = createMissableEventRegistry ( "onContextMenu" , context )
387379 // onDoubleClick/onDoubleClickMissed
388- const missableDoubleClickRegistry = createMissableEventRegistry ( "onDoubleClick" , context , props )
380+ const missableDoubleClickRegistry = createMissableEventRegistry ( "onDoubleClick" , context )
389381
390382 // Default mouse-events
391- const mouseDownRegistry = createDefaultEventRegistry ( "onMouseDown" , context , props )
392- const mouseUpRegistry = createDefaultEventRegistry ( "onMouseUp" , context , props )
383+ const mouseDownRegistry = createDefaultEventRegistry ( "onMouseDown" , context )
384+ const mouseUpRegistry = createDefaultEventRegistry ( "onMouseUp" , context )
393385 // Default pointer-events
394- const pointerDownRegistry = createDefaultEventRegistry ( "onPointerDown" , context , props )
395- const pointerUpRegistry = createDefaultEventRegistry ( "onPointerUp" , context , props )
386+ const pointerDownRegistry = createDefaultEventRegistry ( "onPointerDown" , context )
387+ const pointerUpRegistry = createDefaultEventRegistry ( "onPointerUp" , context )
396388 // Default wheel-event
397- const wheelRegistry = createDefaultEventRegistry ( "onWheel" , context , props , { passive : true } )
389+ const wheelRegistry = createDefaultEventRegistry ( "onWheel" , context , { passive : true } )
398390
399391 return {
400392 /**
0 commit comments