react-three-fiber
is a powerful Three.js renderer that helps render 3D models and animations for React and its native applications. In this tutorial, you will learn how to configure and build 3D models in a React application.
Today, we’re going to learn how to configure and use react-three-fiber
for building and displaying 3D models and animations in React and React Native applications.
This tutorial is for developers who want to learn more about 3D model animations in the web using React and for anyone who’s had limitations with Three.js like inability to create canvas, bind user events like click
events and start a render loop, react-three-fiber
comes with these methods. We’ll be building a 3D model to better understand how to build Three.js 3D models using react-three-fiber
.
react-three-fiber
Three.js is a library that makes it easier to create 3D graphics in the browser, it uses a canvas + WebGL to display the 3D models and animations, you can learn more here.
react-three-fiber is a React renderer for Three.js on the web and react-native, it is a boost to the speed at which you create 3D models and animations with Three.js, some examples of sites with 3D models and animations can be found here. react-three-fiber
reduces the time spent on animations because of its reusable components, binding events and render loop, first let’s take a look at what is Three.js.
react-three-fiber
allows us to build components of threeJS
code using React state, hooks and props, it also comes with the following elements:
react-three-fiber
To use react-three-fiber
, you start by using the commands below:
npm i three react-three-fiber
yarn add three react-three-fiber
Note: For _react-three-fiber_
to work, you’ll need to install _three_
(Three.js) as we did above.
Here we are going to build a 3D ludo dice model using react-three-fiber
like we have in the video below.
3D Ludo Dice Model from Smashing Magazine on Vimeo.
We will be using create-react-app
to initialize our project, to do that let’s execute the command below on our terminal.
create-react-app react-three-fiber-ludo-model
The command above initializes a React project within our local machine, next let’s cd
into the directory and install our packages react-three-fiber
and three
.
cd react-three-fiber-ludo-model
npm i three react-three-fiber
Once, the packages are installed, let’s start our development server using the command
npm start
The above command should start our project development server in our browser. Next let’s open our project in our text editor of choice, inside our project src
folder, delete the following files: App.css
, App.test.js
, serviceWorker.js
and setupTests.js
. Next, let’s delete all code that references the deleted files on our App.js
.
For this project, we will need a Box
component for our ludo dices and our App
component that’s given by React.
#react #javascript #three.js #web-development #developer