写一段苹果手机能执行的html代码,展示爱心烟花的效果
时间: 2023-04-08 08:01:17 浏览: 317
以下是展示爱心烟花效果的HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>爱心烟花</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: black;
}
canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var particles = [];
var particleCount = 100;
var particleSpeed = 2;
var particleSize = 5;
var particleColor = '#ff0000';
var particleShape = 'heart';
var particleImage = new Image();
particleImage.src = 'https://i.imgur.com/9Z8G7.png';
function Particle(x, y) {
this.x = x;
this.y = y;
this.vx = (Math.random() - 0.5) * particleSpeed;
this.vy = (Math.random() - 0.5) * particleSpeed;
this.size = particleSize;
this.color = particleColor;
this.shape = particleShape;
this.image = particleImage;
this.alpha = 1;
this.life = 100;
}
Particle.prototype.draw = function() {
ctx.save();
ctx.globalAlpha = this.alpha;
if (this.shape === 'heart') {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.bezierCurveTo(this.x + 10, this.y - 10, this.x + 20, this.y + 5, this.x, this.y + 20);
ctx.bezierCurveTo(this.x - 20, this.y + 5, this.x - 10, this.y - 10, this.x, this.y);
ctx.fill();
} else if (this.shape === 'image') {
ctx.drawImage(this.image, this.x, this.y, this.size, this.size);
} else {
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
ctx.restore();
};
Particle.prototype.update = function() {
this.x += this.vx;
this.y += this.vy;
this.alpha = this.life / 100;
this.life--;
};
function createParticles(x, y) {
for (var i = 0; i < particleCount; i++) {
var particle = new Particle(x, y);
particles.push(particle);
}
}
function updateParticles() {
for (var i = 0; i < particles.length; i++) {
var particle = particles[i];
particle.update();
if (particle.life <= 0) {
particles.splice(i, 1);
}
}
}
function drawParticles() {
for (var i = 0; i < particles.length; i++) {
var particle = particles[i];
particle.draw();
}
}
canvas.addEventListener('click', function(event) {
createParticles(event.clientX, event.clientY);
});
function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
updateParticles();
drawParticles();
requestAnimationFrame(loop);
}
loop();
</script>
</body>
</html>
```
这段代码会在苹果手机上展示一个黑色背景,当用户点击屏幕时,会在点击位置产生一些爱心烟花效果。这些效果由多个粒子组成,每个粒子都有自己的位置、速度、大小、颜色、形状和透明度。用户可以通过修改代码中的参数来改变烟花的效果,比如粒子数量、速度、大小、颜色、形状和图片等。