Skip to content

Commit a68a122

Browse files
committed
docs: update demo
1 parent 9eea980 commit a68a122

7 files changed

Lines changed: 274 additions & 129 deletions

File tree

playground/App.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { A, Route, Router } from "@solidjs/router"
22
import type { ParentProps } from "solid-js"
33
import * as THREE from "three"
4-
import { extend, T } from "../src/index.ts"
5-
import { Basic } from "./examples/Basic.tsx"
4+
import { Canvas, extend, T } from "../src/index.ts"
5+
import { SimpleSolar } from "./examples/SimpleSolar.tsx"
66
import "./index.css"
77

88
extend(THREE)
@@ -22,26 +22,26 @@ function Layout(props: ParentProps) {
2222
}}
2323
>
2424
<A
25-
href="/basic"
25+
href="/"
2626
style={{
2727
color: "white",
2828
"text-decoration": "none",
2929
padding: "5px 10px",
3030
display: "block",
3131
}}
3232
>
33-
Basic Example
33+
Home
3434
</A>
3535
<A
36-
href="/"
36+
href="/simple-solar"
3737
style={{
3838
color: "white",
3939
"text-decoration": "none",
4040
padding: "5px 10px",
4141
display: "block",
4242
}}
4343
>
44-
Home
44+
Simple Solar
4545
</A>
4646
</nav>
4747
{props.children}
@@ -52,14 +52,19 @@ function Layout(props: ParentProps) {
5252
export function App() {
5353
return (
5454
<Router root={Layout}>
55-
<Route path="/basic" component={Basic} />
55+
<Route path="/simple-solar" component={SimpleSolar} />
5656
<Route
5757
path="/"
5858
component={() => (
59-
<T.Mesh>
60-
<T.BoxGeometry />
61-
<T.MeshBasicMaterial color="gray" />
62-
</T.Mesh>
59+
<Canvas
60+
style={{ width: "100vw", height: "100vh" }}
61+
camera={{ position: new THREE.Vector3(0, 0, 15) }}
62+
>
63+
<T.Mesh>
64+
<T.BoxGeometry />
65+
<T.MeshBasicMaterial color="gray" />
66+
</T.Mesh>
67+
</Canvas>
6368
)}
6469
/>
6570
</Router>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { createMemo, onCleanup, type Ref } from "solid-js"
2+
import type { Event } from "three"
3+
import { OrbitControls as ThreeOrbitControls } from "three-stdlib"
4+
import { useThree, type S3 } from "../../src/index.ts"
5+
import { manageProps } from "../../src/props.ts"
6+
import { every, whenEffect } from "../../src/utils/conditionals.ts"
7+
import { processProps } from "./process-props.ts"
8+
9+
export type OrbitControlsProps = S3.ClassProps<typeof ThreeOrbitControls> & {
10+
ref?: Ref<ThreeOrbitControls>
11+
camera?: S3.CameraType
12+
domElement?: HTMLElement
13+
enableDamping?: boolean
14+
onChange?: (e?: Event<"change", ThreeOrbitControls>) => void
15+
onEnd?: (e?: Event<"end", ThreeOrbitControls>) => void
16+
onStart?: (e?: Event<"start", ThreeOrbitControls>) => void
17+
regress?: boolean
18+
target?: S3.Vector3
19+
keyEvents?: boolean | HTMLElement
20+
}
21+
22+
export function OrbitControls(props: OrbitControlsProps) {
23+
const [config, rest] = processProps(
24+
props,
25+
{
26+
enableDamping: true,
27+
keyEvents: false,
28+
},
29+
[
30+
"camera",
31+
"regress",
32+
"domElement",
33+
"keyEvents",
34+
"onChange",
35+
"onStart",
36+
"onEnd",
37+
"object",
38+
"dispose",
39+
],
40+
)
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())
47+
})
48+
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+
)
70+
71+
manageProps(controls, rest)
72+
73+
return null!
74+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { MergeProps } from "solid-js"
2+
import { mergeProps } from "solid-js"
3+
import type { KeyOfOptionals } from "./type-utils.ts"
4+
5+
export function defaultProps<T, K extends KeyOfOptionals<T>>(
6+
props: T,
7+
defaults: Required<Pick<T, K>>,
8+
): MergeProps<[Required<Pick<T, K>>, T]> {
9+
return mergeProps(defaults, props)
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { splitProps } from "solid-js"
2+
import { defaultProps } from "./default-props.ts"
3+
import type { KeyOfOptionals } from "./type-utils.ts"
4+
5+
export function processProps<
6+
const TProps,
7+
const TKey extends KeyOfOptionals<TProps>,
8+
const TSplit extends readonly (keyof TProps)[],
9+
>(props: TProps, defaults: Required<Pick<TProps, TKey>>, split?: TSplit) {
10+
return splitProps(defaultProps(props, defaults), split ?? [])
11+
}

playground/controls/type-utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export type Args<T> = T extends new (...args: any) => any ? ConstructorParameters<T> : T
2+
3+
export type Mandatory<T, K extends keyof T> = T & { [P in K]-?: T[P] }
4+
5+
type OmitFunctionProperties<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
6+
/** Overwrites the properties in `T` with the properties from `O`. */
7+
export type Overwrite<T, O> = Omit<T, OmitFunctionProperties<O>> & O
8+
9+
export type KeyOfOptionals<T> = keyof {
10+
[K in keyof T as T extends Record<K, T[K]> ? never : K]: T[K]
11+
}
12+
13+
/** Allows using a TS v4 labeled tuple even with older typescript versions */
14+
export type NamedArrayTuple<T extends (...args: any) => any> = Parameters<T>
15+
16+
export type Ref<TRef> = TRef | ((value: TRef) => void)

playground/examples/Basic.tsx

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)