diff --git a/src/components/mindset/components/Scene/Board/index.tsx b/src/components/mindset/components/Scene/Board/index.tsx index 903c89049..6bf72c1e8 100644 --- a/src/components/mindset/components/Scene/Board/index.tsx +++ b/src/components/mindset/components/Scene/Board/index.tsx @@ -1,5 +1,5 @@ import { useThree } from '@react-three/fiber' -import { Fragment, useEffect, useMemo } from 'react' +import { Fragment, useMemo } from 'react' import { Vector3 } from 'three' import { useDataStore } from '~/stores/useDataStore' import { Link } from '~/types' @@ -25,34 +25,7 @@ type LinkExtended = Link & { export const Board = () => { const state = useThree() const { dataInitial } = useDataStore((s) => s) - const { camera, viewport } = state - - useEffect(() => { - const orthoCamera = camera as THREE.OrthographicCamera - - const handleWheel = (event: WheelEvent) => { - event.preventDefault() // Prevent default scrolling behavior - - if (event.ctrlKey) { - // Zoom the camera when ctrlKey is pressed - orthoCamera.zoom += event.deltaY * -0.1 // Adjust zoom level - orthoCamera.zoom = Math.max(2, Math.min(orthoCamera.zoom, 20)) // Clamp zoom - } - - // Move the camera left/right when ctrlKey is NOT pressed - orthoCamera.position.x += event.deltaX * 0.1 // Horizontal movement - - orthoCamera.updateProjectionMatrix() // Update projection matrix - } - - // Add the event listener - window.addEventListener('wheel', handleWheel, { passive: false }) - - return () => { - // Cleanup event listener - window.removeEventListener('wheel', handleWheel) - } - }, [camera]) + const { viewport } = state const processedData = useMemo(() => { if (!dataInitial) { diff --git a/src/components/mindset/components/Scene/index.tsx b/src/components/mindset/components/Scene/index.tsx index d3ef6ccb9..a861bb744 100644 --- a/src/components/mindset/components/Scene/index.tsx +++ b/src/components/mindset/components/Scene/index.tsx @@ -1,12 +1,12 @@ import { OrthographicCamera } from '@react-three/drei' import { Canvas, useFrame, useThree } from '@react-three/fiber' -import { memo } from 'react' +import { memo, useEffect } from 'react' import { usePlayerStore } from '~/stores/usePlayerStore' import { Board } from './Board' const CameraController = () => { - const { camera, viewport } = useThree() // Access the Three.js camera - const playerRef = usePlayerStore((s) => s.playerRef) // Ref to store the current `playerRef` + const { camera, viewport } = useThree() + const playerRef = usePlayerStore((s) => s.playerRef) useFrame(() => { if (playerRef) { @@ -16,7 +16,38 @@ const CameraController = () => { } }) - return null // No React-rendered output + return null +} + +const CanvasZoomHandler = () => { + const { camera, gl } = useThree() + + useEffect(() => { + const orthoCamera = camera as THREE.OrthographicCamera + + const handleWheel = (event: WheelEvent) => { + event.preventDefault() + + if (event.ctrlKey) { + orthoCamera.zoom += event.deltaY * -0.1 + orthoCamera.zoom = Math.max(2, Math.min(orthoCamera.zoom, 20)) + } + + orthoCamera.position.x += event.deltaX * 0.1 // + + orthoCamera.updateProjectionMatrix() + } + + const canvas = gl.domElement + + canvas.addEventListener('wheel', handleWheel, { passive: false }) + + return () => { + canvas.removeEventListener('wheel', handleWheel) + } + }, [camera, gl]) + + return null } export const Scene = memo(() => { @@ -25,11 +56,10 @@ export const Scene = memo(() => { return (