HTML5七夕情人节表白网页制作【canvas爱心绘制相册】HTML+CSS+JavaScript
以下是一个使用HTML、CSS和JavaScript创建的简单七夕情人节表白网页示例,其中包含一个Canvas绘制爱心和相册的简单功能。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>七夕情人节表白</title>
<style>
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f7f7f7;
}
canvas {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#album {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
#album img {
width: 100px;
height: 100px;
margin: 0 10px;
opacity: 0.5;
cursor: pointer;
}
#album img.active {
opacity: 1;
}
</style>
</head>
<body>
<canvas id="heartCanvas" width="400" height="400"></canvas>
<script>
// 爱心绘制函数
function drawHeart(ctx, x, y, scale) {
ctx.save();
ctx.translate(x, y);
ctx.scale(scale, scale);
var dx = 100, dy = 100, s = 1.5;
ctx.beginPath();
ctx.moveTo(-dx, -dy);
ctx.bezierCurveTo(-dx, -dy - s * dy, -dx + s * dx, -dy, -dx + dx, -dy);
ctx.bezierCurveTo(-dx + s * dx, -dy - s * dy, -dx, s * dx, -dx, dy);
ctx.bezierCurveTo(-dx, s * dy + dx * tan(PI / 4), -dx - s * dx, dy, -dx - dx, dy);
ctx.bezierCurveTo(-dx - s * dx, dy, -dx, dy - s * dy, -dx, dy - dx * tan(PI / 4));
ctx.closePath();
ctx.fillStyle = 'red';
ctx.fill();
ctx.restore();
}
// 绘制函数调用
window.onload = function() {
var canvas = document.getElementById('heartCanvas');
var ctx = canvas.getContext('2d');
setInterval(function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawHeart(ctx, canvas.width / 2, canvas.height / 2, 1);
}, 100);
};
</script>
<!-- 相册图片展示 -->
<div id="album">
<img src="photo1.jpg" alt="Photo 1" class="active">
<img src="photo2.jpg" alt="Photo 2">
<img src="photo3.jpg" alt="Photo 3">
<!-- 更多图片可以添加在此处 -->
</div>
<script>
// 相册图片点击事件
document.querySelectorAll('#album img').forEach(img => {
img.addEventListener('click', function() {
document.querySelector('#album .active').classList.remove('active');
this.classList.add('active');
});
});
</script>
</body>
</html>
在这个示例中,使用了HTML来定义结构,CSS来控制样式,以及
评论已关闭