<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Nate Argaw</title>
    <description>The latest articles on Forem by Nate Argaw (@nargaw).</description>
    <link>https://forem.com/nargaw</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F591775%2Fc3bd9298-db65-4902-9c3f-4e271f452586.jpg</url>
      <title>Forem: Nate Argaw</title>
      <link>https://forem.com/nargaw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nargaw"/>
    <language>en</language>
    <item>
      <title>The Power of 3D on the Web and Introduction to Three.js</title>
      <dc:creator>Nate Argaw</dc:creator>
      <pubDate>Fri, 25 Aug 2023 09:59:17 +0000</pubDate>
      <link>https://forem.com/nargaw/the-power-of-3d-on-the-web-and-introduction-to-threejs-196j</link>
      <guid>https://forem.com/nargaw/the-power-of-3d-on-the-web-and-introduction-to-threejs-196j</guid>
      <description>&lt;p&gt;This blog post follows the talk I gave at the Carolina Code Conference, 2023 in Greenville, SC. &lt;/p&gt;

&lt;p&gt;Before I discovered the world of 3D web development, I wanted to do creative projects with code. But I didn’t know where to start, which path to take or which tool to use. Until one day I came across a portfolio website by &lt;a href="https://bruno-simon.com/" rel="noopener noreferrer"&gt;Bruno Simon&lt;/a&gt;. In this website you explore the all the contents of the page by driving around a car. It runs great with smooth animations. It uses physics for collisions. You could also appreciate the developer's unique sense of style and personality. I thought it was effective, powerful and fun. I have been learn about it ever since.&lt;/p&gt;

&lt;p&gt;The web is an awesome place and 3D gives it a new dimension. Why should you try 3D web development? You should try it for three reasons. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An effective form of communication. &lt;/li&gt;
&lt;li&gt;It is powerful and easy to use &lt;/li&gt;
&lt;li&gt;It is creative and fun. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is said that a picture is worth a thousand words. Well, I’d say that a 3D interactive application is worth a thousand pictures. 2D gives you a lot of information but that information is static and does not change. But 3D is dynamic with data that changes based on the way you interact with it. A 3D interactive application allows you to convey much more information than you do with images or videos. This engaging and dynamic information allows you to communicate your complex ideas clearly and effectively. &lt;/p&gt;

&lt;p&gt;This is all possible because of Three.js. Three.js is a 3D JavaScript Library that makes it incredibly easy to render 3D content on a webpage. It was originally created by Ricardo Cabello (Mr. Doob) in 2010. Today it has thousands of contributors and over a million weekly downloads on NPM. Three.js under the hood uses WebGL (also known as Web Graphics Library) to draw 3D content in the browser. WebGL is a web standard developed by the Khronos group and it gives developers direct control of the graphics pipeline. It supports Chrome, Firefox, Safari and others. WebGL runs in the canvas element.&lt;/p&gt;

&lt;p&gt;Why not just use WebGL directly? Well it is possible but it is too difficult. WebGL itself uses the OpenGL Shading Language that is similar to the C programming language. It would be interesting to look into. But for most web developers a JavaScript library is preferable. That is what Three.js does. It allows developers to use a 3D JavaScript library to render 3D content on the web without having to understand shading language or the complex math it requires.&lt;/p&gt;

&lt;p&gt;How does Three.js work? The easiest way to understand it is to imagine you are a movie director. You would need four basic things to make your movie. First is your stage - where your movie is going to be shot including the environment and lighting etc. Second is your Camera - you use this to capture the scene. Third is your screen - this is what will play your recorded scene. Fourth is your actor - this is what you show in your scene using your camera.  &lt;/p&gt;

&lt;p&gt;Go to &lt;a href="https://codesandbox.io/dashboard/" rel="noopener noreferrer"&gt;codesandbox.io&lt;/a&gt; to follow along with this part. Select new sandbox and then Vanilla. Type 'three' under Dependencies. Then you are ready to add this code in the JavaScript file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqcg1eiytizj9xbo5kbpu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqcg1eiytizj9xbo5kbpu.png" alt="Three.js Hello Cube code block"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code block shows a 'Hello Cube'. The scene will include everything that is going to be rendered similar to the stage of a movie director. The camera is a perspective camera. Then we add the renderer and this is the screen that shows our rendered content. We also have our actor which is the mesh. The mesh takes in a geometry and material. We adjust the position of the mesh and add it to our scene. Lastly, we have to animate it and we do that using a requestAnimationFrame function. We render the scene in every frame (approx. 60 frames per second) and we also rotate the mesh slightly in each frame. &lt;/p&gt;

&lt;p&gt;This is where it gets creative and fun. These four aspects (scene, camera, renderer and mesh) can be modified to create anything from your imagination. For example you can manipulate the scene by adding different types of lighting, environment maps, shadows and fog. There are multiple types of cameras to choose from. The camera can be modified to move based on mouse scroll wheel value or can be used to create third person view chase camera similar to car racing games or first person shooter experiences. You can manipulate the renderer by adding a multitude of post processing effects such as a glitch effect, screen space reflections, screen space ambient occlusion, bloom and so much more. The mesh itself can be changed into any model designed using a 3D modeling software such as Blender3D. There are various loaders that can be used to import the models along with geometry, materials, textures and animations.&lt;/p&gt;

&lt;p&gt;Getting started is very easy. One great place to start is the three.js documentation page at &lt;a href="//threejs.org"&gt;threejs.org&lt;/a&gt;. The threejs.org website features amazing examples of what is possible with 3D on the web. There you will also find examples with code. I should also mention the absolute best tutorial on Three.js by Bruno Simon called &lt;a href="https://threejs-journey.com/" rel="noopener noreferrer"&gt;Three.js Journey&lt;/a&gt;. This tutorial is beginner friendly and goes into all the things you want to learn about Three.js.&lt;/p&gt;

&lt;p&gt;Furthermore, I have created a meetup group in Greenville, SC called &lt;a href="https://www.meetup.com/greenville-webgl-meetup-group/" rel="noopener noreferrer"&gt;Greenville Three.js Meetup Group&lt;/a&gt;. It is for anyone interested in 3D web development to meet in-person to exchange ideas, learn from each other and network. Having 3D in your tool bag allows you to express your message in a new dimension. 3D on the web is effective, powerful and easy to use and most importantly, it is fun!  I encourage you to try three.js and let your imagination run wild. Please feel free to reach out to me directly with any questions. Thank you for reading.&lt;/p&gt;

&lt;p&gt;Nate Argaw&lt;/p&gt;

</description>
      <category>threejs</category>
      <category>webgl</category>
      <category>webdev</category>
      <category>3d</category>
    </item>
    <item>
      <title>Three.js Hello Cube - A Beginner's Guide</title>
      <dc:creator>Nate Argaw</dc:creator>
      <pubDate>Sun, 08 Aug 2021 03:33:25 +0000</pubDate>
      <link>https://forem.com/nargaw/three-js-hello-cube-a-beginner-s-guide-2a2k</link>
      <guid>https://forem.com/nargaw/three-js-hello-cube-a-beginner-s-guide-2a2k</guid>
      <description>&lt;p&gt;Imagine you are Hollywood's best director and you are ready to shoot the greatest scene. You are standing in front of an empty set. What do you think you'll need?&lt;/p&gt;

&lt;p&gt;A movie scene is very similar to a Three.js scene. We will create a Hello Cube and figure out the basics of Three.js. &lt;/p&gt;

&lt;p&gt;While we can have an extensive list of things to add to our movie set, three things will be impossible to do without. The first is our movie star. Second, we will need a camera to capture our scene. Lastly, we will need a screen to view what we just captured.&lt;/p&gt;

&lt;p&gt;Briefly, Three.js is a JavaScript library and Application Programming Interface (API) first released by Ricardo Cabello in April 2010. Since release, Three.js has grown to a robust library with thousands of contributors. Three.js uses WebGL to enable easy creation of 3D content. WebGL is a low-level cross-platform API that creates simple shapes such as points, lines and triangles. Because WebGL is low-level, we would need a lot of code to even draw a single triangle. Three.js makes this task much easier.&lt;/p&gt;

&lt;p&gt;In Three.js, our object will be represented by a something called a Mesh. A "mesh" is a 3D form created using vertices. Each vertex is represented by a X, Y and Z dimension coordinate planes. X is the horizontal plane or the left to right plane. Y is the vertical plane or the top to bottom plane. Z is the depth plane or the front to back plane. &lt;/p&gt;

&lt;p&gt;If we have three vertices, we can draw a straight line connecting one vertex to another to form a triangle. This is the simplest shape we can create in WebGL. If we wanted to create a flat rectangle we would need at least two triangles (at least 4 vertices). By adding multiple vertices we can create very complex shapes even the smoothest of spheres.&lt;/p&gt;

&lt;p&gt;That's enough talk. Let's write some code. We can separate the files but to make this even more simple, we will use a single HTML file. Our goal is to make a 3D cube.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Your First Three.js Scene&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nc"&gt;.display&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;outline&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;overflow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"module"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://cdn.skypack.dev/pin/three@v0.131.3-QQa34rwf1xM5cawaQLl8/mode=imports/optimized/three.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;canvas&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"display"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/canvas&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We created a &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; element. Going back to our analogy, the &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; element could be thought of as our screen to display our captured scene. Everything from our scene will be displayed or rendered inside this &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; element. We then set our basic CSS to hide overflow, remove any margin and give an absolute position to our canvas. We then imported the Three.js module into our &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We will write the remaining code inside the &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;Now we are ready to instantiate our &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; inside our script. We can do that using &lt;code&gt;document.querySelector()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//instantiate canvas&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;canvas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.display&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We used the &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; element class to link it to our scene. Just like that, we are ready to create our scene. In our scene, we will add a cube mesh. Continuing with our analogy, the cube is the star of our scene. Like all great movie scenes, we need to be specific about our cast. We have to provide two requirements to make our cube mesh. The first is a geometry and the second is a type of material. &lt;/p&gt;

&lt;p&gt;Three.js makes it very easy for us. Because, shapes such as: box, sphere, cone, circle, plane and many more geometries are already provided in the library. Different geometries require different inputs. A box geometry requires a width, height and depth float values.&lt;/p&gt;

&lt;p&gt;We can also choose from several materials but for now we will go with a Mesh Basic Material. We have an option of adding a color to our material so we will make it Cyan. After we select our parameters we will add the cube to our scene.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//create a scene&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scene&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Scene&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;//create a cube&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cube&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Mesh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;//box geometry with a width, height and depth&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;BoxGeometry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;

    &lt;span class="c1"&gt;//apply a mesh basic material to our mesh&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MeshBasicMaterial &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;0x00ffff&lt;/span&gt; 
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//add our mesh to the scene&lt;/span&gt;
&lt;span class="nx"&gt;scene&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cube&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have completed one of the three necessary requirements to make our scene. We still need to make our camera and our renderer. Our camera will capture our scene and the renderer will display our scene on our canvas. &lt;/p&gt;

&lt;p&gt;Three.js gives us several options for a camera but for now we will go with a Perspective Camera. A perspective camera displays objects closer to the camera as larger and objects further away as smaller. This could simply be represented by a shape called a frustum. A frustum is a cone or pyramidal shaped object with the pointy-end cutoff. The perspective camera displays objects in a way that a cube would look like a frustum. The perspective camera needs four inputs. These are: field of view (FOV), aspect ratio of the display, near value and far value. The FOV is the angle measure from bottom to top given as a float value. The aspect ratio is the canvas width divided by the canvas height. The near and far values are the depth field we want the camera to capture.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//create camera&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;camera&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PerspectiveCamera&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHeight&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;scene&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;position&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the camera position like any object is represented by an x, y and z values with in the scene. By default the object and the camera are both placed at 0, 0, 0. To capture the object we need the camera to be in front of the object. If our monitor had a box shape, the positive z value points towards you and the negative z value points towards the back of the monitor. &lt;/p&gt;

&lt;p&gt;One last thing we need to do is to add the renderer. Three.js provides us with several renderer options but for now we will go with WebGLRenderer. We will display our captured scene using the renderer in our canvas. The renderer has a &lt;code&gt;.render&lt;/code&gt; method that requires the scene we want to render and the camera we used to capture this scene.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//create renderer&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;renderer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;THREE&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WebGLRenderer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;renderer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerWidth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHeight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;renderer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scene&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at that! You created your first Hello Cube!&lt;/p&gt;

&lt;p&gt;From this view, our cube appears to be just a square. But we are going to work some camera magic and make it rotate. We will do this using Orbit Controls. Orbit control rotates the camera around a given target. In this case, the center of the scene, where our cube is placed. First we will load orbit controls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;OrbitControls&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://cdn.skypack.dev/three@v0.131.3-QQa34rwf1xM5cawaQLl8/examples/jsm/controls/OrbitControls.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then instantiate it. Orbit control requires the camera and the canvas it will animate on. We will enable a damping of the camera movement that will prevent jerking camera movement. we will also make it auto rotate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//instantiate OrbitControls after camera and canvas&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;controls&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OrbitControls&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//smooth rotation of camera&lt;/span&gt;
&lt;span class="nx"&gt;controls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;enableDamping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;//auto rotation of camera&lt;/span&gt;
&lt;span class="nx"&gt;controls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;autoRotate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to constantly update the scene we need to create a function that will call &lt;code&gt;window.requestAnimationFrame()&lt;/code&gt; that calls the function itself continuously. The number of callbacks is usually 60 times per second.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//create a call back function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updater&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//call the same function again&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requestAnimationFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;updater&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;//update the orbit controls with every function call&lt;/span&gt;
    &lt;span class="nx"&gt;controls&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;//render the scene again with every function call&lt;/span&gt;
    &lt;span class="nx"&gt;renderer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scene&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;camera&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//call the function&lt;/span&gt;
&lt;span class="nf"&gt;updater&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can move the camera with in our canvas using our mouse. &lt;/p&gt;

&lt;p&gt;From here, the possibilities are endless. Three.js gives developers the ability to utilize WebGL to create responsive, interactive and engaging web content. There are many resources available to read and learn more about Three.js.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://threejs.org/"&gt;https://threejs.org/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://threejs-journey.xyz/"&gt;https://threejs-journey.xyz/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://threejsfundamentals.org/"&gt;https://threejsfundamentals.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for your time. I hope this post was helpful to you. &lt;/p&gt;

&lt;p&gt;You can find me on at &lt;a href="https://twitter.com/nate_dev_"&gt;https://twitter.com/nate_dev_&lt;/a&gt; &lt;/p&gt;

</description>
      <category>threejs</category>
      <category>webgl</category>
      <category>3d</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
