JSDM

HTML

 
1
<canvas id="canvas"></canvas>

CSS

xxxxxxxxxx
5
 
1
body {
2
  margin: 0;
3
  padding: 0;
4
  overflow: hidden;
5
}
? ?
? ?
必须是有效的URL
+ 添加另一个资源

JS

x
 
1
var canvas = document.getElementById('canvas');
2
var ctx = canvas.getContext('2d');
3
4
'floor|random|round|abs|sqrt|PI|atan2|sin|cos|pow|max|min'
5
  .split('|')
6
  .forEach(function(p) { window[p] = Math[p]; });
7
8
var TAU = PI*2;
9
10
function r(n) { return random()*n; }
11
function rrng(lo, hi) { return lo + r(hi-lo); }
12
function rint(lo, hi) { return lo + floor(r(hi - lo + 1)); }
13
function choose(args) { return args[rint(0, args.length-1)]; }
14
15
/*---------------------------------------------------------------------------*/
16
17
var W, H, frame, t0, time, DPR;
18
19
function resize() {
20
  var w = innerWidth;
21
  var h = innerHeight;
22
  DPR = devicePixelRatio || 1;
23
  
24
  canvas.style.width = w+'px';
25
  canvas.style.height = h+'px';
26
  
27
  W = canvas.width = w * DPR;
28
  H = canvas.height = h * DPR;
29
}
30
31
function loop(t) {
32
//  console.log(1000/(t-t0)); t0 = t;
33
  frame = requestAnimationFrame(loop);
34
  draw();
35
  time++;
36
}
37
38
function pause() {
39
  cancelAnimationFrame(frame);
40
  frame = frame ? null : requestAnimationFrame(loop);
41
}
42
43
function reset() {
44
  cancelAnimationFrame(frame);
45
  resize();
46
  ctx.clearRect(0, 0, W, H);
47
  init();
48
  time = 0;
49
  frame = requestAnimationFrame(loop);
50
}
51
52
/*---------------------------------------------------------------------------*/
53
54
function Particle(x, y, vx, vy, color) {
55
  this.px = x;
56
  this.py = y;
57
  this.x = x + vx;
58
  this.y = y + vy;
59
  this.vx = vx;
60
  this.vy = vy;
61
  this.color = color;
62
}
63
64
Particle.prototype.integrate = function() {
65
  this.vx = this.x - this.px;
66
  this.vy = this.y - this.py;
67
  this.px = this.x;
68
  this.py = this.y
69
  this.vy *= DAMP;
70
  this.vx *= DAMP;
71
};
72
73
Particle.prototype.move = function() {
74
  this.x += this.vx;
75
  this.y += this.vy;
76
};
77
78
Particle.prototype.gravitateTo = function(x, y) {
79
  var dx = x - this.x;
80
  var dy = y - this.y;
81
  var d = sqrt(dx*dx + dy*dy);
82
  this.vx += dx/d;
83
  this.vy += dy/d;
84
};
85
86
Particle.prototype.draw = function() {
87
  ctx.moveTo(this.px, this.py);
88
  ctx.lineTo(this.x, this.y);
89
};
90
91
/*---------------------------------------------------------------------------*/
92
93
var DAMP = 1;
94
var particles;
95
var gravitrons, G;
96
97
function color(n) {
98
  n = 64+floor(64*n);
99
  return 'rgba('+0+','+n+','+n+', 0.05)';
100
}
101
102
function init() {
103
  spawnParticles();
104
  DAMP = 0.89;
105
}
106
107
function spawnParticles() {
108
  N = 0;
109
  particles = [];
110
  for (var x = W/2 - 35; x < W/2 + 35; x += 1) 
111
    for (var y = H/2 - 35; y < H/2 + 35; y += 1)
112
      particles[N++] = new Particle(x, y, 0, 0, color(0.5));
113
      
114
  for (var i = 0; i < N; i++)
115
    particles[i].friend = choose(particles);
116
}
117
118
function draw() {
119
  ctx.beginPath();
120
  for (var i = 0; i < N; i++) {
121
    var p = particles[i];
122
    var f = p.friend;
123
    p.draw();
124
    p.integrate();
125
    p.gravitateTo(f.x, f.y);
126
    p.move();
127
  }
128
  ctx.strokeStyle = color((time < 300 ? time : 600 - time) / 300);
129
  ctx.stroke();
130
  if (time == 600) time = 0;
131
  DAMP *= 0.99996;
132
}
133
134
/*---------------------------------------------------------------------------*/
135
136
document.onclick = pause;
137
document.ondblclick = reset;
138
139
reset();
140
必须是有效的URL
+ 添加另一个资源
Close

文件管理 点击文件查看URL

图片

  1. 暂无文件

CSS

  1. 暂无文件

JavaScript

  1. 暂无文件

其他

  1. 暂无文件
拖动文件到上面的区域或者:
加载中 ..................