這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)怎么在JavaScript中使用canvas實(shí)現(xiàn)一個(gè)跟隨鼠標(biāo)事件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
作為一家“創(chuàng)意+整合+營(yíng)銷”的成都網(wǎng)站建設(shè)機(jī)構(gòu),我們?cè)跇I(yè)內(nèi)良好的客戶口碑。創(chuàng)新互聯(lián)公司提供從前期的網(wǎng)站品牌分析策劃、網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、創(chuàng)意表現(xiàn)、網(wǎng)頁(yè)制作、系統(tǒng)開(kāi)發(fā)以及后續(xù)網(wǎng)站營(yíng)銷運(yùn)營(yíng)等一系列服務(wù),幫助企業(yè)打造創(chuàng)新的互聯(lián)網(wǎng)品牌經(jīng)營(yíng)模式與有效的網(wǎng)絡(luò)營(yíng)銷方法,創(chuàng)造更大的價(jià)值。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> body { margin: 0; overflow: hidden; } #canvas { background: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var circleList = []; canvas.width = window.innerWidth; canvas.height = window.innerHeight; canvas.addEventListener('mousemove', function (e) { // 將對(duì)象push到數(shù)組中,畫(huà)出來(lái)的彩色小點(diǎn)可以看作每一個(gè)對(duì)象中記錄著信息 然后存在數(shù)組中 circleList.push(new Circle(e.clientX, e.clientY)); }) //取x到y(tǒng)之間隨機(jī)數(shù):Math.round(Math.random()*(y-x)+x) 包括y function random(min, max) { return Math.round(Math.random() * (max - min) + min); } function Circle(x, y) { this.x = x; this.y = y; this.vx = (Math.random() - 0.5) * 3; //隨機(jī)出來(lái)一個(gè)正數(shù),或者負(fù)數(shù)。乘3是為了讓速度變得大一點(diǎn) this.vy = (Math.random() - 0.5) * 3; this.color = 'rgb(' + random(0, 255) + ',' + random(0, 255) + ',' + random(0, 255) + ')'; this.a = 1; // 初始透明度 this.draw(); } Circle.prototype = { draw() { context.beginPath(); context.fillStyle = this.color; context.globalCompositeOperation = 'lighter'; context.globalAlpha = this.a; //全局透明度 context.arc(this.x, this.y, 30, 0, Math.PI * 2, false); context.fill(); this.update(); }, update() { // 根據(jù)速度更新每一個(gè)小圓的位置 this.x += this.vx; this.y += this.vy; this.a *= 0.98; } } function render() { //把原來(lái)的內(nèi)容區(qū)域清除掉 context.clearRect(0, 0, canvas.width, canvas.height); circleList.forEach(function (ele, i) { ele.draw(); if (ele.a < 0.05) { circleList.splice(i, 1); } }); requestAnimationFrame(render); //動(dòng)畫(huà),會(huì)根據(jù)瀏覽器的刷新頻率更新動(dòng)畫(huà) } render(); </script> </body> </html>
上述就是小編為大家分享的怎么在JavaScript中使用canvas實(shí)現(xiàn)一個(gè)跟隨鼠標(biāo)事件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)題目:怎么在JavaScript中使用canvas實(shí)現(xiàn)一個(gè)跟隨鼠標(biāo)事件
標(biāo)題路徑:http://jinyejixie.com/article10/ijgjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、用戶體驗(yàn)、微信小程序、企業(yè)網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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)