3D animation background using Vanta.js
3D animation background using Vanta.js
It looks like you're trying to integrate a Vanta.js animation effect (specifically, the birds
effect) into your web page using Three.js and Vanta.js libraries. The code you've shared is mostly correct but needs a few improvements for proper setup. Below is the fixed and improved version of your code, including a better structure and explanation:
Complete Code for Vanta.js Bird Animation
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vanta.js Bird Animation</title>
<link rel="stylesheet" href="style.css">
<style>
/* Make sure the body and main container cover the full page */
body, html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
/* Set a container element to hold the animation */
#main {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<!-- Main container where the Vanta.js animation will render -->
<div id="main"></div>
<!-- Include Three.js for Vanta.js dependency -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r121/three.min.js"></script>
<!-- Include Vanta.js for the birds effect -->
<script src="https://cdn.jsdelivr.net/npm/vanta@latest/dist/vanta.birds.min.js"></script>
<script>
// Initialize Vanta Birds effect on the 'main' element
VANTA.BIRDS({
el: "#main", // The element where the animation will render
mouseControls: true, // Enable mouse controls
touchControls: true, // Enable touch controls for mobile
gyroControls: false, // Disable gyro controls for mobile
minHeight: 200.00, // Minimum height of the element
minWidth: 200.00, // Minimum width of the element
scale: 1.00, // Scaling factor for the effect
scaleMobile: 1.00 // Scaling factor for mobile devices
});
</script>
</body>
</html>
Explanation of Key Changes:
-
CSS for Full-Screen Effect:
- The
body, html
styles ensure that the page covers the full viewport with no margins and no overflow. - The
#main
div is set to take up the full width and height of the viewport.
- The
-
Vanta.js and Three.js Setup:
- Three.js is a dependency for Vanta.js. It's included first in the
<script>
tag. - Then, Vanta.js is loaded from the CDN (
vanta.birds.min.js
) to activate the bird animation effect.
- Three.js is a dependency for Vanta.js. It's included first in the
-
Vanta.BIRDS() Initialization:
- The
VANTA.BIRDS()
function initializes the bird animation effect on the element with the id#main
. - Several customizable options are passed, such as:
mouseControls
: Enables mouse interaction with the animation.touchControls
: Enables touch interaction for mobile devices.gyroControls
: Disables gyroscopic control (you can enable it for a 3D tilt effect on mobile devices).minHeight
andminWidth
: These properties ensure that the animation area is large enough to work correctly.scale
andscaleMobile
: Control the scaling of the animation on desktop and mobile.
- The
-
Responsiveness:
- The CSS ensures that the animation will fill the screen no matter the device's size.
Additional Notes:
- If you want to use other Vanta.js effects (e.g., waves, clouds, or others), you can replace the
vanta.birds.min.js
script with another Vanta effect script. Check Vanta.js documentation for more effect options. - You can adjust the
minHeight
,minWidth
,scale
, andscaleMobile
properties to fine-tune the responsiveness and appearance of the animation.
Now, when you open this page, you'll see the Vanta.js Birds animation effect running in the background, with full-screen coverage and responsive behavior for mobile devices.
Comments
Post a Comment