In this tutorial, we will learn how to create an impressive neon cursor effect using Three.js, HTML, CSS, and JavaScript. This effect adds a modern and interactive touch to your website. We will break down each part of the code so you can easily understand and customize this effect for your own projects.
The neon cursor effect is an elegant visual addition that can enhance the user experience on your website. It follows the movement of the mouse, creating a dynamic glowing trail that captures attention. This type of effect is particularly suitable for portfolio websites, landing pages, or any site aiming to provide a unique and engaging user interaction. By using modern visual effects like a neon cursor, you can differentiate your website and make navigation more immersive.
To create this neon cursor effect, we will need several technologies and tools:
This combination of tools will allow you to implement a high-performance and visually appealing neon cursor.
To begin, we will establish the basic HTML structure that will serve as the foundation for our neon cursor effect. Here is the basic HTML code:
Neon Cursor
Digital marketing agency
MonDesign Web
to define the HTML version. Then, the document language is set to English using lang="en".meta charset="UTF-8" ensures that the document uses UTF-8 character encoding.meta http-equiv="X-UA-Compatible" ensures compatibility with Internet Explorer.meta name="viewport" rend la page responsive.styles.css définit les styles de la page.div avec l’id app est le conteneur principal pour notre contenu.hero est l’endroit où nous plaçons le texte principal avec une mise en page centrée.index.js est chargé à la fin pour assurer que le DOM est entièrement chargé avant que le JavaScript ne soit exécuté.Next, we define the base styles for our effect in the styles.css file. Here is an example of CSS code:
:root {
--surface-900: #131320;
--primary-100: #B89CFC;
}
body, html, #app {
margin: 0;
width: 100%;
height: 100%;
}
#app {
overflow: hidden;
touch-action: pan-up;
color: #ffffff;
font-family: "Montserrat", sans-serif;
text-align: center;
}
#app canvas {
display: block;
position: fixed;
z-index: -1;
top: 0;
left: 0;
}
.hero {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
color: #FFFFFF;
}
.main-title {
font-size: 24px;
font-weight: 400;
color: #FFFFFF;
}
.main-title strong {
color: var(--primary-100);
}
@media screen and (min-width: 640px) {
.main-title {
font-size: 34px;
}
}
@media screen and (min-width: 1024px) {
.main-title {
font-size: 42px;
}
}
@media screen and (min-width: 1536px) {
.main-title {
font-size: 60px;
}
}
Three.js is a JavaScript library that simplifies the creation of complex 3D graphics in the browser. It is essential for producing sophisticated visual effects, such as the neon cursor, by leveraging WebGL, an API used for 3D rendering.
Three.js is widely adopted for its ease of use compared to other more complex 3D libraries. It offers great flexibility and a wide range of features for creating animations and interactive graphics. For our neon cursor, Three.js allows us to easily manipulate shaders and 3D objects needed to produce realistic glowing effects.
Le JavaScript est la clé pour animer et gérer le comportement du curseur néon. Nous allons utiliser Three.js pour initier et contrôler cet effet.
The following script should be placed in the index.js file:
import { neonCursor } from 'https://unpkg.com/threejs-toys@0.0.8/build/threejs-toys.module.cdn.min.js';
const root = document.getElementById("app");
const options = {
el: root,
shaderPoints: 20,
curvePoints: 88,
curveLerp: 0.8,
radius1: 5,
radius2: 25,
velocityTreshold: 10,
sleepRadiusY: 200,
sleepRadiusX: 200,
sleepTimeCoefX: 0.0025,
sleepTimeCoefY: 0.0025
};
neonCursor(options);
const options = {
el: root,
shaderPoints: 20,
curvePoints: 88,
curveLerp: 0.8,
radius1: 5,
radius2: 25,
velocityTreshold: 10,
sleepRadiusY: 200,
sleepRadiusX: 200,
sleepTimeCoefX: 0.0025,
sleepTimeCoefY: 0.0025
};
el: root: Specifies the HTML element where the neon effect will be applied (the element with the id “app”).
shaderPoints: 20: Defines the number of glowing points of the cursor.
curvePoints: 88 : Définit le nombre de points qui forment la courbe suivie par le curseur.
curveLerp: 0.8: Controls the smoothness of the curve transitions for a smoother movement.
radius1: 5: Determines the radius of the small sphere that represents the start of the cursor.
radius2: 25: Determines the radius of the large sphere that forms the cursor trail.
velocityTreshold: 10: Sets the minimum speed at which the cursor begins to move.
sleepRadiusY: 200 and sleepRadiusX: 200: Specify the cursor sensitivity when the mouse is idle.
sleepTimeCoefX: 0.0025 and sleepTimeCoefY: 0.0025: Control the cursor’s reaction time when the mouse stops moving.
neonCursor(options): Applies the neon effect using the options defined above.
Even with well-structured code, adjustments may be necessary to optimize performance or resolve specific issues. Here are a few tips:
It is crucial to test your neon cursor effect on various browsers and devices to ensure a consistent and high-performance user experience.
Once you have mastered the basic effect, you can go further by adding advanced features to enhance your website’s interactivity:
This tutorial has guided you step by step to create an impressive neon cursor effect using Three.js, HTML, CSS, and JavaScript. This effect adds a modern and dynamic touch to your website, making navigation more engaging. Feel free to customize the parameters and experiment with different configurations to adapt this effect to your specific needs.
MonDesign Web, your digital marketing agency, dedicated to guiding you to the pinnacle of your online success.
We use cookies to improve your experience on our site. By using our site, you consent to cookies.
Websites store cookies to enhance functionality and personalise your experience. You can manage your preferences, but blocking some cookies may impact site performance and services.
Essential cookies enable basic functions and are necessary for the proper function of the website.
These cookies are needed for adding comments on this website.
Statistics cookies collect information anonymously. This information helps us understand how visitors use our website.
Google Analytics is a powerful tool that tracks and analyzes website traffic for informed marketing decisions.
Service URL: policies.google.com
Marketing cookies are used to follow visitors to websites. The intention is to show ads that are relevant and engaging to the individual user.
Facebook Pixel is a web analytics service that tracks and reports website traffic.
Service URL: www.facebook.com