本文首发于我的个人博客:http://cherryblog.site/
github项目地址:https://github.com/sunshine940326/canvas-nest
首先上效果图:
实现起来也是很简单的, 按照我的步骤一步一步来就可以了~
html代码
首先要制作我们的页面,用到的是html5的新标签canvas;其实canvas就是我们需要用javascript脚本语言来绘图的“画布”,只是相当于一个容器呈现我们画图的结果,所以我们在页面中需要创建一个充满屏幕的canvas
<body>
<canvas id="canvas"></canvas>
<div class="text">仿知乎动态粒子效果背景</div>
</body>
是的,body
中只有这两行代码就可以了,甚至可以只有一行代码
css样式
css样式也没有什么好说的,只是要让canvas充满屏幕就可以了
html{height: 100%}
body{margin: 0;height: 100%;
background: #fff;}
canvas{display: block;width: 100%;height: 100%;}
.text{
width: 100%;
background: transparent;
display: flex;
justify-content: center;
height: 100%;
line-height: 100%;
top: 0;
position: absolute;
top: 50%;
font-size: 50px;
}
写法不唯一,只要要你的canvas是充满整个屏幕的就好,当然,你要是不需要充满屏幕也可以~
js代码
说完了html和css,那么就剩js了,主要是通过js脚本来创建每个线段和粒子的~github上的例子中使用的是es6编写的,不过在demo中也使用了gulp安装babel可以将es6代码转化为es5(所以索demo中同时有es6的代码也有es5的代码,大家按需下载~)主要的思路如下
- 设置单个粒子的随机x,y坐标和圆圈的半径
- 使用canvas的api进行绘制粒子(圆圈)和粒子之前连线,设置一个范围,在此范围内的粒子圆心到圆心通过直线连接
- 让粒子在屏幕范围内移动
- 设置鼠标的交互事件,相当于以鼠标位置的x,y坐标为圆心,固定或随机值为半径重新创建了一个粒子,并且也在一定范围内也设置和其他粒子的连线(同第二步)
其实思路就以上五点,只不过我们需要了解canvas的api才能绘出我们想要的结果
设置单个粒子的随机x,y坐标和圆圈的半径
constructor(x, y)
canvas 画圆和画直线
drawCircle(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, 360)
ctx.closePath();
ctx.fillStyle = 'rgba(204, 204, 204, 0.3)';
ctx.fill();
}
drawLine(ctx, _circle) {
let dx = this.x - _circle.x;
let dy = this.y - _circle.y;
let d = Math.sqrt(dx * dx + dy * dy)
if (d < 150) {
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(_circle.x, _circle.y);
ctx.closePath();
ctx.strokeStyle = 'rgba(204, 204, 204, 0.3)';
ctx.stroke();
}
}
- 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
粒子移动
move(w, h) {
this._mx = (this.x < w && this.x > 0) ? this._mx : (-this._mx);
this._my = (this.y < h && this.y > 0) ? this._my : (-this._my);
this.x += this._mx / 2;
this.y += this._my / 2;
}
完整js
class Circle {
constructor(x, y) {
this.x = x;
this.y = y;
this.r = Math.random() * 10 ;
this._mx = Math.random() ;
this._my = Math.random() ;
}
drawCircle(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, 360)
ctx.closePath();
ctx.fillStyle = 'rgba(204, 204, 204, 0.3)';
ctx.fill();
}
drawLine(ctx, _circle) {
let dx = this.x - _circle.x;
let dy = this.y - _circle.y;
let d = Math.sqrt(dx * dx + dy * dy)
if (d < 150) {
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(_circle.x, _circle.y);
ctx.closePath();
ctx.strokeStyle = 'rgba(204, 204, 204, 0.3)';
ctx.stroke();
}
}
move(w, h) {
this._mx = (this.x < w && this.x > 0) ? this._mx : (-this._mx);
this._my = (this.y < h && this.y > 0) ? this._my : (-this._my);
this.x += this._mx / 2;
this.y += this._my / 2;
}
}
class currentCirle extends Circle {
constructor(x, y) {
super(x, y)
}
drawCircle(ctx) {
ctx.beginPath();
this.r = 8;
ctx.arc(this.x, this.y, this.r, 0, 360);
ctx.closePath();
ctx.fillStyle = 'rgba(255, 77, 54, 0.6)'
ctx.fill();
}
}
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
let w = canvas.width = canvas.offsetWidth;
let h = canvas.height = canvas.offsetHeight;
let circles = [];
let current_circle = new currentCirle(0, 0)
let draw = function () {
ctx.clearRect(0, 0, w, h);
for (let i = 0; i < circles.length; i++) {
circles[i].move(w, h);
circles[i].drawCircle(ctx);
for (j = i + 1; j < circles.length; j++) {
circles[i].drawLine(ctx, circles[j])
}
}
if (current_circle.x) {
current_circle.drawCircle(ctx);
for (var k = 1; k < circles.length; k++) {
current_circle.drawLine(ctx, circles[k])
}
}
requestAnimationFrame(draw)
}
let init = function (num) {
for (var i = 0; i < num; i++) {
circles.push(new Circle(Math.random() * w, Math.random() * h));
}
draw();
}
window.addEventListener('load', init(60));
window.onmousemove = function (e) {
e = e || window.event;
current_circle.x = e.clientX;
current_circle.y = e.clientY;
}
window.onmouseout = function () {
current_circle.x = null;
current_circle.y = null;
};
- 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
- 114
- 115
- 116
更多canvas的api
canvas现在可以写出很多酷炫的效果,详细的api请见:http://www.runoob.com/jsref/dom-obj-canvas.html