JavaScript實現(xiàn)好看的跟隨彩色氣泡效果
更新時間:2020年02月06日 10:21:09 作者:空谷丶幽蘭
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)好看的跟隨彩色氣泡效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript實現(xiàn)前端飛機大戰(zhàn)小游戲
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)前端飛機大戰(zhàn)小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
2022-05-05
深入學習JavaScript ES8中的函數(shù)式編程
函數(shù)式編程已經(jīng)成為現(xiàn)代JavaScript開發(fā)中的一種主要范式,它提供了一種更清晰、更模塊化、更可維護的代碼編寫方式,本文將深入探討ES8中的一些關鍵特性,并演示如何使用這些特性進行函數(shù)式編程實踐,有需要的可以參考下
2023-09-09
常用js,css文件統(tǒng)一加載方法(推薦) 并在加載之后調(diào)用回調(diào)函數(shù)
下面小編就為大家?guī)硪黄S胘s,css文件統(tǒng)一加載方法(推薦) 并在加載之后調(diào)用回調(diào)函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
2016-09-09 
