這篇文章將為大家詳細(xì)講解有關(guān)ES6與canvas如何實(shí)現(xiàn)鼠標(biāo)小球跟隨效果,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

首先,html部分,目前就一個(gè)canvas標(biāo)簽。
<canvas id="canvas"> 當(dāng)前瀏覽器不支持! </canvas>
其次,css部分,沒(méi)有考慮美觀,大家喜歡的話,可以自己添加樣式
<style>
body{
margin: 90px;
}
#canvas{
box-shadow: 0 0 5px;
}
</style>最后,看下js實(shí)現(xiàn)部分
<script>
const canvas = document.getElementById("canvas");
canvas.height = 600;
canvas.width = 1000;
canvas.style.backgroundColor = "#000";
const ctx = canvas.getContext("2d");
//小球類(lèi)
class Ball{
constructor(x, y, color){
this.x = x;
this.y = y;
this.color = color;
//小球半徑默認(rèn)40
this.r = 40;
}
//繪制小球
render(){
ctx.save();
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.restore();
}
}
//移動(dòng)小球
class MoveBall extends Ball{
constructor(x, y, color){
super(x, y, color);
this.dX = Math.floor(Math.random()*5+1);
this.dY = Math.floor(Math.random()*5+1);
this.dR = Math.floor(Math.random()*5+1);
}
upData(){
this.x += this.dX;
this.y += this.dY;
this.r -= this.dR;
if(this.r < 0){
this.r = 0;
}
}
}
let ballArry = [];
let colorArry = ['red', 'green', 'pink', 'yellow', 'blue'];
canvas.addEventListener("mousemove",function(e){
ballArry.push(new MoveBall(e.offsetX, e.offsetY, colorArry[Math.floor(Math.random()*5)]));
})
setInterval(function(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
for(let i=0;i<ballArry.length;i++){
ballArry[i].render();
ballArry[i].upData();
}
},50);
</script>稍作解釋下我的設(shè)計(jì)思路:
首先,獲取canvas對(duì)象,獲取上下文,設(shè)置一些基本的屬性。(canvas不做過(guò)多描述,具體的可以去w3自己研究)。然后,先定義一個(gè)Ball的類(lèi),里面有小球的圓心坐標(biāo)位置,以及半徑和顏色。在定義一個(gè)畫(huà)小球的方法,具體的畫(huà)圓實(shí)現(xiàn),不懂的可以去canvas文檔自己去看。
在定義一個(gè)會(huì)變的小球類(lèi)并繼承Ball類(lèi)。里面會(huì)有更新小球狀態(tài)的方法,用來(lái)改變小球的半徑以及顏色屬相。
最后,開(kāi)啟一個(gè)定時(shí)器,當(dāng)鼠標(biāo)移動(dòng)時(shí),把生成的小球存儲(chǔ)到數(shù)組中,然后遍歷循環(huán)讀取小球,并改變小球的樣式,達(dá)到最終的效果。
最后附上完整代碼。直接拷貝瀏覽器運(yùn)行。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>會(huì)動(dòng)的小球</title>
<style>
body{
margin: 90px;
}
#canvas{
box-shadow: 0 0 5px;
}
</style>
</head>
<body>
<canvas id="canvas">
當(dāng)前瀏覽器不支持!
</canvas>
<script>
const canvas = document.getElementById("canvas");
canvas.height = 600;
canvas.width = 1000;
canvas.style.backgroundColor = "#000";
const ctx = canvas.getContext("2d");
//小球類(lèi)
class Ball{
constructor(x, y, color){
this.x = x;
this.y = y;
this.color = color;
//小球半徑默認(rèn)40
this.r = 40;
}
//繪制小球
render(){
ctx.save();
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.restore();
}
}
//移動(dòng)小球
class MoveBall extends Ball{
constructor(x, y, color){
super(x, y, color);
this.dX = Math.floor(Math.random()*5+1);
this.dY = Math.floor(Math.random()*5+1);
this.dR = Math.floor(Math.random()*5+1);
}
upData(){
this.x += this.dX;
this.y += this.dY;
this.r -= this.dR;
if(this.r < 0){
this.r = 0;
}
}
}
let ballArry = [];
let colorArry = ['red', 'green', 'pink', 'yellow', 'blue'];
canvas.addEventListener("mousemove",function(e){
ballArry.push(new MoveBall(e.offsetX, e.offsetY, colorArry[Math.floor(Math.random()*5)]));
})
setInterval(function(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
for(let i=0;i<ballArry.length;i++){
ballArry[i].render();
ballArry[i].upData();
}
},50);
</script>
</body>
</html>關(guān)于“ES6與canvas如何實(shí)現(xiàn)鼠標(biāo)小球跟隨效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
分享題目:ES6與canvas如何實(shí)現(xiàn)鼠標(biāo)小球跟隨效果-創(chuàng)新互聯(lián)
瀏覽地址:http://www.chinadenli.net/article12/djhogc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、虛擬主機(jī)、云服務(wù)器、網(wǎng)站收錄、網(wǎng)站營(yíng)銷(xiāo)、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容