Issue
I am creating my portfolio page which I am currently animating the buttons' animation and my background is a simple particle animation as my background which powered by javascript. I noticed that the canvas which is the simple particle animation in the css file which I have set my canvas position to other elements except for absolute which is preventing the buttons animation from animating.
I tried changing the position to other elements and it's works for the buttons animation but when I changed the canvas position to absolute , the buttons' animation stopped working again
Here's my code ( html code )
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome to my Page!</title>
<link rel="stylesheet" href="styles.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Abril+Fatface&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Righteous&display=swap" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="header">
<h1>Jhan's Page</h1>
<div class="buttons">
<a href="#" class="button"><span>Button1</span></a>
<a href="#" class="button"><span>Button2</span></a>
<a href="#" class="button"><span>Button3</span></a>
<a href="#" class="button"><span>Button4</span></a>
</div>
</div>
<canvas id="waveCanvas"></canvas>
<script src="script.js"></script>
</body>
</html>
( css code )
body, html {
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
display: flex;
justify-content: center;
align-items: center;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
}
h1 {
color: #f5d442;
font-family: 'Righteous', sans-serif;
font-size: 40px;
margin-left: 50px;
}
.header {
display: flex;
align-items: center;
margin-bottom: 800px; /* Creates space between heading and buttons */
}
.buttons {
margin-left: 400px; /* Adjust the margin for the desired space */
}
.button{
font-family: 'Abril Fatface', serif;
font-size: 20px;
text-decoration: none;
color: #ffffff;
margin-right: 60px; /* Adjust as needed for button spacing */
position: relative;
display: inline-flex;
padding: 10px 30px;
background: #363636;
letter-spacing: 1px;
overflow: hidden;
}
.button:span{
position: absolute;
z-index: 1;
}
.button::before{
content: '';
position: absolute;
top: 0;
left: 0;
width: 20px;
height: 20px;
border-radius: 50%;
background: #2196f3;
transition: width 0.5s,height 0.5s;
}
.button:hover::before{
width: 300px;
height: 300px;
}
javascript file
const canvas = document.getElementById('waveCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particlesArray;
// Define particle properties
class Particle {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.size = Math.random() * 3 + 1;
this.speedX = Math.random() * 3 - 1.5;
this.speedY = Math.random() * 3 - 1.5;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
if (this.size > 0.2) this.size -= 0.1;
}
draw() {
ctx.fillStyle = 'white';
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
}
}
function init() {
particlesArray = [];
for (let i = 0; i < 1000; i++) {
particlesArray.push(new Particle());
}
}
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particlesArray.length; i++) {
particlesArray[i].update();
particlesArray[i].draw();
// Reset particles when they go off-screen
if (particlesArray[i].x < 0 || particlesArray[i].x > canvas.width || particlesArray[i].y < 0 || particlesArray[i].y > canvas.height) {
particlesArray[i] = new Particle();
}
}
}
init();
animate();
// Update canvas size when the window is resized
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
init();
});
Solution
The canvas being on top of everything, prevents the buttons from being hovered with the cursor.
Just add pointer-events: none;
for your canvas
element, so that it "ignores" the mouse cursor.
Answered By - CBroe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.