-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
165 lines (141 loc) · 4.24 KB
/
canvas.js
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
'use strict';
(function () {
var c = document.getElementById('c'),
ctx = c.getContext('2d'),
hero = c.parentElement,
width = hero.offsetWidth,
height = hero.offsetHeight,
xCentre = width / 2,
yCentre = height / 2,
xSpeed = width / 500,
ySpeed = height / 500,
voidSize = 100,
density = 17,
minNodeSize = 1,
maxNodeSize = 1,
baseColour = {
r: 255, g: 255, b: 255
},
totalNodes = Math.round(width / density * height / density);
var play = true,
iter = 0;
c.width = width;
c.height = height;
var imgData = ctx.getImageData(0, 0, width, height);
var gaussianRand = function gaussianRand() {
var rand = 0;
for (var i = 0; i < 2; i += 1) {
rand += Math.random();
}
return rand / 2;
};
var insideVoid = function insideVoid(x, y, xFromC, yFromC) {
return yFromC < voidSize && xFromC + y - yCentre < voidSize;
};
var outsideBounds = function outsideBounds(x, y) {
return x < 0 || y < 0 || x >= width || y >= height;
};
var updateDistFromCentre = function updateDistFromCentre(node) {
node.xFromC = Math.abs(node.x - xCentre);
node.yFromC = Math.abs(node.y - yCentre);
};
var generateNodes = function generateNodes() {
var sizeRange = maxNodeSize - minNodeSize;
var output = new Array(totalNodes);
for (var i = 0; i < totalNodes; ++i) {
var strength = 255; //Math.random() * 100 + 155
output[i] = {
x: gaussianRand() * width,
y: gaussianRand() * height,
nodeSize: Math.round(Math.random() * sizeRange + minNodeSize),
/*colour: {
...baseColour
},*/
colour: {
r: strength,
g: strength,
b: strength
},
neighbour: i > 0 ? output[i - 1] : null
};
updateDistFromCentre(output[i]);
}
output[0].neighbour = output[totalNodes - 1];
return output;
};
var nodes = generateNodes();
var move = function move(node) {
var x = node.x,
y = node.y,
xFromC = node.xFromC,
yFromC = node.yFromC,
neighbour = node.neighbour;
if (insideVoid(x, y, xFromC, yFromC) || outsideBounds(x, y)) {
// reset node position
node.x = Math.random() * width;
node.y = Math.random() * height;
} else {
// move towards left neighbour at random speed
/*const moveX = Math.round((Math.random() * 2 - 1) * xSpeed)
const moveY = Math.round((Math.random() * 2 - 1) * ySpeed)
node.x += moveX + neighbour.x - x > 0 ? 1 : -1
node.y += moveY + neighbour.y - y > 0 ? 1 : -1*/
var dirX = node.x < neighbour.x ? 1 : -1;
var dirY = node.y < neighbour.y ? 1 : -1;
node.x += dirX * (Math.random() * xSpeed);
node.y += dirY * (Math.random() * ySpeed);
}
// update the distance from centres
updateDistFromCentre(node);
};
var fillPixel = function fillPixel(arrayPos, color) {
var p = arrayPos * 4;
var _imgData = imgData,
data = _imgData.data;
data[p] = color.r;
data[p + 1] = color.g;
data[p + 2] = color.b;
data[p + 3] = 200;
};
var paint = function paint(node) {
var x = Math.round(node.x);
var y = Math.round(node.y);
var colour = node.colour,
nodeSize = node.nodeSize;
var maxLeft = Math.min(x + nodeSize, width);
var maxTop = Math.min(y + nodeSize, height);
for (var left = x; left < maxLeft; ++left) {
for (var top = y; top < maxTop; ++top) {
fillPixel(left + top * width, colour);
}
}
};
var processNode = function processNode(node) {
// do movement
move(node);
// do painting
paint(node);
};
var clearCanvas = function clearCanvas() {
ctx.clearRect(0, 0, width, height);
//imgData.data.fill(0);
//imgData = ctx.createImageData(width, height);
imgData = ctx.getImageData(0, 0, width, height);
};
var animLoop = function animLoop() {
// clear the canvas
clearCanvas();
// single node work loop
for (var n = 0; n < totalNodes; ++n) {
processNode(nodes[n]);
}
// paint on the canvas
ctx.putImageData(imgData, 0, 0);
iter++;
if (play) {
requestAnimationFrame(animLoop);
}
};
// start the animation
requestAnimationFrame(animLoop);
})();