canvas实现文字变成沙粒交互动画效果代码
代码语言:html
所属分类:动画
代码描述:canvas实现文字变成沙粒交互动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css?family=Medula+One" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
overflow: hidden;
}
.container {
position: relative;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
h1 {
font-size: 8rem;
text-align: center;
color: #333;
text-transform: uppercase;
position: absolute;
z-index: 0;
transition: opacity 0.5s ease;
font-weight: 900;
padding: 1.5rem 0;
}
canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
}
.hidden {
opacity: 0;
}
</style>
</head>
<body>
<div class="container">
<h1 id="text">BFW.WIKI</h1>
<canvas id="canvas"></canvas>
</div>
<script >
document.addEventListener('DOMContentLoaded', () => {
// Get elements
const text = document.getElementById('text');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d', { alpha: true });
// Set canvas dimensions
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Configuration options
const config = {
particleDensity: 3, // Lower = more particles
particleSizeMultiplier: 1 / 20, // Adjust particle size relative to font
gravity: 0.15,
friction: 0.95,
bounce: 0.6,
returnSpeed: 0.05,
colorVariation: 15, // Random color variation
initialCycleDelay: 2000, // Initial delay before animation starts
fallingDuration: 5000, // How long particles fall
returnDuration: 800, // How long particles return
restDuration: 2000, // Rest time between cycles
canInteract: true, // Allow interaction with the mouse
interactiveForce: 5, // Force applied by mouse interaction
interactiveRadius: 100 // Radius of mouse interaction
};
// Mouse position tracking
let mouse = {
x: undefined,
y: undefined,
isPressed: false };
// Track mouse movement
canvas.addEventListener('mousemove', event => {
mouse.x = event.x;
mouse.y = event.y;
});
// Track mouse press
canvas.addEventListener('mousedown', () => {
mouse.isPressed = true;
});
canvas.addEventListener('mouseup', () => {
mouse.isPressed = false;
});
// Touch support
canvas.addEventListener('touchmove', event => {
event.preventDefault();
mouse.x = event.touches[0].clientX;
mouse.y = event.touches[0].clientY;
mouse.isPressed = true;
});
canvas.addEventListener('touchend', () => {
mouse.isPressed = false;
});
// Resize canvas on window resize with debounce
let resizeTimeout;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Recreate particles on resize if they exist
if (particles.length > 0) {
particles = createParticles();
}
}, 250);
});
// Particle class
class Particle {
constructor(x, y, color, size) {
this.x = x;
this.y = y;
this.originalX = x;
this.originalY = y;
this.baseColor = color;
this.color = this.addColorVariation(color);
this.size = size;
this.baseSize = size;
this.velocity = {
x: (Math.random() - 0.5) * 2,
y: Math.random() * -2 };
this.gravity = config.gravity * (0.8 + Math.random() * 0.4); // Slight variation in gravity
this.friction = config.friction;
this.active = false;
this.activeDelay = Math.random() * 500; // Random delay for each particle
this.returning = false;
this.returnSpeed = config.returnSpeed * (0.8 + Math.random() * 0.4); // Variation in return speed
this.opacity = 1;
this.fadeSpeed = 0.02;
}
// Add slight color variation to particles
addColorVariation(color) {
const rgbMatch = color.match(.........完整代码请登录后点击上方下载按钮下载查看
网友评论0