html5粉色樱花雨加照片
时间: 2023-06-11 15:10:26 浏览: 125
HTML5七夕情人节表白网页制作【粉色樱花雨3D相册】HTML+CSS+JavaScript
您可以使用HTML5的canvas元素来创建一个动态的粉色樱花雨效果,并在其中添加一些照片。以下是一个简单的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Pink Sakura Rain</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f0f0f0;
}
canvas {
display: block;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<img src="your-photo.jpg" id="photo" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1;">
<script>
// Canvas setup
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Sakura petals
var petals = [];
var petalCount = 150;
var petalImage = new Image();
petalImage.src = "sakura-petal.png";
petalImage.onload = function() {
for (var i = 0; i < petalCount; i++) {
var petal = {
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
speed: 1 + Math.random() * 2,
angle: Math.random() * Math.PI * 2,
rotation: Math.random() * Math.PI * 0.02,
scale: 0.5 + Math.random(),
alpha: 0.5 + Math.random() * 0.5,
image: petalImage
};
petals.push(petal);
}
requestAnimationFrame(draw);
};
// Draw function
function draw() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw petals
for (var i = 0; i < petals.length; i++) {
var petal = petals[i];
petal.x += Math.cos(petal.angle) * petal.speed;
petal.y += Math.sin(petal.angle) * petal.speed;
petal.angle += Math.random() * 0.05 - 0.025;
petal.rotation += Math.random() * 0.01 - 0.005;
ctx.save();
ctx.translate(petal.x, petal.y);
ctx.rotate(petal.rotation);
ctx.globalAlpha = petal.alpha;
ctx.drawImage(petal.image, -petal.image.width * petal.scale / 2, -petal.image.height * petal.scale / 2, petal.image.width * petal.scale, petal.image.height * petal.scale);
ctx.restore();
if (petal.y > canvas.height) {
petal.y = -petal.image.height * petal.scale;
}
}
// Request next frame
requestAnimationFrame(draw);
}
</script>
</body>
</html>
```
该代码将在页面背景上创建一个粉色樱花雨效果,并在页面中央添加一张图片。您需要将`sakura-petal.png`替换为您自己的樱花瓣图片,将`your-photo.jpg`替换为您自己的图片,并根据需要调整样式和其他参数。
阅读全文