|
1 | | -import { createMemo, onCleanup, type Ref } from "solid-js" |
| 1 | +import { createEffect, createMemo, onCleanup, type Ref } from "solid-js" |
2 | 2 | import type { Event } from "three" |
3 | 3 | import { OrbitControls as ThreeOrbitControls } from "three-stdlib" |
4 | | -import { useThree, type S3 } from "../../src/index.ts" |
| 4 | +import { useFrame, useThree, type S3 } from "../../src/index.ts" |
5 | 5 | import { manageProps } from "../../src/props.ts" |
6 | | -import { every, whenEffect } from "../../src/utils/conditionals.ts" |
| 6 | +import { whenEffect } from "../../src/utils/conditionals.ts" |
7 | 7 | import { processProps } from "./process-props.ts" |
8 | 8 |
|
9 | 9 | export type OrbitControlsProps = S3.ClassProps<typeof ThreeOrbitControls> & { |
@@ -39,36 +39,46 @@ export function OrbitControls(props: OrbitControlsProps) { |
39 | 39 | ], |
40 | 40 | ) |
41 | 41 | const three = useThree() |
42 | | - const controls = createMemo(() => new ThreeOrbitControls(config.camera ?? three.camera)) |
43 | | - |
44 | | - whenEffect(controls, controls => { |
45 | | - controls.connect(props.domElement ?? three.gl.domElement) |
46 | | - onCleanup(() => controls.dispose()) |
| 42 | + const controls = createMemo<ThreeOrbitControls>(previous => { |
| 43 | + previous?.dispose() |
| 44 | + return new ThreeOrbitControls(config.camera ?? three.camera) |
47 | 45 | }) |
48 | 46 |
|
49 | | - whenEffect( |
50 | | - every(controls, () => config.onStart), |
51 | | - ([controls, callback]) => { |
52 | | - controls.addEventListener("start", callback) |
53 | | - onCleanup(() => controls.removeEventListener("start", callback)) |
54 | | - }, |
55 | | - ) |
56 | | - whenEffect( |
57 | | - every(controls, () => config.onChange), |
58 | | - ([controls, callback]) => { |
59 | | - controls.addEventListener("change", callback) |
60 | | - onCleanup(() => controls.removeEventListener("change", callback)) |
61 | | - }, |
62 | | - ) |
63 | | - whenEffect( |
64 | | - every(controls, () => config.onEnd), |
65 | | - ([controls, callback]) => { |
66 | | - controls.addEventListener("end", callback) |
67 | | - onCleanup(() => controls.removeEventListener("end", callback)) |
68 | | - }, |
69 | | - ) |
| 47 | + useFrame(() => controls().update()) |
| 48 | + |
| 49 | + whenEffect(controls, controls => controls.connect(props.domElement ?? three.gl.domElement)) |
| 50 | + |
| 51 | + createEffect(() => { |
| 52 | + const callback = config.onStart |
| 53 | + if (!callback) return |
| 54 | + const _controls = controls() |
| 55 | + _controls.addEventListener("start", callback) |
| 56 | + onCleanup(() => _controls.removeEventListener("start", callback)) |
| 57 | + }) |
| 58 | + createEffect(() => { |
| 59 | + const callback = config.onChange |
| 60 | + if (!callback) return |
| 61 | + const _controls = controls() |
| 62 | + _controls.addEventListener("change", callback) |
| 63 | + onCleanup(() => _controls.removeEventListener("change", callback)) |
| 64 | + }) |
| 65 | + createEffect(() => { |
| 66 | + const callback = config.onEnd |
| 67 | + if (!callback) return |
| 68 | + const _controls = controls() |
| 69 | + _controls.addEventListener("end", callback) |
| 70 | + onCleanup(() => _controls.removeEventListener("end", callback)) |
| 71 | + }) |
70 | 72 |
|
71 | 73 | manageProps(controls, rest) |
72 | 74 |
|
73 | 75 | return null! |
74 | 76 | } |
| 77 | + |
| 78 | +function createEventListener() { |
| 79 | + const callback = config.onStart |
| 80 | + if (!callback) return |
| 81 | + const _controls = controls() |
| 82 | + _controls.addEventListener("start", callback) |
| 83 | + onCleanup(() => _controls.removeEventListener("start", callback)) |
| 84 | +} |
0 commit comments