How to add Orbit Controls to a ThreeJS scene in ReactJS

Import the Orbit Controls, set them up and update them in the animation frame.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 11/08/2023 at 12:22
Last Updated on 11/10/2023 at 23:28
<p>Two flowers that represent ReactJS and ThreeJS side by side. Beneath them sits the text "Orbit Controls."</p>

Import the Orbit Controls, set them up and update them in the animation frame.

SubscribeDownload Open Source Project

We recommend that you clone our Open Source React-Redux Starter Project, checking out the tutorial/three-js/starter branch and carrying out the steps below. The changes can be found on the tutorial/three-js/orbital-controls branch.

git clone git@github.com:delasign/react-redux-starter-project.git
View Repository

Step One: Setup the Project

<p>A screenshot of chrome showing the cube geometry and the shader material.</p>

Follow the tutorial below to learn how to create a ThreeJS project that implements a shader on a box geometry.

How to add a shader to a box geometry in ThreeJS

Step Two: Import the Controls

<p>A screenshot of VSCode highlighting how to import the orbit controls.</p>

At the top of your file, import the Orbit Controls.

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';

Step Three: Initialize the Controls

<p>A screenshot of VSCode highlighting how to setup the orbit controls.</p>

Within your react component, after declaring camera and renderer, initialize the Orbit Controls using code similar to the one below.

const controls = new OrbitControls( camera, renderer.domElement );

Step Four: Update Controls in Animation

<p>A screenshot of VSCode highlighting that you need to update the controls in the animation frame.</p>

Within your animation loop, call the following function:

controls.update()

Step Five: Test

<p>A screenshot of Chrome showing that the orbit controls work and that the cube can now be seen from different angles and distances.</p>

Run the code and confirm that you can now rotate, move and zoom in and out using your trackpad and keyboard.

Looking to learn more about ReactJS and ThreeJS ?

Search our blog to find educational content on learning how to use ReactJS and ThreeJS.

Search our BlogReact, ThreeJS & GLSL Resources

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe