-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# h5 canvas彩色网状线条粒子动画特效 | ||
|
||
#### 介绍 | ||
基于html5 canvas 2d画布绘制的彩色网状线条粒子动画特效,网状线条粒子跟随鼠标光标移动而变化。 | ||
|
||
#### 技术栈 | ||
|
||
HTML5 canvas + CSS3 + JS + JQuery | ||
|
||
#### 在线预览 | ||
|
||
[Github仓库](https://github.com/sunyctf/css-effects) | [Demo预览](https://sunyctf.github.io/js-effects/jquery彩色网状线条粒子动画/index.html) 🌐 [Gitee仓库](https://gitee.com/sunyctf/css-effects) | [Demo预览](https://sunyctf.gitee.io/js-effects/jquery彩色网状线条粒子动画/index.html) | ||
|
||
#### 软件架构 | ||
软件架构说明 | ||
|
||
|
||
#### 安装教程 | ||
|
||
1. xxxx | ||
2. xxxx | ||
3. xxxx | ||
|
||
#### 使用说明 | ||
|
||
1. xxxx | ||
2. xxxx | ||
3. xxxx | ||
|
||
#### 参与贡献 | ||
|
||
1. Fork 本仓库 | ||
2. 新建 Feat_xxx 分支 | ||
3. 提交代码 | ||
4. 新建 Pull Request | ||
|
||
|
||
#### 特技 | ||
|
||
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md | ||
2. GitHub 官方博客 [blog.github.com](https://github.blog) | ||
3. 你可以 [https://github.com/explore](https://github.com/explore) 这个地址来了解 GitHub 上的优秀开源项目 | ||
4. GitHub官方提供的使用手册 [https://docs.github.com/cn](https://docs.github.com/cn) | ||
5. GitHub中文社区 https://www.githubs.cn/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>html5 canvas彩色网状线条粒子动画特效</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
|
||
<script src="js/jquery.min.js"></script> | ||
<script src="js/script.js"></script> | ||
|
||
</head> | ||
<body style="overflow:hidden;"> | ||
|
||
<canvas></canvas> | ||
|
||
</body> | ||
</html> |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
try{ | ||
if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { | ||
|
||
}else{ | ||
$(function(){ | ||
var canvas = document.querySelector('canvas'), | ||
ctx = canvas.getContext('2d') | ||
canvas.width = window.innerWidth; | ||
canvas.height = window.innerHeight; | ||
ctx.lineWidth = .3; | ||
ctx.strokeStyle = (new Color(150)).style; | ||
|
||
var mousePosition = { | ||
x: 30 * canvas.width / 100, | ||
y: 30 * canvas.height / 100 | ||
}; | ||
|
||
var dots = { | ||
nb: 250, | ||
distance: 100, | ||
d_radius: 150, | ||
array: [] | ||
}; | ||
|
||
function colorValue(min) { | ||
return Math.floor(Math.random() * 255 + min); | ||
} | ||
|
||
function createColorStyle(r,g,b) { | ||
return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)'; | ||
} | ||
|
||
function mixComponents(comp1, weight1, comp2, weight2) { | ||
return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2); | ||
} | ||
|
||
function averageColorStyles(dot1, dot2) { | ||
var color1 = dot1.color, | ||
color2 = dot2.color; | ||
|
||
var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius), | ||
g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius), | ||
b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius); | ||
return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b)); | ||
} | ||
|
||
function Color(min) { | ||
min = min || 0; | ||
this.r = colorValue(min); | ||
this.g = colorValue(min); | ||
this.b = colorValue(min); | ||
this.style = createColorStyle(this.r, this.g, this.b); | ||
} | ||
|
||
function Dot(){ | ||
this.x = Math.random() * canvas.width; | ||
this.y = Math.random() * canvas.height; | ||
|
||
this.vx = -.5 + Math.random(); | ||
this.vy = -.5 + Math.random(); | ||
|
||
this.radius = Math.random() * 2; | ||
|
||
this.color = new Color(); | ||
console.log(this); | ||
} | ||
|
||
Dot.prototype = { | ||
draw: function(){ | ||
ctx.beginPath(); | ||
ctx.fillStyle = this.color.style; | ||
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false); | ||
ctx.fill(); | ||
} | ||
}; | ||
|
||
function createDots(){ | ||
for(i = 0; i < dots.nb; i++){ | ||
dots.array.push(new Dot()); | ||
} | ||
} | ||
|
||
function moveDots() { | ||
for(i = 0; i < dots.nb; i++){ | ||
|
||
var dot = dots.array[i]; | ||
|
||
if(dot.y < 0 || dot.y > canvas.height){ | ||
dot.vx = dot.vx; | ||
dot.vy = - dot.vy; | ||
} | ||
else if(dot.x < 0 || dot.x > canvas.width){ | ||
dot.vx = - dot.vx; | ||
dot.vy = dot.vy; | ||
} | ||
dot.x += dot.vx; | ||
dot.y += dot.vy; | ||
} | ||
} | ||
|
||
function connectDots() { | ||
for(i = 0; i < dots.nb; i++){ | ||
for(j = 0; j < dots.nb; j++){ | ||
i_dot = dots.array[i]; | ||
j_dot = dots.array[j]; | ||
|
||
if((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance){ | ||
if((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius){ | ||
ctx.beginPath(); | ||
ctx.strokeStyle = averageColorStyles(i_dot, j_dot); | ||
ctx.moveTo(i_dot.x, i_dot.y); | ||
ctx.lineTo(j_dot.x, j_dot.y); | ||
ctx.stroke(); | ||
ctx.closePath(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function drawDots() { | ||
for(i = 0; i < dots.nb; i++){ | ||
var dot = dots.array[i]; | ||
dot.draw(); | ||
} | ||
} | ||
|
||
function animateDots() { | ||
ctx.clearRect(0, 0, canvas.width, canvas.height); | ||
moveDots(); | ||
connectDots(); | ||
drawDots(); | ||
|
||
requestAnimationFrame(animateDots); | ||
} | ||
|
||
$('canvas').on('mousemove', function(e){ | ||
mousePosition.x = e.pageX; | ||
mousePosition.y = e.pageY; | ||
}); | ||
|
||
$('canvas').on('mouseleave', function(e){ | ||
mousePosition.x = canvas.width / 2; | ||
mousePosition.y = canvas.height / 2; | ||
}); | ||
|
||
createDots(); | ||
requestAnimationFrame(animateDots); | ||
}); | ||
} | ||
}catch(e){} |