用GSAP实现烟花效果
时间: 2024-01-20 19:03:18 浏览: 179
烟花效果的实现
4星 · 用户满意度95%
使用GSAP(GreenSock Animation Platform)实现烟花效果需要一些基本的HTML、CSS和JS知识,以及对GSAP的了解。下面是一个基本的烟花效果实现步骤:
1. 创建HTML结构
首先,创建一个HTML文件并添加一个canvas元素,用于在其上绘制烟花效果。在此之前,可以添加一些样式来设置canvas的大小和位置。例如:
```html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Fireworks with GSAP</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000;
}
canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script>
<script src="script.js"></script>
</body>
</html>
```
2. 编写JS代码
在JS文件中,使用GSAP创建一个TweenMax对象,用于创建动画效果。下面的代码演示了如何创建一个基本的烟花效果:
```javascript
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
particles = [],
particleCount = 30,
colors = ["#FFFFFF", "#FF4136", "#0074D9", "#2ECC40", "#FFDC00", "#FF851B", "#B10DC9", "#111111"],
WIDTH = window.innerWidth,
HEIGHT = window.innerHeight;
canvas.width = WIDTH;
canvas.height = HEIGHT;
function Particle() {
this.radius = Math.random() * 20 + 10;
this.x = Math.random() * (WIDTH - this.radius * 2) + this.radius;
this.y = Math.random() * (HEIGHT - this.radius * 2) + this.radius;
this.color = colors[Math.floor(Math.random() * colors.length)];
this.vx = Math.random() * 4 - 2;
this.vy = Math.random() * 4 - 2;
this.gravity = 0.2;
this.opacity = 1;
}
Particle.prototype.draw = function() {
ctx.globalAlpha = this.opacity;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.fillStyle = this.color;
ctx.fill();
};
Particle.prototype.update = function() {
this.x += this.vx;
this.y += this.vy;
this.opacity -= 0.01;
this.radius -= 0.1;
};
function createParticles() {
for (var i = 0; i < particleCount; i++) {
particles.push(new Particle());
}
}
function drawParticles() {
for (var i = 0; i < particles.length; i++) {
particles[i].draw();
}
}
function updateParticles() {
for (var i = 0; i < particles.length; i++) {
particles[i].update();
if (particles[i].opacity <= 0) {
particles.splice(i, 1);
}
}
}
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(0, 0, WIDTH, HEIGHT);
drawParticles();
updateParticles();
}
createParticles();
animate();
```
代码解释:
- 创建canvas元素和一些变量,包括particles数组、particleCount(粒子数量)、颜色数组、canvas的宽度和高度。
- 创建Particle对象,包括粒子的半径、位置、颜色和速度等属性,以及draw(绘制)和update(更新)方法。
- 创建createParticles(创建粒子)、drawParticles(绘制粒子)和updateParticles(更新粒子)函数。
- 创建animate(动画)函数,使用requestAnimationFrame方法实现动画效果。
- 调用createParticles函数和animate函数,实现烟花效果的基本动画。
3. 添加交互效果
要使烟花效果更加有趣,可以添加一些交互效果。例如,点击鼠标时,可以在鼠标位置创建一个新的烟花效果。下面是如何实现这个交互效果的代码:
```javascript
function addFirework(x, y) {
for (var i = 0; i < particleCount; i++) {
var p = new Particle();
p.x = x;
p.y = y;
particles.push(p);
}
}
canvas.addEventListener("mousedown", function(e) {
var x = e.clientX,
y = e.clientY;
addFirework(x, y);
});
```
代码解释:
- 创建addFirework函数,用于在指定位置创建烟花效果。
- 在canvas元素上添加mousedown事件,当鼠标按下时,获取鼠标位置,并调用addFirework函数创建烟花效果。
4. 添加动画效果
为了让烟花效果更加生动,可以添加一些动画效果,例如粒子的旋转和缩放效果。使用TweenMax对象可以轻松地实现这些效果。下面是如何添加粒子旋转和缩放效果的代码:
```javascript
Particle.prototype.update = function() {
this.x += this.vx;
this.y += this.vy;
this.opacity -= 0.01;
this.radius -= 0.1;
this.rotation += Math.random() * 20 - 10;
this.scale -= 0.005;
};
function drawParticles() {
for (var i = 0; i < particles.length; i++) {
var p = particles[i],
tl = new TimelineMax();
tl.to(p, 0.5, {scale: 1.2, ease: Power1.easeInOut})
.to(p, 0.5, {scale: 1, ease: Power1.easeInOut})
.to(p, 0.5, {rotation: "+=360", ease: Power1.easeInOut});
p.draw();
}
}
```
代码解释:
- 在Particle对象的update方法中,添加rotation(旋转)和scale(缩放)属性,并在更新方法中增加旋转和缩放效果。
- 在drawParticles函数中,使用TweenMax对象创建一个时间轴,包括粒子的缩放和旋转效果。
- 调用p.draw方法绘制粒子。
5. 完整代码
下面是完整的烟花效果代码:
```html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Fireworks with GSAP</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000;
}
canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script>
<script>
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
particles = [],
particleCount = 30,
colors = ["#FFFFFF", "#FF4136", "#0074D9", "#2ECC40", "#FFDC00", "#FF851B", "#B10DC9", "#111111"],
WIDTH = window.innerWidth,
HEIGHT = window.innerHeight;
canvas.width = WIDTH;
canvas.height = HEIGHT;
function Particle() {
this.radius = Math.random() * 20 + 10;
this.x = Math.random() * (WIDTH - this.radius * 2) + this.radius;
this.y = Math.random() * (HEIGHT - this.radius * 2) + this.radius;
this.color = colors[Math.floor(Math.random() * colors.length)];
this.vx = Math.random() * 4 - 2;
this.vy = Math.random() * 4 - 2;
this.gravity = 0.2;
this.opacity = 1;
this.rotation = 0;
this.scale = 1;
}
Particle.prototype.draw = function() {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation * Math.PI / 180);
ctx.scale(this.scale, this.scale);
ctx.globalAlpha = this.opacity;
ctx.beginPath();
ctx.arc(0, 0, this.radius, 0, Math.PI * 2, false);
ctx.fillStyle = this.color;
ctx.fill();
ctx.restore();
};
Particle.prototype.update = function() {
this.x += this.vx;
this.y += this.vy;
this.opacity -= 0.01;
this.radius -= 0.1;
this.rotation += Math.random() * 20 - 10;
this.scale -= 0.005;
};
function createParticles() {
for (var i = 0; i < particleCount; i++) {
particles.push(new Particle());
}
}
function drawParticles() {
for (var i = 0; i < particles.length; i++) {
var p = particles[i],
tl = new TimelineMax();
tl.to(p, 0.5, {scale: 1.2, ease: Power1.easeInOut})
.to(p, 0.5, {scale: 1, ease: Power1.easeInOut})
.to(p, 0.5, {rotation: "+=360", ease: Power1.easeInOut});
p.draw();
}
}
function updateParticles() {
for (var i = 0; i < particles.length; i++) {
particles[i].update();
if (particles[i].opacity <= 0) {
particles.splice(i, 1);
}
}
}
function addFirework(x, y) {
for (var i = 0; i < particleCount; i++) {
var p = new Particle();
p.x = x;
p.y = y;
particles.push(p);
}
}
canvas.addEventListener("mousedown", function(e) {
var x = e.clientX,
y = e.clientY;
addFirework(x, y);
});
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(0, 0, WIDTH, HEIGHT);
drawParticles();
updateParticles();
}
createParticles();
animate();
</script>
</body>
</html>
```
运行代码后,点击鼠标可以在指定位置创建烟花效果,粒子会旋转和缩放,直到消失。可以尝试修改代码中的参数,例如粒子数量、颜色数组、速度等,以获得不同的烟花效果。
阅读全文