-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-链接.html
127 lines (101 loc) · 3.76 KB
/
canvas-链接.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="keywords" content="关键词">
<meta name="description" content="描述">
<meta name="author" content="练习 加油">
<style>
body{font-family: "Microsoft YaHei",serif;}
body,dl,dd,p,h1,h2,h3,h4,h5,h6{margin:0;}
ol,ul,li{margin:0;padding:0;list-style:none;}
img{border:none;}
canvas{
background: url("./img/timg2.jpg");
display: block;
}
</style>
</head>
<body>
<canvas></canvas>
<script>
let c = document.querySelector('canvas'),
cxt = c.getContext('2d');
resize();
window.addEventListener('resize',resize);
function resize() {
c.width = window.innerWidth;
c.height = window.innerHeight;
}
let mouse = {
x:null,
y:null,
max:20000//平方,吸引半径
}
//鼠标事件
window.addEventListener('mousemove',function (e) {
e = e || event;
mouse.x = e.clientX;
mouse.y = e.clientY;
})
window.onmouseout = function () {
mouse.x = c.width/2;
mouse.y = c.height/2;
}
//例子集合
let dots = [];
for(let i=0;i<400;i++){
dots.push({
x:Math.random()*c.width,
y:Math.random()*c.height,
xv:Math.random()*2-1,
yv:Math.random()*2-1,
max:6000
})
}
//创建
create();
function create(){
cxt.clearRect(0,0,c.width,c.height)
let ndots;
ndots = [mouse].concat(dots);
//不包含鼠标产生的点
dots.forEach(function (dot) {
// console.log(dot);
dot.x += dot.xv;
dot.y += dot.yv;
dot.xv *=(dot.x>c.width||dot.x<0)?-1:1;
dot.yv *=(dot.y>c.height||dot.x<0)?-1:1;
cxt.fillStyle = 'rgba(255,255,255,0.7)'
cxt.fillRect(dot.x-0.5,dot.y-0.5,1,1);
//循环对比例子
for(let i=0;i<ndots.length;i++){
let now = ndots[i],
disX = dot.x - now.x,
disY = dot.y - now.y,
dis = disX*disX + disY*disY;
if( dis < now.max ){
//鼠标控制
if(now === mouse && dis > now.max/2 ){
dot.x -= disX * 0.03;
dot.y -= disY * 0.03;
}
let scale = (now.max - dis)/now.max;
cxt.beginPath();
cxt.lineWidth = scale/2;
let color = cxt.createLinearGradient(dot.x,dot.y,now.x,now.y);
color.addColorStop(0,'rgba(255,255,255,1)');
color.addColorStop(1,'rgba(255,255,255,0.05)');
cxt.strokeStyle = color;
cxt.moveTo(dot.x,dot.y);
cxt.lineTo(now.x,now.y);
cxt.stroke()
}
}
})
requestAnimationFrame(create)
}
</script>
</body>
</html>