2024-10-31Web Development, Video Production, Programming
Building a Typing Code Animation Component
By O. Wolfson
This article will guide you through building a typing code animation component in React/Next.js. This component types out code with syntax highlighting, providing an engaging way to display code snippets for tutorials, presentations, or educational content. Incidentally, this setup is perfect for video recording, as the component is sized at 720p to maintain consistent formatting and high resolution for tutorial videos or presentations.
Here below is a simple example of how to output text one character at a time. This can be run as a node script to see the effect.
javascript
const content = `Hello, App Router!
Hello World`;
let sentence = "";
consttypeTest = async () => {
for (let index = 0; index < content.length; index++) {
sentence = content.slice(0, index + 1);
awaitnewPromise((resolve) =>setTimeout(resolve, 50));
console.log(sentence);
}
};
typeTest();
Overview of the Component
The component takes an array of code blocks and animates the typing of each block one character at a time. It includes an elapsed time display to track how long the animation has been running and provides an estimated total time for completion.
Core Features
Typing Animation: Each code block is revealed character by character with a specified delay.
Elapsed Time HUD: Displays the elapsed time and the estimated total time for the animation.
Syntax Highlighting: Uses the react-syntax-highlighter library with a customizable theme for highlighting code.
Code Walkthrough
Let's dive into the code and explain how each function and component works:
This function handles the animation of typing out the code block one character at a time. It iterates through the content string and calls the onUpdate callback with the updated string at each step, adding a delay between characters using setTimeout.
2. calculateTotalAnimationTime Function
Calculates the estimated total time for the entire animation by summing up:
The number of characters across all code blocks multiplied by the delay per character.
The initial delay (time) for each code block before it starts typing.
This is displayed in the HUD to give users an idea of how long the animation will take.
3. Timer Management Functions
startTimer: Starts the timer and updates elapsedTime every second.
stopTimer: Stops the timer when the animation is complete.
4. startAnimation Function
The main function that controls the animation flow:
Resets and starts the timer.
Iterates through each code block and starts typing after a specified initial time.
Updates displayedCode to reflect the currently typed characters.
5. Component Structure
The component displays a button to start the animation.
Once started, it shows the typing animation of the code blocks using react-syntax-highlighter with the darcula theme for syntax highlighting.
A HUD at the top displays the elapsed time and the total estimated time for the animation.
Styling
Most of the styling is handled using Tailwind CSS classes for consistency and maintainability. Custom styles are added using the :global rule to remove the background of the syntax highlighter.
Sound Effects
To add a bit of realism, I added a sound effect that plays when the animation starts. This is done using the useEffect hook to initialize the audio object and the play method to start the sound.