1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<canvas id="heartCanvas"></canvas>
<script>
document.addEventListener("DOMContentLoaded", function () {
const canvas = document.getElementById("heartCanvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.position = "fixed";
canvas.style.top = "0";
canvas.style.left = "0";
canvas.style.pointerEvents = "none";
let hearts = [];
function Heart(x, y, size, speedX, speedY) {
this.x = x;
this.y = y;
this.size = size;
this.speedX = speedX;
this.speedY = speedY;
this.opacity = 1;
}
Heart.prototype.update = function () {
this.x += this.speedX;
this.y += this.speedY;
this.opacity -= 0.02;
};
Heart.prototype.draw = function () {
ctx.globalAlpha = this.opacity;
ctx.fillStyle = "red";
ctx.font = `${this.size}px Arial`;
ctx.fillText("❤️", this.x, this.y);
ctx.globalAlpha = 1;
};
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
hearts.forEach((heart, index) => {
heart.update();
heart.draw();
if (heart.opacity <= 0) {
hearts.splice(index, 1);
}
});
requestAnimationFrame(animate);
}
document.addEventListener("mousemove", function (e) {
let size = Math.random() * 10 + 15;
let speedX = (Math.random() - 0.5) * 2;
let speedY = (Math.random() - 0.5) * 2;
hearts.push(new Heart(e.clientX, e.clientY, size, speedX, speedY));
});
animate();
});
</script>
|