-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.source.js
More file actions
156 lines (134 loc) · 3.58 KB
/
canvas.source.js
File metadata and controls
156 lines (134 loc) · 3.58 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
(function(){
const 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)
let play = true,
iter = 0
c.width = width
c.height = height
let imgData = ctx.getImageData(0, 0, width, height)
const gaussianRand = () => {
var rand = 0
for (var i = 0; i < 2; i += 1) {
rand += Math.random()
}
return rand / 2
}
const insideVoid = (x, y, xFromC, yFromC) => {
return yFromC < voidSize && xFromC + y - yCentre < voidSize
}
const outsideBounds = (x, y) => {
return x < 0 || y < 0 || x >= width || y >= height
}
const updateDistFromCentre = node => {
node.xFromC = Math.abs(node.x - xCentre)
node.yFromC = Math.abs(node.y - yCentre)
}
const generateNodes = () => {
const sizeRange = maxNodeSize - minNodeSize
const output = new Array(totalNodes)
for(var i=0; i<totalNodes; ++i){
const 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
}
const nodes = generateNodes()
const move = node => {
const { x, y, xFromC, yFromC, neighbour } = node
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*/
const dirX = node.x < neighbour.x ? 1 : -1
const 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)
}
const fillPixel = (arrayPos, color) => {
const p = arrayPos * 4
const { data } = imgData
data[p] = color.r
data[p + 1] = color.g
data[p + 2] = color.b
data[p + 3] = 200
}
const paint = node => {
const x = Math.round(node.x)
const y = Math.round(node.y)
const { colour, nodeSize } = node
const maxLeft = Math.min(x + nodeSize, width)
const 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)
}
}
}
const processNode = node => {
// do movement
move(node)
// do painting
paint(node)
}
const clearCanvas = () => {
ctx.clearRect(0, 0, width, height);
//imgData.data.fill(0);
//imgData = ctx.createImageData(width, height);
imgData = ctx.getImageData(0, 0, width, height)
}
const 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)
})()