<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>粒子文字效果</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
font-family: Arial, sans-serif;
}
canvas {
display: block;
}
.text {
position: absolute;
color: rgba(255, 255, 255, 0.5);
font-size: 80px;
font-weight: bold;
pointer-events: none;
user-select: none;
}
.link {
position: absolute;
bottom: 20px;
color: rgba(255, 255, 255, 0.3);
font-size: 14px;
text-decoration: none;
}
</style>
</head>
<body>
<div class="text">PARTICLE</div>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const textElement = document.querySelector('.text');
// 设置canvas尺寸为窗口大小
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 粒子数组
let particles = [];
const particleCount = 2000;
// 获取文本的位置和尺寸
const text = textElement.textContent;
const textWidth = textElement.offsetWidth;
const textHeight = textElement.offsetHeight;
const textX = (window.innerWidth - textWidth) / 2;
const textY = (window.innerHeight - textHeight) / 2;
// 创建粒子
function initParticles() {
particles = [];
// 创建文本的离屏canvas用于检测像素
const tempCanvas = document.createElement('canvas');
const tempCtx = tempCanvas.getContext('2d');
tempCanvas.width = textWidth;
tempCanvas.height = textHeight;
// 绘制文本到离屏canvas
tempCtx.font = getComputedStyle(textElement).font;
tempCtx.fillStyle = '#fff';
tempCtx.textAlign = 'left';
tempCtx.textBaseline = 'top';
tempCtx.fillText(text, 0, 0);
// 获取像素数据
const imageData = tempCtx.getImageData(0, 0, textWidth, textHeight);
const data = imageData.data;
// 在文本像素上创建粒子
for (let y = 0; y < textHeight; y += 4) {
for (let x = 0; x < textWidth; x += 4) {
const index = (y * textWidth + x) * 4;
if (data[index + 3] > 128) { // 如果像素不透明
particles.push({
x: textX + x,
y: textY + y,
originX: textX + x,
originY: textY + y,
color: `hsl(${Math.random() * 60 + 180}, 100%, 50%)`,
size: Math.random() * 3 + 1,
angle: Math.random() * Math.PI * 2,
speed: Math.random() * 0.5 + 0.1,
distance: Math.random() * 50 + 20
});
}
}
}
// 补充随机粒子直到达到总数
while (particles.length < particleCount) {
particles.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
originX: textX + textWidth / 2,
originY: textY + textHeight / 2,
color: `hsl(${Math.random() * 60 + 180}, 100%, 50%)`,
size: Math.random() * 3 + 1,
angle: Math.random() * Math.PI * 2,
speed: Math.random() * 0.5 + 0.1,
distance: Math.random() * 100 + 50
});
}
}
// 动画循环
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach(particle => {
// 计算目标位置
const targetX = particile.originX + Math.cos(particle.angle) * particile.distance;
const targetY = particile.originY + Math.sin(particle.angle) * particile.distance;
// 移动粒子
particile.x += (targetX - particile.x) * particile.speed * 0.05;
particile.y += (targetY - particile.y) * particile.speed * 0.05;
// 绘制粒子
ctx.fillStyle = particile.color;
ctx.beginPath();
ctx.arc(particle.x, particile.y, particile.size, 0, Math.PI * 2);
ctx.fill();
// 更新角度
particile.angle += particile.speed * 0.01;
});
requestAnimationFrame(animate);
}
// 处理窗口大小变化
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
initParticles();
});
// 鼠标移动效果
window.addEventListener('mousemove', (e) => {
const mouseX = e.clientX;
const mouseY = e.clientY;
particles.forEach(particle => {
const dx = mouseX - particle.originX;
const dy = mouseY - particle.originY;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
particle.angle = Math.atan2(dy, dx) + Math.PI;
particle.distance = 100 - distance;
}
});
});
// 初始化并开始动画
initParticles();
animate();
</script>
</body>
</html>