function App() { // const [dpr, setDpr] = useState(1.5); const { moveAnimation } =…
- User
function App() { // const [dpr, setDpr] = useState(1.5); const { moveAnimation } = useSnapshot(store); const [isDragging, setIsDragging] = useState(false); const [lastX, setLastX] = useState(0); const [targetRotationY, setTargetRotationY] = useState(Math.PI * 0.5); const cameraRef = useRef(null); const fakeLoaderRef = useRef(null);
// POINTER EVENT BOTH USE IN MOUSE AND TOUCH CONTROLS
const handlePointerDown = (clientX: number) => { setIsDragging(true); setLastX(clientX); };
const handlePointerUp = () => { setIsDragging(false); };
const handlePointerMove = (clientX: number) => { if (!isDragging) return;
codeconst deltaX = clientX - lastX; setLastX(clientX); const rotationSpeed = 0.002; setTargetRotationY((prev) => prev - deltaX * rotationSpeed);};
// MOUSE CONTROLS
const handleMouseDown = ( event: React.MouseEvent<HTMLDivElement, MouseEvent> ) => { handlePointerDown(event.clientX); };
const handleMouseUp = () => { handlePointerUp(); };
const handleMouseMove = ( event: React.MouseEvent<HTMLDivElement, MouseEvent> ) => { handlePointerMove(event.clientX); };
// TOUCH CONTROLS
const handleTouchStart = (event: React.TouchEvent) => { if (event.touches.length === 1) { handlePointerDown(event.touches[0].clientX); } };
const handleTouchMove = (event: React.TouchEvent) => { if (event.touches.length === 1) { handlePointerMove(event.touches[0].clientX); } };
const handleTouchEnd = () => { handlePointerUp(); };
return ( <button onClick={() => moveAnimation(24.2, 0, 1, 1, 1.6)}> Eingang / Foyer <button onClick={() => moveAnimation(2.2, 14.2, 5, 3.3, 2.04)}> Kunststofftechnik <button onClick={() => moveAnimation(0.1, 1.7, 3, 1.7, Math.PI * 0.4)}> Fertigung <button onClick={() => moveAnimation(2.8, -12.8, 4, 2.6, 0.84)}> Maschinenbau <button onClick={() => moveAnimation(19, 0.7, 1, 4, -0.36)}> Verwaltung & GF <Canvas
codeonMouseMove={handleMouseMove} onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} onTouchStart={handleTouchStart} onTouchMove={handleTouchMove} onTouchEnd={handleTouchEnd} > <AdaptiveDpr /> <Environment preset="city" /> <OrbitCamera cameraRef={cameraRef} setTargetRotationY={setTargetRotationY} targetRotationY={targetRotationY} fakeLoaderRef={fakeLoaderRef} /> <group position={[-140, 0, 160]}> <Suspense fallback={null}> <ConditionalRender /> <Hauptgebaeude /> <Walls /> <Doors /> <Spawnpoint /> <Labels /> </Suspense> </group> </Canvas> <Loader /> </main> - Assistant
To limit the camera rotation when moving the mouse, you can constrain the value of
targetRotationYto stay within a specific range. This can be implemented by clamping the calculatedtargetRotationYvalue between a minimum and a maximum value in thehandlePointerMovefunction.Here's how you can achieve this:
-
Define the Min and Max Rotation Values: Decide on the minimum and maximum allowable rotation values. These will limit how far the user can rotate the camera.
-
- User
thanks that's work
- Assistant
You're welcome! I'm glad to hear that it worked for you. If you have any more questions or need further assistance, feel free to ask. Happy coding!