资料库 – 中文知识库
—
by
在这个数字时代,用技术表达爱意成为一种时尚。HTML5作为现代网页开发的核心技术,可以让我们轻松打造一个充满浪漫氛围的表白页面。下面,我将带你一步步实现一个用烟花传递爱意的浪漫表白页面。 准备工作 在开始之前,请确保你已经安装了以下工具: 文本编辑器 :如Visual Studio Code、Sublime Text等。 浏览器 :如Chrome、Firefox等,用于预览效果。 第一步:搭建基本页面结构 首先,创建一个名为 index.html 的文件,并输入以下代码:
// 设置canvas大小 canvas.width = window.innerWidth; canvas.height = window.innerHeight;
// 烟花类 class Firework { constructor(x, y, targetX, targetY, color) { this.x = x; this.y = y; this.targetX = targetX; this.targetY = targetY; this.color = color; this.distance = 0; this.distanceToTarget = Math.sqrt(Math.pow(this.targetX – this.x, 2) + Math.pow(this.targetY – this.y, 2)); this.angle = Math.atan2(this.targetY – this.y, this.targetX – this.x); this.speed = 2; this.acceleration = 1.05; this.brightness = Math.random() * 50 + 50; this.targetRadius = 1; }
update() { this.distance = Math.sqrt(Math.pow(this.targetX – this.x, 2) + Math.pow(this.targetY – this.y, 2)); if (this.distance < this.targetRadius + 5) { createParticles(this.targetX, this.targetY, this.color); } else { this.x += Math.cos(this.angle) * this.speed; this.y += Math.sin(this.angle) * this.speed; this.speed *= this.acceleration; } } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, 5, 0, Math.PI * 2); ctx.fillStyle = `hsl(${this.color}, 100%, ${this.brightness}%)`; ctx.fill(); } } // 粒子类 class Particle { constructor(x, y, color, velocity) { this.x = x; this.y = y; this.color = color; this.velocity = velocity; this.alpha = 1; this.friction = 0.95; this.gravity = 0.05; } update() { this.velocity.y += this.gravity; this.velocity.x *= this.friction; this.x += this.velocity.x; this.y += this.velocity.y; this.alpha -= 0.01; } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, 3, 0, Math.PI * 2); ctx.fillStyle = `hsla(${this.color}, 100%, 50%, ${this.alpha})`; ctx.fill(); } } // 创建烟花 function createFirework(x, y, targetX, targetY, color) { fireworks.push(new Firework(x, y, targetX, targetY, color)); } // 创建粒子 function createParticles(x, y, color) { const numParticles = 30; for (let i = 0; i < numParticles; i++) { const velocity = { x: (Math.random() - 0.5) * 4, y: (Math.random() - 0.5) * 4 }; particles.push(new Particle(x, y, color, velocity)); } } // 初始化烟花和粒子数组 let fireworks = []; let particles = []; // 渲染 function render() { requestAnimationFrame(render); ctx.globalCompositeOperation = 'destination-out'; ctx.fillStyle = 'rgba(0, 0, 0, 0.5)'; ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.globalCompositeOperation = 'lighter'; for (let i = fireworks.length - 1; i >= 0; i–) { fireworks[i].draw(); fireworks[i].update(); if (fireworks[i].distance < fireworks[i].targetRadius + 5 || fireworks[i].speed < 0.5) { fireworks.splice(i, 1); } } for (let i = particles.length - 1; i >= 0; i–) { particles[i].draw(); particles[i].update(); if (particles[i].alpha <= 0) { particles.splice(i, 1); } } } // 监听鼠标点击事件 canvas.addEventListener('click', (e) => { createFirework(e.clientX, e.clientY, e.clientX, e.clientY, Math.random() * 360); });
// 初始化 render(); 这段代码定义了烟花和粒子类,并实现了烟花效果。当用户点击屏幕时,会创建一个烟花,并从点击位置向目标位置发射。 第三步:美化页面 为了使页面更加浪漫,你可以添加以下样式: body { background: url(‘background.jpg’) no-repeat center center fixed; background-size: cover; } 将 background.jpg 替换为你喜欢的背景图片。 总结 通过以上步骤,你就可以轻松实现一个用烟花传递爱意的浪漫表白页面。你可以根据自己的需求,添加更多功能和样式,让页面更加个性化和浪漫。祝你在表白时好运!