麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > 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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品夜色视频一级区 | 亚洲成人免费影视 | 精品一区二区免费 | 亚洲欧美国产精品va在线观看 | 免费一级毛片在线播放视频老 | 日韩视频高清 | a黄色网 | 国产一区二区三区视频在线 | 国产精品9191| 精品中文字幕在线观看 | 午夜视频在线观看91 | 国产成人高清成人av片在线看 | 欧美成人精品一区二区 | 国产 日韩 一区 | 国产高潮好爽受不了了夜色 | 99精品在线观看 | 亚洲第一激情 | 91网页| 看黄在线 | 一本色道久久综合亚洲精品图片 | 久久综合给合久久狠狠狠97色69 | 91精品国产乱码久久久久久久久 | 亚洲免费视 | 美国av免费看 | 女人久久久www免费人成看片 | 国产精品午夜性视频 | av成人在线电影 | 欧美亚洲综合在线 | 久久精品亚洲欧美日韩精品中文字幕 | 一区二区久久 | 国产1区在线观看 | 91一区二区三区久久久久国产乱 | av电影免费观看 | 欧美日韩精品一区二区三区不卡 | 精品亚洲夜色av98在线观看 | 色柚视频网站ww色 | 福利在线小视频 | 一区二区久久久久草草 | 在线a免费观看 | 免费午夜视频 | 成人福利视频网站 |