The Wayback Machine - https://web.archive.org/web/20240909150243/https://www.geeksforgeeks.org/introduction-to-three-js/
Open In App

Introduction to Three.js

Last Updated : 09 Aug, 2020
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Did you ever imagine that how these graphics and games are 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. WebGL(Web graphics Library) is JavaScript API which is used to render the 3-dimensional objects, 2-dimensional objects and Graphics on the Web-Browser. The WebGL API allows users to experience interactive content on webpages, with GPU acceleration, without having to first download or install any plug-ins. For developers, WebGL provides low-level access to hardware with the familiar code structure of OpenGL ES.

WebGL was created by Mozilla Organization. With all these benefits of WebGL there are some drawbacks of WebGL where Three.js comes into role. WebGL is very low level system that only draws basic objects like point, square and line. To do any meaningful thing with WebGL generally requires quite a bit of code and that is where Three.js comes in. 

What is Three.js?

Three.js is an open source JavaScript library that is used to display the graphics, 3D and 2D objects on the web browser. It uses WebGL API behind the scene.  Three.js allow you to use your GPU(Graphics Processing Unit) to render the Graphics and 3D objects on a canvas in the web browser. since we are using JavaScript so we can also interact with other HTML elements. Three.js was released by Ricardo Cabello in April 2010.

Why we use Three.js?

  • Since Three.js is open source so we can easily watch the source code and understand the functionality of the code(functions).
  • When we use WebGL for Graphics then it doesn’t support most of the browser but Three.js supports most of the browsers.
  • It doesn’t required any third party plugin to run the code.
  • You just need to work on only one programming language JavaScript and off course HTML.

How To Include Three.js in Your Projects?

There are a lot of ways to add Three.js in projects some of them are quite simple and some of them are a little bit complex, however they all ways said that we have to include any one of these files in our project:

  • three.js
  • three.min.js
  • three.module.js

these all the files are available on the GitHub page of Three.js .

Approach 1: (Download the complete project Three.js): The easiest way to do is just download the complete three.js project on your System and use the files from there.

You can download the latest release of three.js from its GitHub page. Once you have downloaded it, open it and look inside the build folder, you’ll find the three scripts there.

Approach 2: (Install the package of Three.js using NPM or YARN): Three.js is also available as a package on NPM. This means that if you have Node.js and NPM set up on your computer, then you can open a command prompt and type:

npm i three

Then, you can import three.js from the three.module.js file into your JavaScript file by referring to the three package:

import * as THREE from "three";

And, if you prefer Yarn over NPM then also you can add the package using following command in the terminal window:

yarn add three

Approach 3: (Use the CDN Link): You can link the files from a CDN (Content Delivery Network), which is a remote site dedicated to hosting files so that you can use them in a website.

In fact, you can even use the Three.js.org site as a CDN. You can link the three.js file using this link: https://threejs.org/build/three.js, and include it in your HTML like this:

<script src="https://threejs.org/build/three.js"></script>

But there is a problem in using three.js.org CDN link by using this link you’ll always works on an updated version. It is good when we are in development mode but if we talked about production this isn’t good if any function or syntax change with the update your code will stop working so we suggest to use CDN from these sites:

And that’s it by using any of these approach you can use Three.js in your project.



Similar Reads

Creating a Navigation Bar with three Different Alignments using CSS
In this article, we are going to create a navigation bar with three different positions i.e left, right and Centre. To understand this article we need to understand some basic HTML and CSS. Approach: Here we are going to make a navigation bar for EBook websites.The tags we are going to use for the Navigation bar is header, image, nav, button. Here
5 min read
How to display bootstrap carousel with three post in each slide?
A Bootstrap Carousel is a slideshow for rotating through a series of contents. It is built with CSS and Javascript. It works with a series of photos, images, texts, etc. It can be used as an image slider for showcasing a huge amount of content within a small space on the web page, as it works on the principle of dynamic presentations. Syntax: &lt;d
5 min read
How to iterate over three paragraphs and sets their color property red in jQuery ?
In this article, we will learn how to iterate over three paragraphs and sets their color property red in jQuery. We have three paragraphs, and we want to iterate on these paragraphs and set their color property to red. Approach: To do that, first, we add a button and on click this button, a function is called which name is fun() and in this functio
1 min read
Javascript Program to Count Inversions of size three in a given array
Given an array arr[] of size n. Three elements arr[i], arr[j] and arr[k] form an inversion of size 3 if a[i] > a[j] >a[k] and i Example :   Input: {8, 4, 2, 1} Output: 4 The four inversions are (8,4,2), (8,4,1), (4,2,1) and (8,2,1). Input: {9, 6, 4, 5, 8} Output: 2 The two inversions are {9, 6, 4} and {9, 6, 5} We have already discussed inversion c
4 min read
Object is undefined" error when trying to rotate object in three.js
In this article, we will see the "Uncaught TypeError: Cannot read properties of undefined (reading 'rotation') at animate". It is a type error for a variable, in Three .js, this error occurs when the variable is declared, but the value is not assigned or the value is undefined. We can observe from the error, message that this error occurred, when t
3 min read
What is the cascading order of the three types of CSS ?
Cascading Style Sheets (CSS) is a style sheet language used for describing the look and formatting of a document written in a markup language. One of the most important aspects of CSS is the cascading order, which determines the priority of styles applied to an element. In this article, we will explain the different cascading methods that can be us
5 min read
What do these three dots (...) in React do ?
In React the three dots (...) notation is used as the Spread syntax and Rest parameter that has been part of React for a long time when it could be used via transpilation, although, it has been made a part of JavaScript as part of the ES2015 syntax. The Spread syntax is used to deconstruct an array or object into separate variables where the exact
3 min read
How to calculate the date three months prior using JavaScript ?
To calculate the date three months prior using JavaScript, we could use the getMonth() and setMonth() methods. In this article, we are going to learn how to calculate the date three months prior using JavaScript. ApproachFirst, select the date object.Then use the getMonth() method to get the months.Then subtract three months from the getMonth() met
1 min read
How to find largest of three numbers using JavaScript ?
To find the largest of three numbers using JavaScript, we have multiple approaches. In this article, we are going to learn how to find the largest of three numbers using JavaScript. Below are the approaches to finding the largest of three numbers using JavaScript: Table of Content Using Conditional Statements (if-else)Using the Math.max() MethodUsi
3 min read
How to create three boxes in the same div using HTML and CSS ?
We can place three or more different divs side by side in the same div using CSS. You can achieve this using Flexbox, but you need to use wrapper divs and apply different flex directions to each of them to make the grid layout work. You can set the height and width of the div by using CSS properties. Syntax:flex: flex-grow flex-shrink flex-basis |
3 min read
How to Create a Three-Column Layout in Bootstrap?
Creating a three-column layout in Bootstrap involves dividing the container into three equal-width columns using the col-sm-4 class within a row and container. This ensures the columns are responsive and maintain equal width on small-sized devices and larger, providing a balanced layout for organizing content effectively. Syntax&lt;div class="conta
2 min read
JavaScript Program to Find the Smallest Among Three Numbers
In JavaScript, finding the smallest among three numbers is a common task in programming. There are multiple approaches to achieving this, each with its advantages and use cases. There are several approaches in JavaScript to find the smallest among three numbers which are as follows: Table of Content Using Conditional StatementsUsing Math.min() Func
2 min read
CSS Viewer Cube without Three.js using Vanilla JavaScript
Creating a CSS viewer cube without three.js using Vanilla JavaScript offers a lightweight alternative for rendering 3D cubes in web applications. This article explores various approaches to implementing a CSS viewer cube without relying on three.js, providing developers with flexibility and simplicity. These are the following approaches: Table of C
4 min read
Find Common Elements In Three Sorted Arrays using JavaScript
JavaScript can be used to find the common elements in given three sorted arrays. We are given three different sorted arrays, we have to return common elements of three arrays. Example:Input array1 = [1 , 2 , 3 , 4 ,5 ] array2 = [3 , 4, 5 , 6 ,7 ]array3 = [ 3 , 4 , 7 , 8 , 9] Output [3 , 4]Below are different approaches to finding common elements pr
4 min read
How to Display Three Bootstrap Cards in a Column ?
In Bootstrap, aligning the three cards in a column is important to create a vertical layout for displaying content on smaller screen devices or to make the web page responsive and compatible for all devices. Use the below approaches to display three Bootstrap cards in a column: Table of Content Using Grid ClassesUsing FlexBox ClassesUsing Grid Clas
3 min read
Sorting Array of Exactly Three Unique Repeating Elements in JavaScript
Given an array of Numbers that contain any frequency of exactly three unique elements. Our task is to sort this array using JavaScript. Examples: Input: arr[] = [3, 1, 3, 2, 1, 2, 1, 3, 2]Output: [1, 1, 1, 2, 2, 2,3,3,3]Input = arr[] =[1,2,3,1,2,3]Output: [1,1,2,2,3,3]Table of Content Counting Sort with Extra SpaceThree-Way PartitioningCounting Sor
2 min read
How to get the first three characters of a string using JavaScript ?
In this article, we are going to learn how to get the first three characters of a string using JavaScript. Example: Input: todayOutput: todInput: tomorrowOutput: tomThese are the approaches by which we can get the first three characters of a string using JavaScript: Table of Content Approach 1: Using Slice() methodApproach 2: Using string.substr()
3 min read
JavaScript Program to Find the Largest Three Elements in an Array
In this article, we are given an array of numbers, we need to find the largest three elements in an array in JavaScript. We will explore the easiest and most efficient code of each approach and also go through the output of the code. Approaches to find the largest three elements in an array in JavaScriptTable of Content Using JavaScript sort() Meth
5 min read
JavaScript Program to Find the Middle of Three
In this article, we are going to implement an algorithm through which we can find the number that lies in the middle. Basically, we have three numbers, and the desired number will lie in the middle of the other two numbers according to the number line. Examples: Input : a = 2, b = 3, c = 4Output : 3Input : a = 50, n = 75, c = 100Output : 75Table of
4 min read
CSS Viewer Cube in React without Three.js
A CSS viewer cube is a visually appealing and interactive way to display images or content on a webpage. It resembles a 3D cube that can be rotated along its axes to reveal different faces. While traditionally, libraries like Three.js have been used to achieve this effect. In this article, we'll explore how to create a CSS viewer cube in React with
3 min read
Three Important Contexts in Web Extension
In this article, we will see the 3 important contexts in web extensions. The Web Extension is an interface and a function for a browser. It is made using web technologies like HTML, CSS, Javascript, and web extension APIs. Web extension can be made with three important contexts, which are: Background ScriptsContent ScriptPopup ScriptsWe will unders
5 min read
What are the three principles that Redux follows ?
Redux was developed to help front-end developers write applications for consistent behavior. In addition to this, redux also helps tackle the React performance issues. As a result, Redux is constant when it comes to running in different environments--native, server, and client. Usually, Redux works with React to eliminate issues encountered with th
6 min read
How to Display Three Columns Per Row in CSS Flexbox?
CSS Flexbox is a one-dimensional layout model that allows items within a container to be arranged in rows or columns with dynamic sizing and alignment. It simplifies the process of creating complex layouts and responsive designs. In this article we will explore approach to arrange columns in a container so that there are three columns per row. Appr
2 min read
How to Set Three Columns in One Particular Row in CSS Grid?
Setting up a grid layout with specific columns in CSS can sometimes be a bit challenging, especially if we are new to CSS Grid. It is a powerful tool that helps us to create complex easily. In this article, we are going to learn about different approaches to setting three columns in one particular row in the CSS grid, with their syntax and code exa
4 min read
JavaScript Program to find Smallest Difference Triplet from Three Arrays
The article is about finding the smallest difference triplet from three arrays in JavaScript. Given three sorted arrays, we need to find three elements (one from each array) such that the difference between the maximum and minimum among the three is minimized. In other words, we are looking for a triplet (a, b, c) where a is from the first array, b
4 min read
Javascript Program For Finding A Triplet From Three Linked Lists With Sum Equal To A Given Number
Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12-&gt;6-&gt;29, 23-&gt;5-&gt;8, and 90-&gt;20-&gt;59, and the given number is 101, the output should be triple "6 5 90".In the following solutions, size of all th
4 min read
How to float three div side by side using CSS?
Floating three div elements side by side using CSS means aligning them horizontally in a row. This is done by applying float: left; to each div, which moves them to the left of the parent container, allowing them to sit next to each other. These are the following ways to solve this problem: Table of Content Using float propertyUsing display propert
3 min read
Introduction to React Native
If you want to build mobile apps for both Android and iOS. What should you learn? The individual native languages for each app i.e, Java for Android and Swift/Objective-C for iOS?, Actually NO. Native Android and iOS development are quite different and can be expensive – first, the language itself is quite different, and second, all the underlying
3 min read
Introduction to Object Oriented Programming in JavaScript
As JavaScript is widely used in Web Development, in this article we will explore some of the Object Oriented mechanisms supported by JavaScript to get the most out of it. Some of the common interview questions in JavaScript on OOPS include: How is Object-Oriented Programming implemented in JavaScript? How does it differ from other languages? Can yo
7 min read
PHP | MySQL Database Introduction
What is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. MySQL is developed, distributed, and supported by Oracle Corporation. The data in a MySQL database are stored in tables which consists of columns and rows.MySQL is a database system that runs on a server.MySQL
4 min read