Have you ever imagined how these graphics and games run on a web browser? What is the main technology behind it? Off course, it is not possible by just using HTML and CSS. In previous days we used WebGL for this task. So we are introducing you to Three.js and how to implement it in React in this blog.
You already know what Three.js is and what wonders it can do! But, if you don’t, then Three.js is a javascript library that can help you render 3D objects and scenes on a web browser.
In this article, we’ll discuss minor adjustments to implement Three.js in React. While you can use third-party wrappers such as react-three-fiber to quickly set up your project, if you prefer to avoid using a wrapper or are curious about the core workings of Three.js, this blog post is for you.
First, install a boilerplate react app. I’ll use create-react-app for this. Use the following npm command to get started.
npx create-react-app threejs-react
Next, create a new file ThreeCube.js in the src folder. Add the following starter code to this file:
src/ThreeCube.js
import React from "react"; const ThreeCube = () => { return ( <div> <h3>Three Cube</h3> </div> ); }; export default ThreeCube;
This is how your App.js file should look now:
src/App.js
import React from "react"; import ThreeCube from "./ThreeCube"; const App = () => { return ( <div> <h1>Three.js-React Integration</h1> <ThreeCube /> </div> ); }; export default App;
That’s it for our starter code.
Installing Three.js
We are ready to install the Three.js package now. Use the following npm command to install Three.js:
Terminal command
npm install three
That’s it. We’re done. Now we can use Three.js in React.
Our next step is to create a 3D cube. All code we write from now on will go into our ThreeCube.js file. We’ll use the getting started code from the official Three.js website. You can view the getting started code here.
If we paste the code in the above example into a useEffect hook in our react app, the code should technically work. So let’s do that.
Let’s add the cube example from the official docs to a useEffect hook in our ThreeCube.js file. We’ll also need to import three from the three libraries we installed earlier.
Modify your ThreeCube.js file so that it looks like this:
src/ThreeCube.js
import React, { useEffect } from "react"; import * as THREE from "three"; const ThreeCube = () => { useEffect(() => { var scene = new THREE.Scene(); var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); var geometry = new THREE.BoxGeometry(); var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); var cube = new THREE.Mesh(geometry, material); scene.add(cube); camera.position.z = 5; var animate = function () { requestAnimationFrame(animate); cube.rotation.x += 0.01; cube.rotation.y += 0.01; renderer.render(scene, camera); }; animate(); }, []); return ( <> <div></div> </> ); }; export default ThreeCube;
This is how our web app should be looking at the moment:
In conclusion, Three.js in React is a versatile tool for generating 3D images and shapes on the client side, regardless of the platform you’re working on. Its functionality and flexibility make it an ideal choice for developers looking to create immersive web experiences or visualizations. With the help of React Three.js, the possibilities for creating stunning 3D graphics are virtually limitless.
Leverage the benefits of Three.js in React for your business with Mindbowser. Hire a skilled react.js developer today and create stunning 3D visuals and custom solutions. Contact us to get started.
How to Effectively Hire and Manage a Remote Team of Developers.
Download NowThe Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork