JavaScript實(shí)現(xiàn)好看的跟隨彩色氣泡效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)跟隨彩色氣泡的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <style> *{ margin:0;padding:0; } body{overflow:hidden;} #canvas{ background-color:black; /*width:100%; height:100vh;*/ } </style> </head> <body> <canvas id="canvas" ></canvas> </body> <script> var canvas = document.querySelector('#canvas'); var ctx = canvas.getContext("2d"); var starlist = []; function init(){ canvas.width = window.innerWidth; canvas.height = window.innerHeight; } init(); window.onresize = init; canvas.addEventListener('mousemove',function(e){ starlist.push(new Star(e.offsetX,e.offsetY)); console.log(starlist) }) function random(min,max){ return Math.floor((max-min)*Math.random()+ min); } function Star(x,y){ this.x = x; this.y = y; this.vx = (Math.random()-0.5)*3; this.vy = (Math.random()-0.5)*3; this.color = 'rgb('+random(0,256)+','+random(0,256)+','+random(0,256)+')'; this.a = 1; console.log(this.color); this.draw(); } Star.prototype={ draw:function(){ ctx.beginPath(); ctx.fillStyle = this.color; ctx.globalCompositeOperation='lighter' ctx.globalAlpha= this.a; ctx.arc(this.x,this.y,30,0,Math.PI*2,false); ctx.fill(); this.updata(); }, updata(){ this.x+=this.vx; this.y+=this.vy; this.a*=0.98; } } console.log(new Star(150,200)); function render(){ ctx.clearRect(0,0,canvas.width,canvas.height) starlist.forEach((item,i)=>{ item.draw(); if(item.a<0.05){ starlist.splice(i,1); } }) requestAnimationFrame(render); } render(); </script> <div style="text-align:center;"> </div> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用JS實(shí)現(xiàn)氣泡跟隨鼠標(biāo)移動(dòng)的動(dòng)畫(huà)效果
- D3.js實(shí)現(xiàn)散點(diǎn)圖和氣泡圖的方法詳解
- 純JS代碼實(shí)現(xiàn)氣泡效果
- javascript實(shí)現(xiàn)很浪漫的氣泡冒出特效
- JavaScript實(shí)現(xiàn)鼠標(biāo)滑過(guò)處生成氣泡的方法
- js由下向上不斷上升冒氣泡效果實(shí)例
- Jquery插件分享之氣泡形提示控件grumble.js
- JS+CSS實(shí)現(xiàn)一個(gè)氣泡提示框
- jquery.cvtooltip.js 基于jquery的氣泡提示插件
- js實(shí)現(xiàn)可愛(ài)的氣泡特效
相關(guān)文章
es6數(shù)組之?dāng)U展運(yùn)算符操作實(shí)例分析
這篇文章主要介紹了es6數(shù)組之?dāng)U展運(yùn)算符操作,結(jié)合實(shí)例形式總結(jié)分析es6數(shù)組擴(kuò)展運(yùn)算符具體原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04Input文本框隨著輸入內(nèi)容多少自動(dòng)延伸的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇Input文本框隨著輸入內(nèi)容多少自動(dòng)延伸的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02JavaScript setTimeout()基本用法有哪些
這篇文章主要介紹了JavaScript setTimeout()基本用法有哪些,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11解決layui的table插件無(wú)法多層級(jí)獲取json數(shù)據(jù)的問(wèn)題
今天小編就為大家分享一篇解決layui的table插件無(wú)法多層級(jí)獲取json數(shù)據(jù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09js獲取當(dāng)前日期代碼適用于網(wǎng)頁(yè)頭部
大家在瀏覽網(wǎng)頁(yè)時(shí)可能會(huì)注意到網(wǎng)頁(yè)頭部有個(gè)不錯(cuò)的時(shí)間在顯示,看起來(lái)感覺(jué)不錯(cuò)于是自己也實(shí)現(xiàn)了一個(gè),下面是具體的代碼,感興趣的朋友可以參考下哈2013-06-06簡(jiǎn)單實(shí)現(xiàn)js浮動(dòng)框
這篇文章主要為大家詳細(xì)介紹了如何簡(jiǎn)單實(shí)現(xiàn)js浮動(dòng)框,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12