Skip to content

Add camera controls transition event listeners #1398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2023
Merged

Add camera controls transition event listeners #1398

merged 1 commit into from
Apr 11, 2023

Conversation

sinedie
Copy link
Contributor

@sinedie sinedie commented Apr 11, 2023

Why

Camera controls won't work with ondemand frame loop

What

Added the event listeners on camera controls docs for transitions to work on demand frame loop

Checklist

  • Documentation updated (No need to change any documentation)
  • Storybook entry added (camera controls doesn't have storybook)
  • Ready to be merged

Camera controls won't work with ondemand frame loop
@vercel
Copy link

vercel bot commented Apr 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
drei ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 11, 2023 7:04pm

@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 413bb74:

Sandbox Source
epic-river-gumrws Configuration
Ground reflections and video textures Configuration
arc-x-pmndrs-colors Configuration

@sinedie
Copy link
Contributor Author

sinedie commented Apr 11, 2023

Workaround for someone who need it, but PR doesn't get merged

import { useRef } from 'react';
import { Canvas, useThree } from '@react-three/fiber';
import { CameraControls, CameraControlsProps } from '@react-three/drei';
import CameraControlsImpl from 'camera-controls';

const Controls = forwardRef<CameraControlsImpl, CameraControlsProps>(
  (props, ref) => {
    const { invalidate, controls } = useThree();

    const callback = () => {
      invalidate();
    };

    useEffect(() => {
      if (!controls) return;

      controls.addEventListener('control', callback);
      controls.addEventListener('transitionstart', callback);
      controls.addEventListener('wake', callback);

      return () => {
        controls.removeEventListener('control', callback);
        controls.removeEventListener('transitionstart', callback);
        controls.removeEventListener('wake', callback);
      };
    }, [controls]);

    return <CameraControls ref={ref} {...props} />;
  }
);

function Main() {
    const cameraControlsRef = useRef<CameraControls | null>(null);

    return (
        <Canvas frameloop="demand">
            <Controls ref={cameraControlsRef} makeDefault />
             <mesh>
                <boxGeometry />
                <meshStandardMaterial />
              </mesh>

        </Canvas>
    )

@drcmda drcmda merged commit 96e62dc into pmndrs:master Apr 11, 2023
@github-actions
Copy link

🎉 This PR is included in version 9.65.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants