帮我一段烟花的js代码
时间: 2023-09-09 19:04:32 浏览: 96
JavaScript实现的简单烟花特效代码
### 回答1:
好的,这是一段简单的烟花效果的 JavaScript 代码:
```
// 设置画布的宽高
var canvas = document.getElementById("fireworks");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 获取画布的上下文
var ctx = canvas.getContext("2d");
// 用于存储烟花的数组
var fireworks = [];
// 设置烟花的颜色
var colors = ["#f44336", "#e91e63", "#9c27b0", "#673ab7", "#3f51b5", "#2196f3", "#03a9f4", "#00bcd4", "#009688", "#4caf50", "#8bc34a", "#cddc39", "#ffeb3b", "#ffc107", "#ff9800", "#ff5722", "#795548", "#9e9e9e", "#607d8b"];
// 设置随机函数
function random(min, max) {
return Math.random() * (max - min) + min;
}
// 定义烟花类
function Firework(x, y, targetX, targetY, shade) {
this.x = x;
this.y = y;
this.startX = x;
this.startY = y;
this.hitX = false;
this.hitY = false;
this.coordinates = [];
this.coordinateCount = 5;
while (this.coordinateCount--) {
this.coordinates.push([this.x, this.y]);
}
this.angle = Math.atan2(targetY - this.startY, targetX - this.startX);
this.speed = random(2, 10);
this.acceleration = 1.05;
this.brightness = random(50, 70);
this.targetX = targetX;
this.targetY = targetY;
this.shade = shade;
}
// 绘制烟花
Firework.prototype.draw = function () {
ctx.beginPath();
ctx.moveTo(this.coordinates[this.coordinates.length - 1][0], this.coordinates[this.coordinates.length - 1][1]);
ctx.lineTo(this.x, this.y);
ctx.strokeStyle = "hsl(" + this.shade + ", 100%, " + this.brightness + "%)";
ctx.stroke();
ctx.beginPath();
ctx.arc(this.x, this.y, 1, 0, Math.PI * 2);
ctx.fillStyle = "hsl(" + this.shade + ", 100%, " + this.brightness + "%)";
ctx.fill();
};
### 回答2:
当然,请见下文:
```javascript
function Firework(x, y) {
this.x = x;
this.y = y;
this.radius = 3;
this.velocity = {
x: Math.random() * 4 - 2,
y: Math.random() * 4 - 2
};
this.gravity = 0.05;
this.opacity = 1;
}
Firework.prototype.update = function() {
this.x += this.velocity.x;
this.y += this.velocity.y;
this.velocity.y += this.gravity;
this.opacity -= 0.01;
};
Firework.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;
ctx.fill();
};
let fireworks = [];
function createFirework() {
const firework = new Firework(canvas.width / 2, canvas.height);
fireworks.push(firework);
}
function animate() {
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (let i = fireworks.length - 1; i >= 0; i--) {
fireworks[i].update();
fireworks[i].draw();
if (fireworks[i].opacity <= 0) {
fireworks.splice(i, 1);
}
}
if (Math.random() < 0.05) {
createFirework();
}
requestAnimationFrame(animate);
}
animate();
```
这是一个简单的使用HTML5 Canvas和JavaScript实现的烟花效果。它创建了一个Firework对象,包含了其位置、速度、半径、重力和透明度等属性,以及相关的update和draw方法。同时还有一个存储所有烟花对象的数组fireworks。createFirework函数用于创建新的烟花对象并将其添加到数组中。animate函数用于更新烟花的位置和透明度,并在每一帧中绘制烟花。最后使用requestAnimationFrame函数实现动画效果。
### 回答3:
当然可以帮您编写一段烟花的JavaScript代码。以下是一个简单的实现烟花效果的代码示例:
```javascript
// 创建一个画布
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext('2d');
// 定义烟花的类
function Firework() {
this.x = Math.random() * canvas.width;
this.y = canvas.height;
this.color = Math.random() < 0.5 ? 'red' : 'blue';
this.speedX = Math.random() * 3 - 1.5;
this.speedY = Math.random() * -10 - 5;
this.radius = Math.random() * 5 + 2;
}
// 更新烟花的位置
Firework.prototype.update = function() {
this.x += this.speedX;
this.y += this.speedY;
this.speedY += 0.1; // 加速度
this.radius -= 0.05; // 烟花半径逐渐减小
};
// 绘制烟花
Firework.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
};
// 创建烟花数组
var fireworks = [];
// 循环绘制烟花
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 创建新的烟花
if (Math.random() < 0.05) {
fireworks.push(new Firework());
}
// 更新和绘制烟花
for (var i = 0; i < fireworks.length; i++) {
fireworks[i].update();
fireworks[i].draw();
if (fireworks[i].radius <= 0) {
fireworks.splice(i, 1); // 烟花消失后从数组中移除
}
}
requestAnimationFrame(animate);
}
// 启动动画
animate();
```
通过执行以上代码,您可以在网页中看到随机生成的彩色烟花在画布中升起并逐渐消失的效果。您可以根据需求自定义代码中的参数,如烟花的颜色、大小、速度等。希望这段代码能满足您对烟花效果的需求。
阅读全文