Download CV

Archive: Javascript

Latest Posts
12 August 2022
React - The Concept Of Closures

In React, closures are an important concept that is used to avoid common bugs and improve the performance of components. Closures are a fundamental feature of the JavaScript language, and they are used in many other programming languages as well.

A closure is a function that has access to the variables and scope of its outer function, even after the outer function has returned. This allows the inner function to retain its state and continue to operate on the data that it was given by the outer function.

In React, closures are used in several ways to avoid common bugs and improve the performance of components. One of the main uses of closures in React is to prevent the loss of state when updating a component.

When a component is updated in React, the framework re-renders the component and its children to reflect the changes in the data. However, if the component uses external variables that are not part of its state, these variables may be lost during the update process.

To prevent this, React uses closures to ensure that the variables are preserved and accessible to the component during the update. This allows the component to retain its state and continue to operate on the data that was passed to it.

Another use of closures in React is to improve the performance of components by avoiding unnecessary re-renders. In React, components are only re-rendered when their state or props change. However, if a component uses external variables that are not part of its state or props, these variables may cause the component to be re-rendered even if they are not actually used by the component.

To avoid this, React uses closures to ensure that the external variables are only accessed when they are actually needed by the component. This allows React to avoid re-rendering the component unnecessarily, which improves its performance and avoids potential bugs.

In summary, closures are an important concept in React that is used to avoid common bugs and improve the performance of components. Closures are a fundamental feature of JavaScript, and they are used in many other programming languages as well.

25 May 2022
ThreeJS - Working In 3D On The Web

Table of Contents:

  • What is three.js?
  • Features of three.js
  • Getting started with three.js
  • Examples of projects using three.js
  • Community and resources for three.js
  • Conclusion

What is three.js?

Three.js is a popular open-source JavaScript library for creating 3D graphics and interactive experiences in web browsers. It was created by Ricardo Cabello (aka Mr. doob) and is maintained by a large community of contributors. three.js uses WebGL, the standard API for 3D graphics on the web, and provides a set of building blocks that make it easy to create complex 3D scenes with just a few lines of code.

Features of three.js

Three.js has a wide range of features that make it a powerful tool for creating 3D graphics and interactive experiences. Some of the key features of three.js include:

  • A simple and intuitive API that makes it easy to create 3D scenes with just a few lines of code.
  • A large library of built-in objects and materials, including geometry, lights, cameras, and textures.
  • Support for various 3D file formats, including OBJ, STL, and GLTF, which allows users to import 3D models from other applications.
  • A rich set of animation and interaction tools, such as tweening, keyframes, and physics simulations.
  • Support for VR and AR devices, which allows users to create immersive experiences that can be viewed with a VR headset or on a smartphone.

Getting started with three.js

Getting started with three.js is easy, even for users with no prior experience in 3D graphics or JavaScript. Here are the steps to create your first 3D scene with three.js:

Step 1. Download the three.js library from the official website and include it in your HTML file:

<script src="https://cdn.jsdelivr.net/npm/three@0.117.1/build/three.min.js"></script>

Step 2. Create a scene, a camera, and a renderer:

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();

Step 3. Add a cube to the scene:

const geometry = new THREE.BoxGeometry(1, 1, 1);\n
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });\n
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);

Step 4. Set the camera position and render the scene:

camera.position.z = 5;
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
renderer.render(scene, camera);

Examples of projects using three.js

Three.js is used in a wide range of projects, from simple 3D graphics to complex interactive experiences. Some examples of projects using three.js include:

The 3D music visualizer on the official three.js website, which allows users to explore a 3D world generated from the music they play. The WebVR Experiments website, which showcases a collection of VR walkthoughs.