Newer
Older
// Adapted from https://blog.alexwendland.com/2015/particle-network-js-animations/
// Which is released under CC BY 4.0
function addTriangles(parent, color) {
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
var canvas = document.createElement("canvas")
parent.appendChild(canvas)
canvas.width = canvas.clientWidth;
canvas.height = canvas.clientHeight;
var ctx = canvas.getContext("2d");
var TAU = 2 * Math.PI;
times = [];
function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
update();
draw();
requestAnimationFrame(loop);
}
function Ball (startX, startY, startVelX, startVelY) {
this.x = startX || Math.random() * canvas.width;
this.y = startY || Math.random() * canvas.height;
this.vel = {
x: startVelX || (Math.random() * 2 - 1)/2,
y: startVelY || (Math.random() * 2 - 1)/2
};
this.update = function(canvas) {
if (this.x > canvas.width + 50 || this.x < -50) {
this.vel.x = -this.vel.x;
}
if (this.y > canvas.height + 50 || this.y < -50) {
this.vel.y = -this.vel.y;
}
this.x += this.vel.x;
this.y += this.vel.y;
};
this.draw = function(ctx, can) {
ctx.beginPath();
var distM = distMouse(this);
if (distM > 200) {
ctx.fillStyle = color;
ctx.globalAlpha = .2;
} else {
ctx.fillStyle = color;
ctx.globalAlpha = 1 - distM / 240;
}
ctx.arc((0.5 + this.x) | 0, (0.5 + this.y) | 0, 3, 0, TAU, false);
ctx.fill();
}
}
var balls = [];
for (var i = 0; i < canvas.width * canvas.height / (65*65); i++) {
balls.push(new Ball(Math.random() * canvas.width, Math.random() * canvas.height));
}
var lastTime = Date.now();
function update() {
var diff = Date.now() - lastTime;
for (var frame = 0; frame * 16.6667 < diff; frame++) {
for (var index = 0; index < balls.length; index++) {
balls[index].update(canvas);
}
}
lastTime = Date.now();
}
var mouseX = -1e9, mouseY = -1e9;
document.addEventListener('mousemove', function(event) {
mouseX = event.clientX;
mouseY = event.clientY;
});
function distMouse(ball) {
return Math.hypot(ball.x - mouseX, ball.y - mouseY);
}
function draw() {
for (var index = 0; index < balls.length; index++) {
var ball = balls[index];
ctx.beginPath();
for (var index2 = balls.length - 1; index2 > index; index2 += -1) {
var ball2 = balls[index2];
var dist = Math.hypot(ball.x - ball2.x, ball.y - ball2.y);
if (dist < 120) {
var distM = distMouse(ball2);
ctx.strokeStyle = color;
if (distM > 180) {
ctx.globalAlpha = .4;
} else {