使用JavaScript?+?HTML5?Canvas實(shí)現(xiàn)互動(dòng)愛(ài)心雨完整代碼
實(shí)例代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>情人節(jié)快樂(lè)!</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// 愛(ài)心類(lèi)
class Heart {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * -canvas.height;
this.size = Math.random() * 10 + 10;
this.speed = Math.random() * 2 + 1;
}
draw() {
ctx.beginPath();
const t = Date.now() / 1000;
const x = 16 * Math.pow(Math.sin(t), 3);
const y = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t);
const scaledX = this.x + x * this.size;
const scaledY = this.y - y * this.size;
ctx.moveTo(scaledX, scaledY);
ctx.bezierCurveTo(scaledX + this.size, scaledY - this.size, scaledX + 2 * this.size, scaledY + this.size, scaledX, scaledY + 2 * this.size);
ctx.bezierCurveTo(scaledX - 2 * this.size, scaledY + this.size, scaledX - this.size, scaledY - this.size, scaledX, scaledY);
ctx.fillStyle = 'red';
ctx.fill();
}
move() {
this.y += this.speed;
if (this.y > canvas.height) {
this.y = -this.size;
this.x = Math.random() * canvas.width;
}
}
}
// 創(chuàng)建愛(ài)心數(shù)組
const hearts = [];
for (let i = 0; i < 100; i++) {
hearts.push(new Heart());
}
// 動(dòng)畫(huà)循環(huán)
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
hearts.forEach(heart => {
heart.draw();
heart.move();
});
requestAnimationFrame(animate);
}
animate();
// 鼠標(biāo)互動(dòng)
canvas.addEventListener('mousemove', (e) => {
const mouseX = e.clientX;
const mouseY = e.clientY;
hearts.forEach(heart => {
const dx = mouseX - heart.x;
const dy = mouseY - heart.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
const angle = Math.atan2(dy, dx);
heart.x -= Math.cos(angle) * 2;
heart.y -= Math.sin(angle) * 2;
}
});
});
</script>
</body>
</html>效果圖

總結(jié)
到此這篇關(guān)于使用JavaScript + HTML5 Canvas實(shí)現(xiàn)互動(dòng)愛(ài)心雨的文章就介紹到這了,更多相關(guān)JS+HTML5 Canvas實(shí)現(xiàn)互動(dòng)愛(ài)心雨內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
canvas實(shí)現(xiàn)環(huán)形進(jìn)度條效果
本文主要介紹了canvas實(shí)現(xiàn)環(huán)形進(jìn)度條效果的實(shí)例。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流with綁定
這篇文章主要介紹了KnockoutJS 3.X API 第四章之?dāng)?shù)據(jù)控制流with綁定的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
通過(guò)Tabs方法基于easyUI+bootstrap制作工作站
本教程給大家介紹如何制作easyUI+bootstrap工作站,主要學(xué)習(xí)tabs方法,本文介紹非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-03-03
使用JavaScript判斷手機(jī)瀏覽器是橫屏還是豎屏問(wèn)題
這篇文章主要介紹了使用JavaScript判斷手機(jī)瀏覽器是橫屏還是豎屏問(wèn)題的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08
微信小程序使用Promise簡(jiǎn)化回調(diào)
本篇文章主要介紹了微信小程序使用Promise簡(jiǎn)化回調(diào),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

