国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > CSS > 正文

ES6與canvas實現鼠標小球跟隨效果

2024-07-11 09:08:04
字體:
來源:轉載
供稿:網友

最近閑來無聊,看了下ES6的語法,結合canvas實現了動畫特效——隨著鼠標的移動,會有小球跟隨且自動消失的動畫。

首先,html部分,目前就一個canvas標簽。

 <canvas id="canvas">         當前瀏覽器不支持! </canvas>

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

<style>        body{            margin: 90px;        }        #canvas{            box-shadow: 0 0 5px;        }    </style>

最后,看下js實現部分

<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;            //小球半徑默認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>

稍作解釋下我的設計思路:

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

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

最后,開啟一個定時器,當鼠標移動時,把生成的小球存儲到數組中,然后遍歷循環讀取小球,并改變小球的樣式,達到最終的效果。

最后附上完整代碼。直接拷貝瀏覽器運行。

<!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">        當前瀏覽器不支持!    </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;            //小球半徑默認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>

總結

以上所述是小編給大家介紹的ES6與canvas實現鼠標小球跟隨鼠標效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!

 

注:相關教程知識閱讀請移步到CSS教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广安市| 祁阳县| 五常市| 茂名市| 论坛| 安徽省| 卢湾区| 郑州市| 宽甸| 政和县| 龙泉市| 宝丰县| 常宁市| 香港 | 南宁市| 东乡县| 安多县| 沙雅县| 红河县| 资兴市| 台北市| 巩留县| 滁州市| 麟游县| 尚志市| 白朗县| 万源市| 吉林市| 庆城县| 神池县| 蓝山县| 洱源县| 汪清县| 探索| 仁布县| 东兰县| 双峰县| 康保县| 调兵山市| 平度市| 察哈|