Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit panning distance in react three fiber MapControls

I‘m using React Three Fiber and drei. I'm wondering how to limit the maximum panning distance with the MapControls. There are some solutions online on how to archive it with plain three.js but nothing using the MapControls or OrbitControls from drei and r3f.

I tried this but once I reach the limit the camera glitches weirdly.

function Controls() {
  const { camera } = useThree();

  useFrame(() => {
    camera.position.x = THREE.MathUtils.clamp(camera.position.x, -90, 90)
    camera.position.y = THREE.MathUtils.clamp(camera.position.y, -90, 90)
  })
  
  return (
    <MapControls />
  )
}

Thanks for your help

Alexander

like image 611
Alexander Hörl Avatar asked Jan 20 '26 06:01

Alexander Hörl


2 Answers

Based on this answer a solution would be to create a custom 'Controls' component looking like this.

const Controls = () => {
  const { camera } = useThree()
  const controlsRef = useRef()    

  useEffect(() => {
    controlsRef.current.addEventListener('change', function () {
      if (this.target.y < -10) {
        this.target.y = -10
        camera.position.y = -10
      } else if (this.target.y > 10) {
        this.target.y = 10
        camera.position.y = 10
      }
    })
  }, [])

  return (
    <MapControls ref={controlsRef} enableZoom={false} enableRotate={false} />
  )
}

Which can then be used as a child to the Canvas component.

<Canvas>
  <Controls />
</Canvas>
like image 55
Alexander Hörl Avatar answered Jan 22 '26 19:01

Alexander Hörl


My solution is based on the answers above

  const { camera, size } = useThree();

  const limitPanningDistance = useCallback(
    (e?: THREE.Event) => {
      // 704.5 102
      // 1056.75 320

      // Returns the drag container width and height
      const [w, h] = [1920, 1080]

      const pan = (w * camera.zoom - size.width) / 2 / camera.zoom;
      const vertical = (h * camera.zoom - size.height) / 2 / camera.zoom;

      // console.log('pan vertical', pan, vertical);

      const maxX = pan;
      const minX = -pan;
      const maxY = vertical;
      const minY = -vertical;
      const x = e?.target.target.x;
      const y = e?.target.target.y;
      if (x < minX || x > maxX) {
        e?.target.target.setX(x < minX ? minX : maxX);
        camera.position.setX(x < minX ? minX : maxX);
      }
      if (y < minY || y > maxY) {
        e?.target.target.setY(y < minY ? minY : maxY);
        camera.position.setY(y < minY ? minY : maxY);
      }
    },
    [camera.zoom, size]
  );
      <MapControls
        enableRotate={false}
        minZoom={1}
        maxZoom={2}
        onChange={(e) => {
          // console.log(e?.target);
          limitPanningDistance(e);
        }}
        makeDefault
      />
like image 31
xiaotian Avatar answered Jan 22 '26 20:01

xiaotian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!