粒子文字效果

<!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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值