使用JS實(shí)現(xiàn)氣泡跟隨鼠標(biāo)移動(dòng)的動(dòng)畫(huà)效果
氣泡跟隨鼠標(biāo)移動(dòng),并在每次點(diǎn)擊時(shí)產(chǎn)生不同的變化
效果如下

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
簡(jiǎn)單的氣泡效果
</title>
<style type="text/css">
body{background-color:#000000;margin:0px;overflow:hidden}
</style>
</head>
<body>
</body>
</html>
<script>
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
windowW = window.screen.width ,
windowH = window.screen.height ,
Mx,
My,
paused = true;
suzu = [];
booms = [];
boomks = [];
start();
canvas.onmousemove = function(e) {
var loc = canvasMove(e.clientX, e.clientY);
Mx = loc.x;
My = loc.y
};
canvas.onmousedown = function() {
creatarry(Mx, My);
paused = !paused
};
function creatarry(a, b) {
for (var i = 0; i < 40; ++i) {
booms[i] = {
x: a,
y: b,
gravity: 0.3,
speedX: Math.random() * 20 - 10,
speedY: Math.random() * 15 - 7,
radius: Math.random() * 15,
color: Math.random() * 360,
apc: 0.6
};
boomks.push(booms[i]);
if (boomks.length > 300) {
boomks.shift()
};
console.log(boomks)
}
};
function loop1() {
boomks.forEach(function(circle) {
context.beginPath();
context.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2, false);
context.fillStyle = 'hsla(' + circle.color + ',100%,60%,' + circle.apc + ')';
context.fill();
movecircles(circle)
})
}
function movecircles(circle) {
circle.x += circle.speedX;
circle.speedY += circle.gravity;
circle.y += circle.speedY;
circle.apc -= 0.008
}
function canvasMove(x, y) {
var bbox = canvas.getBoundingClientRect();
return {
x: x - bbox.left * (canvas.width / bbox.width),
y: y - bbox.top * (canvas.height / bbox.height)
}
};
function start() {
document.body.appendChild(canvas);
canvas.width = windowW;
canvas.height = windowH;
setInterval(fang, 25)
}
function fang() {
context.clearRect(0, 0, canvas.width, canvas.height);
loop1();
loop()
}
function loop() {
var circle = new createCircle(Mx, My);
suzu.push(circle);
for (i = 0; i < suzu.length; i++) {
var ss = suzu[i];
ss.render(context);
ss.update()
}
if (suzu.length > 1000) {
suzu.shift()
}
}
function createCircle(x, y) {
this.x = x;
this.y = y;
this.color = Math.random() * 360;
this.radius = Math.random() * 25;
this.xVel = Math.random() * 5 - 2;
this.apc = 0.6;
this.gravity = 0.07;
this.yVel = Math.random() * 10 - 3;
this.render = function(c) {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
c.fillStyle = 'hsla(' + this.color + ',100%,60%,' + this.apc + ')';
c.fill()
};
this.update = function() {
if (!paused) {
this.yVel += this.gravity;
this.y += this.yVel
} else {
this.y -= 5
}
this.x += this.xVel;
this.apc -= 0.01;
if (this.radius > 1) {
this.radius -= 0.4
}
} }
</script>
總結(jié)
以上所述是小編給大家?guī)?lái)了使用JS實(shí)現(xiàn)氣泡跟隨鼠標(biāo)移動(dòng)的動(dòng)畫(huà)效果 ,希望對(duì)大家有所幫助!
- javascript實(shí)現(xiàn)圖片跟隨鼠標(biāo)移動(dòng)效果的方法
- js實(shí)現(xiàn)文字跟隨鼠標(biāo)移動(dòng)而移動(dòng)的方法
- js圖片跟隨鼠標(biāo)移動(dòng)代碼
- javascript DIV實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)
- 基于JavaScript實(shí)現(xiàn)鼠標(biāo)懸浮彈出跟隨鼠標(biāo)移動(dòng)的帶箭頭的信息層
- js實(shí)現(xiàn)圖片跟隨鼠標(biāo)移動(dòng)效果
- javascript跟隨鼠標(biāo)x,y坐標(biāo)移動(dòng)的字效果
- js實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)的小球
- js實(shí)現(xiàn)圖片放大并跟隨鼠標(biāo)移動(dòng)特效
- javascript實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)的圖片
相關(guān)文章
微信小程序之?dāng)?shù)據(jù)緩存的實(shí)例詳解
這篇文章主要介紹了微信小程序之?dāng)?shù)據(jù)緩存的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
微信小程序使用WxJava獲取用戶手機(jī)號(hào)步驟
這篇文章主要介紹了微信小程序使用WxJava獲取用戶手機(jī)號(hào)的相關(guān)資料,還詳細(xì)講解了WxMpService接口的主要功能和常用方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
JavaScript實(shí)現(xiàn)批量重命名文件
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)批量重命名文件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12
二級(jí)域名或跨域共享Cookies的實(shí)現(xiàn)方法
適用于Asp。 在主域名設(shè)置的Cookie,在各子域名共用;適用于博客等提供二級(jí)域名。這個(gè)問(wèn)題,以網(wǎng)上有眾多帖子,可惜都沒(méi)有完整解決。2008-08-08
原生js添加節(jié)點(diǎn)appendChild、insertBefore方式
這篇文章主要介紹了原生js添加節(jié)點(diǎn)appendChild、insertBefore方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
js瀏覽器滾動(dòng)條卷去的高度scrolltop(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇js瀏覽器滾動(dòng)條卷去的高度scrolltop(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
一個(gè)層慢慢增高展開(kāi),有種向下滑動(dòng)的效果
一個(gè)層慢慢增高展開(kāi),有種向下滑動(dòng)的效果...2006-11-11
解決JSON.parse轉(zhuǎn)化不規(guī)范json字符串的問(wèn)題
這篇文章主要介紹了解決JSON.parse轉(zhuǎn)化不規(guī)范json字符串的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

