Repository navigation
Expand file tree
/
Copy pathpushpull.html
More file actions
167 lines (135 loc) · 4.51 KB
/
Copy pathpushpull.html
File metadata and controls
167 lines (135 loc) · 4.51 KB
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
<html>
<head>
<style>
body {
background-color: #000000;
margin: 0px;
}
canvas,
img {
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: optimize-contrast;
-ms-interpolation-mode: nearest-neighbor;
}
</style>
<title>stuff</title>
</head>
<body>
<canvas id="gridCanvas" width="1000" height="1000"></canvas>
<script type="text/javascript">
var ctx = document.getElementById('gridCanvas').getContext('2d');
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
var centerX = ctx.canvas.width / 2;
var centerY = ctx.canvas.height / 2;
var pixels;
var dotCount = 100;
var friction = 1;//.025;
var kick = 0;
var halfKick = kick / 2;
var gravity = 1
function Color(r, g, b, a){
this.r = r || 255;//Math.floor(Math.random() * 255);
this.g = g || 255;//Math.floor(Math.random() * 255);
this.b = b || 255;//Math.floor(Math.random() * 255);
this.a = a || 255;
}
function Point(x, y){
this.x = x || 0;
this.y = y || 0;
}
function Dot(x, y, m){
var self = this;
self.x = x || Math.floor(Math.random() * ctx.canvas.width);
self.y = y || Math.floor(Math.random() * ctx.canvas.height);
self.color = new Color();
self.vector = new Point((Math.random() *kick) - halfKick, (Math.random() * kick) - halfKick);
self.mass = m || 1;
self.Bounce = function(){
// Bounce
var bounce = -.5
if (self.x > ctx.canvas.width) { self.x = ctx.canvas.width; self.vector.x *= bounce;}
if (self.x < 0) { self.x = 0; self.vector.x *= bounce;}
if (self.y > ctx.canvas.height) { self.y = ctx.canvas.height; self.vector.y *= bounce;}
if (self.y < 0) { self.y = 0; self.vector.y *= bounce;}
}
self.Bonk = function(){
// Bonk
if (self.x > ctx.canvas.width) { self.x = ctx.canvas.width; self.vector.x = 0; self.vector.y = 0; }
if (self.x < 0) { self.x = 0; self.vector.x = 0; self.vector.y = 0; }
if (self.y > ctx.canvas.height) { self.y = ctx.canvas.height; self.vector.x = 0; self.vector.y = 0; }
if (self.y < 0) { self.y = 0; self.vector.x = 0; self.vector.y = 0; }
}
self.Wrap = function(){
// wrap
if (self.x > ctx.canvas.width) { self.x = 0; }
if (self.x < 0) { self.x = ctx.canvas.width; }
if (self.y > ctx.canvas.height) { self.y = 0; }
if (self.y < 0) { self.y = ctx.canvas.height; }
}
self.DoMovement = function(){
self.x += self.vector.x;
self.y += self.vector.y;
// "friction"
self.vector.x /= friction;
self.vector.y /= friction;
self.Bounce();
}
}
function init() {
var vectors = [];
for (var i = 0; i <= dotCount; i++) { vectors.push(new Dot()); }
DrawGrid(vectors);
}
function DrawGrid(dots) {
for(var i = dotCount; i > 0; i--){ var imo = i-1;
for(var j=imo; j > 0; j--){
var dx = dots[j].x - dots[i].x;
var dy = dots[j].y - dots[i].y;
var distance = Math.sqrt((dx*dx) + (dy * dy));
var mag = (gravity * dots[i].mass * dots[i].mass) / (distance * distance * distance);
if(distance > 1 ) {
dots[i].vector.x += dx * mag * dots[j].mass;
dots[i].vector.y += dy * mag * dots[j].mass;
dots[j].vector.x -= dx * mag * dots[i].mass;
dots[j].vector.y -= dy * mag * dots[i].mass;
} else {
dots[i].vector.x -= dx * mag * dots[j].mass;
dots[i].vector.y -= dy * mag * dots[j].mass;
dots[j].vector.x += dx * mag * dots[i].mass;
dots[j].vector.y += dy * mag * dots[i].mass;
}
}
}
for(var i = dotCount; i > 0; i--){
dots[i].DoMovement();
}
// clear screen
pixels = ctx.createImageData(ctx.canvas.width, ctx.canvas.height);
var index = 0;
var dot;
// draw
for (var i = dotCount; i > 0; i--) {
dot = dots[i];
var x = Math.floor(dot.x);
var y = Math.floor(dot.y);
index = (x + y * ctx.canvas.width) * 4;
if (!(x < 0 || y < 0 || x > ctx.canvas.width || y > ctx.canvas.height)){
var v = (Math.abs(dot.vector.x) + Math.abs(dot.vector.y)) * 20;
if (v > 255) {v = 255;}
pixels.data[index] = dot.color.r;
pixels.data[index+1] = dot.color.g-v;
pixels.data[index+2] = dot.color.b-v;
pixels.data[index+3] = dot.color.a;
}
}
ctx.putImageData(pixels, 0, 0);
setTimeout(function () { DrawGrid(dots) }, 1);
return;
}
init();
</script>
</body>
</html>