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

被折叠的 条评论
为什么被折叠?



