-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow.js
79 lines (61 loc) · 1.66 KB
/
flow.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
const SimplexNoise = require('simplex-noise')
const canvasSketch = require('canvas-sketch');
const simplex = new SimplexNoise()
const settings = {
dimensions: [ 1080, 1920 ],
fps: 36,
animate: true,
duration: 12
};
const sketch = ({width,height}) => {
const cols = 60,
rows = 107,
size = width / cols,
grid = []
class Dot {
constructor(row, col) {
this.x = col*size
this.y = row*size
this.originalX = this.x
this.originalY = this.y
this.vx = 0
this.vy = 0
this.random = Math.random()
this.size = size * this.random
this.gravity = 2
this.friction = 0.7
this.acceleration = 1.5
}
draw(context, playhead) {
this.move(playhead)
context.save()
context.fillStyle = "#fff"
context.beginPath()
context.arc(this.x + this.size/2, this.y + this.size/2, this.size/2, 0, Math.PI*2)
context.fill()
context.restore()
}
move(playhead) {
let angle = simplex.noise2D(this.x/1000, this.y/600, playhead)*Math.PI*2
this.vx += Math.cos(angle)*this.acceleration
this.vy += Math.sin(angle)*this.acceleration
this.vx *= this.friction
this.vy *= this.friction
this.x += this.vx
this.y += this.vy
}
}
for(let i = 0; i < rows; i++) {
for(let j = 0; j < cols; j++) {
grid.push(new Dot(i, j))
}
}
const render = ({ context, width, height, playhead }) => {
context.clearRect(0,0, width, height)
context.fillStyle = "#000"
context.fillRect(0,0, width, height)
grid.forEach(cell => cell.draw(context, playhead))
};
return render
}
canvasSketch(sketch, settings);