Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DevicePhoneViewport.tsx

122 lines4.9 KB

1import { useEffect, useRef } from "react";
2import type { PhoneViewer } from "@t3tools/client-runtime/device/phone-viewer";
3import type {
4 DeviceAccessorySource,
5 DeviceModelSource,
6} from "@t3tools/client-runtime/device/model";
7import type { DeviceShapeProfile } from "@t3tools/client-runtime/device/shape-profile";
8import type { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction";
9import type { DeviceScreenSize, DeviceStreamClient } from "@t3tools/client-runtime/device/stream";
10import { mountDeviceViewer } from "./mountDeviceViewer";
11​
12const loadPhoneViewer = () => import("@t3tools/client-runtime/device/phone-viewer");
13​
14export function DevicePhoneViewport(props: {
15 readonly profile: DeviceShapeProfile;
16 readonly model: DeviceModelSource | null;
17 readonly accessory: DeviceAccessorySource | null;
18 readonly source: HTMLCanvasElement;
19 readonly onFrameListener: (listener: (() => void) | null) => void;
20 readonly onResetReady: (reset: (() => void) | null) => void;
21 readonly onInputCancel: (cancel: (() => void) | null) => void;
22 readonly client: DeviceStreamClient;
23 readonly screen: DeviceScreenSize | null;
24 readonly foldAngle: number | null;
25 readonly onUnavailable: () => void;
26}) {
27 const viewerRef = useRef<PhoneViewer | null>(null);
28 const interactionRef = useRef<ReturnType<typeof createPhoneInteraction> | null>(null);
29 const screenRef = useRef(props.screen);
30 const foldAngleRef = useRef(props.foldAngle);
31 const profileRef = useRef(props.profile);
32 const modelRef = useRef(props.model);
33 const accessoryRef = useRef(props.accessory);
34 const { source, onFrameListener, client, onInputCancel, onResetReady, onUnavailable } = props;
35​
36 useEffect(() => {
37 foldAngleRef.current = props.foldAngle;
38 viewerRef.current?.setFoldAngle(props.foldAngle);
39 }, [props.foldAngle]);
40​
41 useEffect(() => {
42 screenRef.current = props.screen;
43 profileRef.current = props.profile;
44 modelRef.current = props.model;
45 accessoryRef.current = props.accessory;
46 interactionRef.current?.end();
47 viewerRef.current?.setModel(props.model);
48 viewerRef.current?.setAccessory(props.accessory);
49 viewerRef.current?.setScreen(props.screen, props.profile);
50 }, [props.screen, props.profile, props.model, props.accessory]);
51​
52 // The 3D viewer lives as long as its canvas; later prop changes are pushed into it above.
53 const mountViewer = (canvas: HTMLCanvasElement | null) => {
54 const host = canvas?.parentElement;
55 if (!canvas || !host) return;
56 return mountDeviceViewer(canvas, host, {
57 load: () =>
58 loadPhoneViewer().then(
59 ({ createPhoneViewer }) =>
60 () =>
61 createPhoneViewer({
62 canvas,
63 source,
64 onUnavailable,
65 onModelError: (cause) => console.warn("Device 3D asset could not load", cause),
66 profile: profileRef.current,
67 model: modelRef.current,
68 accessory: accessoryRef.current,
69 foldAngle: foldAngleRef.current,
70 }),
71 ),
72 touch: (phase, point) => client.sendTouch(phase, point.x, point.y),
73 viewerRef,
74 interactionRef,
75 onInputCancel,
76 onResetReady,
77 onFrameListener,
78 onUnavailable,
79 ready: ({ viewer }) => {
80 viewer.setScreen(screenRef.current);
81 return { frame: viewer.frameUpdated };
82 },
83 });
84 };
85​
86 const point = (event: React.PointerEvent<HTMLCanvasElement>) => {
87 const rect = event.currentTarget.getBoundingClientRect();
88 return {
89 x: (event.clientX - rect.left) / rect.width,
90 y: (event.clientY - rect.top) / rect.height,
91 };
92 };
93 return (
94 <div className="absolute inset-0">
95 <div className="absolute inset-0">
96 <div
97 aria-hidden
98 className="pointer-events-none absolute bottom-[6%] left-1/2 h-5 w-2/5 -translate-x-1/2 rounded-full bg-foreground/10 blur-xl"
99 />
100 <canvas
101 ref={mountViewer}
102 aria-label="Interactive 3D device. Drag the screen to interact. Drag outside it or swipe with two fingers to turn."
103 className="size-full touch-none"
104 onPointerDown={(event) => {
105 if (event.button !== 0) return;
106 if (!interactionRef.current?.begin(event.pointerId, point(event), event.altKey)) return;
107 event.preventDefault();
108 event.currentTarget.setPointerCapture(event.pointerId);
109 (event.currentTarget.closest('[role="application"]') as HTMLElement | null)?.focus();
110 }}
111 onPointerMove={(event) => interactionRef.current?.move(event.pointerId, point(event))}
112 onPointerUp={(event) => {
113 interactionRef.current?.move(event.pointerId, point(event));
114 interactionRef.current?.end(event.pointerId);
115 }}
116 onPointerCancel={(event) => interactionRef.current?.end(event.pointerId)}
117 onLostPointerCapture={(event) => interactionRef.current?.end(event.pointerId)}
118 />
119 </div>
120 </div>
121 );