-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
191 lines (162 loc) · 5.02 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Animated Lines</title>
<style>
body,
html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
background-color: black;
}
canvas {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var TWO_PI = Math.PI * 2;
var HALF_PI = Math.PI / 2;
var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
var scale = window.devicePixelRatio || 1;
var lines = [];
var frame = 0;
var width;
var height;
var gradient;
function Line(x, y) {
this.x = x;
this.y = y;
this.path = [];
this.pathLength = 0;
this.angle = 0;
this.speed = random(1, 4);
this.target = { x: x + 0.1, y: y + 0.1 };
this.thickness = Math.round(random(0.5, 3));
this.maxLength = Math.round(random(100, 350));
this.hasShadow = this.thickness > 2;
this.decay = random(0.0075, 0.05);
this.alpha = 1;
}
Line.prototype.step = function () {
if (this.pathLength >= this.maxLength) {
this.alpha -= this.decay;
return;
}
this.x += Math.cos(this.angle) * this.speed;
this.y += Math.sin(this.angle) * this.speed;
var isAnchor = false;
var target = this.target;
var dx = target.x - this.x;
var dy = target.y - this.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.speed) {
isAnchor = true;
this.x = target.x;
this.y = target.y;
this.steer();
}
this.path.push({
x: this.x,
y: this.y,
isAnchor: isAnchor,
});
this.pathLength++;
};
Line.prototype.draw = function () {
context.save();
context.globalAlpha = this.alpha;
context.lineWidth = this.thickness;
context.beginPath();
if (this.hasShadow) {
context.shadowOffsetX = 10;
context.shadowOffsetY = 20;
context.shadowBlur = 12;
context.shadowColor = "rgba(0,0,0,0.09)";
}
context.strokeStyle = "lime";
this.path.forEach(function (point, i) {
context[i === 0 ? "moveTo" : "lineTo"](point.x, point.y);
});
context.stroke();
context.beginPath();
context.arc(this.path[0].x, this.path[0].y, 4, 0, TWO_PI);
context.fill();
context.stroke();
context.restore();
};
Line.prototype.steer = function () {
var distance = random(50, 200);
var angle = random([-HALF_PI, 0, HALF_PI, -Math.PI]);
this.path = this.path.filter(function (point) {
return point.isAnchor === true;
});
this.target.x = this.x + Math.cos(angle) * distance;
this.target.y = this.y + Math.sin(angle) * distance;
this.angle = angle;
};
function random(min, max) {
if (arguments.length == 0) return Math.random();
if (Array.isArray(min))
return min[Math.floor(Math.random() * min.length)];
if (typeof min == "undefined") min = 1;
if (typeof max == "undefined") (max = min || 1), (min = 0);
return min + Math.random() * (max - min);
}
function draw(time) {
context.clearRect(0, 0, canvas.width, canvas.height);
context.lineCap = "round";
gradient = context.createLinearGradient(
width * 0.25,
0,
width * 0.75,
0
);
gradient.addColorStop(1, "#C2D832");
gradient.addColorStop(0, "#71C5E8");
context.strokeStyle = gradient;
lines = lines.filter(function (line) {
line.step();
return line.alpha > 0.01;
});
lines.forEach(function (line) {
line.draw();
});
if (frame % 12 === 0) {
var x = width * 0.5 + random(-150, 150);
var y = height * 0.5 + random(-100, 100);
lines.push(new Line(x, y));
}
frame++;
draw.raf = requestAnimationFrame(draw);
}
function resize(event) {
width = window.innerWidth;
height = window.innerHeight;
canvas.width = width * scale;
canvas.height = height * scale;
context.scale(scale, scale);
}
canvas.style.cssText =
"position:absolute;width:100%;height:100%;top:0;left:0;";
document.documentElement.style.cssText = "margin:0;padding:0;height:100%";
document.body.style.cssText =
"height:100%;margin:0;padding:0;overflow:hidden;";
document.body.appendChild(canvas);
window.addEventListener("resize", resize);
resize();
requestAnimationFrame(draw);
</script>
</body>
</html>