炫酷激光扫描特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>炫酷激光扫描特效</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #000;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: 'Arial', sans-serif;
        }
        
        canvas {
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1;
        }
        
        .content {
            position: relative;
            z-index: 2;
            color: #fff;
            text-align: center;
            padding: 20px;
            background-color: rgba(0, 0, 0, 0.5);
            border-radius: 10px;
            box-shadow: 0 0 20px rgba(0, 255, 255, 0.5);
            max-width: 80%;
        }
        
        h1 {
            color: #0ff;
            text-shadow: 0 0 10px #0ff, 0 0 20px #0ff;
            margin-bottom: 20px;
        }
        
        .link {
            color: #0f0;
            text-decoration: none;
            font-size: 18px;
            display: inline-block;
            margin-top: 20px;
            padding: 10px 20px;
            border: 1px solid #0f0;
            border-radius: 5px;
            transition: all 0.3s;
        }
        
        .link:hover {
            background-color: rgba(0, 255, 0, 0.2);
            box-shadow: 0 0 10px #0f0;
        }
    </style>
</head>
<body>
    <canvas id="laserCanvas"></canvas>
    
    <div class="content">
        <h1>激光扫描特效</h1>
        <p>这是一个使用HTML5 Canvas和JavaScript创建的炫酷激光扫描动画效果。</p>
        <p>激光线条会随机移动并留下渐变的轨迹,创造出科幻感十足的视觉效果。</p>
       
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const canvas = document.getElementById('laserCanvas');
            const ctx = canvas.getContext('2d');
            
            // 设置canvas大小为窗口大小
            function resizeCanvas() {
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
            }
            
            window.addEventListener('resize', resizeCanvas);
            resizeCanvas();
            
            // 激光点数组
            const lasers = [];
            const laserCount = 5;
            
            // 初始化激光点
            for (let i = 0; i < laserCount; i++) {
                lasers.push({
                    x: Math.random() * canvas.width,
                    y: Math.random() * canvas.height,
                    vx: (Math.random() - 0.5) * 5,
                    vy: (Math.random() - 0.5) * 5,
                    color: `hsl(${Math.random() * 360}, 100%, 50%)`,
                    trail: [],
                    maxTrail: 50
                });
            }
            
            // 动画循环
            function animate() {
                // 半透明黑色背景,用于创建拖尾效果
                ctx.fillStyle = 'rgba(0, 0, 0, 0.05)';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                
                // 更新和绘制每个激光点
                lasers.forEach(laser => {
                    // 更新位置
                    laser.x += laser.vx;
                    laser.y += laser.vy;
                    
                    // 边界检查
                    if (laser.x < 0 || laser.x > canvas.width) {
                        laser.vx = -laser.vx;
                        laser.x = Math.max(0, Math.min(canvas.width, laser.x));
                    }
                    
                    if (laser.y < 0 || laser.y > canvas.height) {
                        laser.vy = -laser.vy;
                        laser.y = Math.max(0, Math.min(canvas.height, laser.y));
                    }
                    
                    // 随机改变方向
                    if (Math.random() < 0.02) {
                        laser.vx = (Math.random() - 0.5) * 5;
                        laser.vy = (Math.random() - 0.5) * 5;
                    }
                    
                    // 添加当前位置到轨迹
                    laser.trail.push({x: laser.x, y: laser.y});
                    if (laser.trail.length > laser.maxTrail) {
                        laser.trail.shift();
                    }
                    
                    // 绘制轨迹
                    if (laser.trail.length > 1) {
                        ctx.beginPath();
                        ctx.moveTo(laser.trail[0].x, laser.trail[0].y);
                        
                        for (let i = 1; i < laser.trail.length; i++) {
                            const point = laser.trail[i];
                            ctx.lineTo(point.x, point.y);
                        }
                        
                        // 创建渐变线条
                        const gradient = ctx.createLinearGradient(
                            laser.trail[0].x, laser.trail[0].y,
                            laser.trail[laser.trail.length - 1].x, 
                            laser.trail[laser.trail.length - 1].y
                        );
                        
                        gradient.addColorStop(0, 'rgba(255, 255, 255, 0)');
                        gradient.addColorStop(0.3, laser.color);
                        gradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
                        
                        ctx.strokeStyle = gradient;
                        ctx.lineWidth = 2;
                        ctx.lineCap = 'round';
                        ctx.lineJoin = 'round';
                        ctx.stroke();
                    }
                    
                    // 绘制激光点
                    ctx.beginPath();
                    ctx.arc(laser.x, laser.y, 3, 0, Math.PI * 2);
                    ctx.fillStyle = laser.color;
                    ctx.fill();
                    
                    // 绘制光晕效果
                    const gradient = ctx.createRadialGradient(
                        laser.x, laser.y, 0,
                        laser.x, laser.y, 10
                    );
                    gradient.addColorStop(0, laser.color);
                    gradient.addColorStop(1, 'rgba(255, 255, 255, 0)');
                    
                    ctx.beginPath();
                    ctx.arc(laser.x, laser.y, 10, 0, Math.PI * 2);
                    ctx.fillStyle = gradient;
                    ctx.fill();
                });
                
                requestAnimationFrame(animate);
            }
            
            animate();
            
            // 添加鼠标交互效果
            canvas.addEventListener('mousemove', function(e) {
                // 让一个激光点跟随鼠标
                if (lasers.length > 0) {
                    const mouseLaser = lasers[0];
                    mouseLaser.vx = (e.clientX - mouseLaser.x) * 0.1;
                    mouseLaser.vy = (e.clientY - mouseLaser.y) * 0.1;
                }
            });
        });
    </script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值