Ajax Loader
×
HTML
<canvas></canvas>
1
<canvas></canvas>
2
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
 
CSS
html, body {
1
html, body {
2
  background: #000;
3
  margin: 0;
4
}
5
 
6
canvas {
7
  position: absolute;
8
}
 
JavaScript
//Forked from http://andreasstorm.com/
1
//Forked from http://andreasstorm.com/
2
//Made by Bogden
3
 
4
//CANVAS
5
$(function(){
6
  var canvas = document.querySelector('canvas'),
7
      ctx = canvas.getContext('2d')
8
  canvas.width = window.innerWidth;
9
  canvas.height = window.innerHeight;
10
  ctx.lineWidth = .3;
11
  ctx.strokeStyle = (new Color(150)).style;
12
 
13
  var mousePosition = {
14
    x: 30 * canvas.width / 100,
15
    y: 30 * canvas.height / 100
16
  };
17
 
18
  var dots = {
19
    nb: 250,
20
    distance: 100,
21
    d_radius: 150,
22
    array: []
23
  };
24
 
25
  function colorValue(min) {
26
    return Math.floor(Math.random() * 255 + min);
27
  }
28
  
29
  function createColorStyle(r,g,b) {
30
    return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
31
  }
32
  
33
  function mixComponents(comp1, weight1, comp2, weight2) {
34
    return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
35
  }
36
  
37
  function averageColorStyles(dot1, dot2) {
38
    var color1 = dot1.color,
39
        color2 = dot2.color;
40
    
41
    var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
42
        g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
43
        b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
44
    return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
45
  }
46
  
47
  function Color(min) {
48
    min = min || 0;
49
    this.r = colorValue(min);
50
    this.g = colorValue(min);
51
    this.b = colorValue(min);
52
    this.style = createColorStyle(this.r, this.g, this.b);
53
  }
54
 
55
  function Dot(){
56
    this.x = Math.random() * canvas.width;
57
    this.y = Math.random() * canvas.height;
58
 
59
    this.vx = -.5 + Math.random();
60
    this.vy = -.5 + Math.random();
61
 
62
    this.radius = Math.random() * 2;
63
 
64
    this.color = new Color();
65
    console.log(this);
66
  }
67
 
68
  Dot.prototype = {
69
    draw: function(){
70
      ctx.beginPath();
71
      ctx.fillStyle = this.color.style;
72
      ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
73
      ctx.fill();
74
    }
75
  };
76
 
77
  function createDots(){
78
    for(i = 0; i < dots.nb; i++){
79
      dots.array.push(new Dot());
80
    }
81
  }
82
 
83
  function moveDots() {
84
    for(i = 0; i < dots.nb; i++){
85
 
86
      var dot = dots.array[i];
87
 
88
      if(dot.y < 0 || dot.y > canvas.height){
89
        dot.vx = dot.vx;
90
        dot.vy = - dot.vy;
91
      }
92
      else if(dot.x < 0 || dot.x > canvas.width){
93
        dot.vx = - dot.vx;
94
        dot.vy = dot.vy;
95
      }
96
      dot.x += dot.vx;
97
      dot.y += dot.vy;
98
    }
99
  }
100
 
101
  function connectDots() {
102
    for(i = 0; i < dots.nb; i++){
103
      for(j = 0; j < dots.nb; j++){
104
        i_dot = dots.array[i];
105
        j_dot = dots.array[j];
106
 
107
        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){
108
          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){
109
            ctx.beginPath();
110
            ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
111
            ctx.moveTo(i_dot.x, i_dot.y);
112
            ctx.lineTo(j_dot.x, j_dot.y);
113
            ctx.stroke();
114
            ctx.closePath();
115
          }
116
        }
117
      }
118
    }
119
  }
120
 
121
  function drawDots() {
122
    for(i = 0; i < dots.nb; i++){
123
      var dot = dots.array[i];
124
      dot.draw();
125
    }
126
  }
127
 
128
  function animateDots() {
129
    ctx.clearRect(0, 0, canvas.width, canvas.height);
130
    moveDots();
131
    connectDots();
132
    drawDots();
133
 
134
    requestAnimationFrame(animateDots); 
135
  }
136
 
137
  $('canvas').on('mousemove', function(e){
138
    mousePosition.x = e.pageX;
139
    mousePosition.y = e.pageY;
140
  });
141
 
142
  $('canvas').on('mouseleave', function(e){
143
    mousePosition.x = canvas.width / 2;
144
    mousePosition.y = canvas.height / 2;
145
  });
146
 
147
  createDots();
148
  requestAnimationFrame(animateDots); 
149
});
 

Untitled

CSSDeck G+