Skip to content

Commit

Permalink
feat: allow vertical scenes of small screens
Browse files Browse the repository at this point in the history
  • Loading branch information
VmMad committed Feb 20, 2024
1 parent 59655f3 commit 4cce639
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions client/src/features/visualizer-threejs/CameraControls.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { CameraControls as DreiCameraControls } from "@react-three/drei";
import { getCameraAngles } from "./utils";
import React, { useEffect } from "react";
import { useThree } from "@react-three/fiber";
import { CanvasElement } from "./enums";
import { useTangleStore } from "./store";
import { useTangleStore, useConfigStore } from "./store";
import { VISUALIZER_PADDINGS } from "./constants";

const CameraControls = () => {
Expand All @@ -15,6 +14,8 @@ const CameraControls = () => {
const zoom = useTangleStore((s) => s.zoom);
const get = useThree((state) => state.get);
const mesh = get().scene.getObjectByName(CanvasElement.TangleWrapperMesh);
const canvasDimensions = useConfigStore((state) => state.dimensions);
const camera = useThree((state) => state.camera);

// Set fixed zoom
useEffect(() => {
Expand All @@ -24,11 +25,22 @@ const CameraControls = () => {
}
}, [controls, zoom, shouldLockZoom]);

/**
* Sets the scene to be vertical or horizontal
* depending on the canvas dimensions.
*/
useEffect(() => {
if (camera && canvasDimensions.width && canvasDimensions.height) {
const renderVerticalScene = canvasDimensions.width < canvasDimensions.height;
const cameraUp: [number, number, number] = renderVerticalScene ? [-1, 0, 0] : [0, 1, 0];
camera.up.set(...cameraUp);
}
}, [canvasDimensions, camera]);

// Fix to TangleMesh
useEffect(() => {
if (controls.current && mesh) {
controls.current.fitToBox(mesh, false, { ...VISUALIZER_PADDINGS });
controls.current.setOrbitPoint(0, 0, 0);
setShouldLockZoom(true);
}
}, [controls, mesh]);
Expand Down

0 comments on commit 4cce639

Please sign in to comment.