Wave-Pattern Matrix Rain Animation With HTML, CSS, and JavaScript
by nasaleader in Circuits > Computers
394 Views, 5 Favorites, 0 Comments
Wave-Pattern Matrix Rain Animation With HTML, CSS, and JavaScript
Introduction
In this project, we will create a visually stunning Wave-Pattern Matrix Rain Animation using HTML, CSS, and JavaScript. Inspired by the classic Matrix code rain effect, this project will enhance the original effect with wave-like motion and multi-colored falling characters. This animation will run on a full-screen HTML canvas and provide a mesmerizing wave effect where each letter dances down the screen with varying colors and undulating motion.
The main goal is to combine mathematical wave functions with a classic text animation, producing a fluid, visually engaging experience. Whether you're looking to add some flair to your website or simply exploring the fun of creative coding, this project offers a perfect blend of design and code!
Supplies
Supplies
To build this project, you will need the following:
- Text Editor:
- We will be coding in HTML, CSS, and JavaScript, so you need a code editor like Visual Studio Code, Sublime Text, or Atom. Visual Studio Code is a popular choice due to its powerful features and user-friendly interface.
- HTML File (index.html):
- This will define the structure of the page, where we’ll set up the <canvas> element.
- CSS File (styles.css):
- Used for minimal styling and ensuring the canvas fits the screen and removes default browser padding/margin.
- JavaScript File (main.js):
- This file contains the core logic for the Matrix wave animation, handling the characters’ motion, the wave effect, and the random color generation.
- A Modern Browser:
- Any browser that supports HTML5 canvas and JavaScript (e.g., Chrome, Firefox, or Edge) will work to display the animation.
With these supplies ready, you're all set to create a dynamic, wave-based Matrix rain effect.
This project makes the letters look like they are "surfing" down the screen in a wave pattern. The key differences are:
- Wave Motion: Letters fall in a sine wave pattern, making the fall more dynamic.
- Multiple Colors: Letters change colors as they fall to give a gradient or rainbow effect.
1. HTML Code (Main.html)
2. CSS Code (Command.css)
3. JavaScript Code (Main.js)
Explanation of Key Changes:
- Wave Motion: The sine function creates a wave-like effect for each column. The wave's height (waveAmplitude) and frequency (waveFrequency) control the appearance.
- Color Variation: Each letter is randomly assigned a color from a palette. You can modify the colors in the colors array to change the effect.
- Wave Offset: As the waveOffset increases, the wave moves across the screen, making it look dynamic as letters fall in sync with the wave.
Preview:
This effect will make the letters fall down in a wave-like motion, with the letters changing color as they move. You can experiment with wave parameters to control how dramatic the wave is, and tweak the color scheme to your preference.
Step 1: Create the HTML File
- Open Visual Studio Code.
- Create a new file and name it main.html.
- Add the following HTML code:
- This file sets up the structure of the webpage and links the CSS and JavaScript files.
- It also defines the <canvas> element where the animation will be drawn.
Step 2: Create the CSS File
- Create another file and name it command.css.
- Add this CSS code to style the canvas:
- This CSS ensures the canvas takes up the full screen with no margins or padding. The background is black to match the classic Matrix aesthetic.
Step 3: Create the JavaScript File
- Create a third file and name it main.js.
- Add the following JavaScript code:
- This code handles the animation of the letters, making them fall in a wave-like pattern while changing colors.
- It defines the letters, the wave motion, the color transitions, and the loop that keeps the animation running.
Step 4: Test the Code
- Save all three files (main.html, command.css, and main.js).
- Open main.html in a browser. You should now see the letters falling in a wave pattern across the screen, with random colors!
Explanation of each line
Canvas and Context Setup
- const canvas = document.getElementById("matrixCanvas");: This selects the <canvas> element from the HTML using its ID (matrixCanvas). This is where all the drawing and animation will occur.
- const ctx = canvas.getContext("2d");: We create a 2D context for drawing on the canvas. The ctx variable allows us to use 2D drawing functions like drawing shapes, text, etc.
Set Canvas Size
- canvas.width = window.innerWidth;: This sets the width of the canvas to match the width of the browser window (making it responsive).
- canvas.height = window.innerHeight;: This sets the height of the canvas to match the height of the browser window.
Define Letters
- This defines a string of all the characters that will be used in the falling animation.
- .split(""): This splits the string into an array of individual characters. Each character is now accessible by its index, making it easy to select random letters for the animation.
Set Font Size and Calculate Columns
- const fontSize = 16;: This sets the size of the falling characters to 16 pixels.
- const columns = canvas.width / fontSize;: We calculate how many vertical columns can fit across the canvas. It’s calculated by dividing the canvas width by the font size. Each column will be 16 pixels wide, and characters will fall down each of these columns.
Initialize Drops Array
- Array.from({ length: columns }, () => 1);: We create an array called drops with a length equal to the number of columns.
- Each element in this array represents the current vertical position of a letter in a particular column.
- Initially, all letters start at position 1 (top of the screen).
Color Palette
- This defines an array of color codes. Each falling letter will randomly pick one of these colors to display.
- "#0f0" is green (the classic Matrix color).
- You can modify these hex color codes to customize the color scheme.
Wave Parameters
- waveAmplitude = 50;: This controls the height of the wave motion. A larger value will create taller waves.
- waveFrequency = 0.05;: This determines the spacing between wave peaks. A smaller value creates more stretched-out waves, while a larger value creates tighter waves.
- waveOffset = 0;: This will be incremented in the animation loop to move the wave across the screen over time.
Draw Function
- This defines the draw() function, which contains all the logic to render each frame of the animation. This function will be called continuously to keep the animation running.
Clear the Canvas with a Semi-Transparent Fill
- ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';: This sets the fill color to a semi-transparent black. The 0.1 opacity allows the previous frame to partially show through, creating the motion trail effect.
- ctx.fillRect(0, 0, canvas.width, canvas.height);: This fills the entire canvas with the semi-transparent black color, “erasing” the previous frame but leaving a faint trail.
Loop Through Each Column
- This loop goes through each column, where i represents the column index. Each column will display one falling letter.
Calculate Wave Offset for Each Column
- Math.sin(i * waveFrequency + waveOffset): This generates a sine wave for the column. The waveFrequency controls how tightly packed the waves are, and waveOffset shifts the wave horizontally as the animation progresses.
- * waveAmplitude: This scales the height of the wave. The wave height is multiplied by waveAmplitude to control how high or low the letters move in a wave pattern.
Pick a Random Letter and Color
- letters[Math.floor(Math.random() * letters.length)]: This picks a random letter from the letters array by generating a random index.
- colors[Math.floor(Math.random() * colors.length)]: This selects a random color from the colors array to apply to the letter.
Draw the Letter on the Canvas
- ctx.fillStyle = color;: Sets the color of the letter to the randomly chosen color.
- ctx.font = ${fontSize}px monospace;: Sets the font size and style. The monospace font ensures each letter takes up the same amount of space.
- ctx.fillText(letter, i * fontSize, drops[i] * fontSize + wave);: This draws the letter on the canvas at the correct position:
- i * fontSize: The horizontal position is determined by the column number (i), each column being fontSize pixels wide.
- drops[i] * fontSize + wave: The vertical position is determined by the current drop’s vertical position (drops[i]) plus the wave offset.
Move Drops Down the Screen
- drops[i]++: This moves the letter down by increasing its vertical position (drops[i]).
- if (drops[i] * fontSize > canvas.height || Math.random() > 0.975): This checks if the letter has reached the bottom of the screen. If so, or if a random chance occurs (Math.random() > 0.975), the letter will be reset to the top of the screen.
- drops[i] = 0;: Resets the letter's vertical position back to the top of the canvas, creating a continuous falling effect.
Move the Wave
- waveOffset += 0.1;: This increases the waveOffset slightly on each frame, which makes the wave move horizontally over time.
Keep Drawing the Animation
- requestAnimationFrame(draw);: This is the function that continuously calls the draw function, creating the smooth animation. It tells the browser to call draw at the next available screen repaint.
Start the Animation
- This calls the draw() function for the first time, starting the loop that will continuously update the canvas and animate the falling letters.