ES6與canvas如何實(shí)現(xiàn)鼠標(biāo)小球跟隨效果-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)不只是一家網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司;我們對營銷、技術(shù)、服務(wù)都有自己獨(dú)特見解,公司采取“創(chuàng)意+綜合+營銷”一體化的方式為您提供更專業(yè)的服務(wù)!我們經(jīng)歷的每一步也許不一定是最完美的,但每一步都有值得深思的意義。我們珍視每一份信任,關(guān)注我們的成都網(wǎng)站制作、做網(wǎng)站質(zhì)量和服務(wù)品質(zhì),在得到用戶滿意的同時,也能得到同行業(yè)的專業(yè)認(rèn)可,能夠為行業(yè)創(chuàng)新發(fā)展助力。未來將繼續(xù)專注于技術(shù)創(chuàng)新,服務(wù)升級,滿足企業(yè)一站式營銷型網(wǎng)站建設(shè)需求,讓再小的品牌網(wǎng)站制作也能產(chǎn)生價值!

首先,html部分,目前就一個canvas標(biāo)簽。

 <canvas id="canvas">
         當(dāng)前瀏覽器不支持!
 </canvas>

其次,css部分,沒有考慮美觀,大家喜歡的話,可以自己添加樣式

<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");
    //小球類
    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();
        }
    }
    //移動小球
    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è)計思路:

首先,獲取canvas對象,獲取上下文,設(shè)置一些基本的屬性。(canvas不做過多描述,具體的可以去w3自己研究)。然后,先定義一個Ball的類,里面有小球的圓心坐標(biāo)位置,以及半徑和顏色。在定義一個畫小球的方法,具體的畫圓實(shí)現(xiàn),不懂的可以去canvas文檔自己去看。

在定義一個會變的小球類并繼承Ball類。里面會有更新小球狀態(tài)的方法,用來改變小球的半徑以及顏色屬相。

最后,開啟一個定時器,當(dāng)鼠標(biāo)移動時,把生成的小球存儲到數(shù)組中,然后遍歷循環(huán)讀取小球,并改變小球的樣式,達(dá)到最終的效果。

最后附上完整代碼。直接拷貝瀏覽器運(yùn)行。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>會動的小球</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");
    //小球類
    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();
        }
    }
    //移動小球
    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)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

分享題目:ES6與canvas如何實(shí)現(xiàn)鼠標(biāo)小球跟隨效果-創(chuàng)新互聯(lián)
瀏覽地址:http://bm7419.com/article12/djhogc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司虛擬主機(jī)、云服務(wù)器、網(wǎng)站收錄、網(wǎng)站營銷、網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計