-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflowField8.mjs
More file actions
124 lines (108 loc) · 2.45 KB
/
flowField8.mjs
File metadata and controls
124 lines (108 loc) · 2.45 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
// https://www.youtube.com/watch?v=sZBfLgfsvSk&list=PLeCiJGCSl7jc5UWvIeyQAvmCNc47IuwkM&index=22
import ControlPanel, { Range, Select } from '../lib/ControlPanel/index.mjs'
export default function (p) {
const [w, h] = [500, 500]
const metadata = {
name: 'flowField8',
frameRate: 30,
}
const nPoints = 10000
const points = []
let patternIndex = 0
const patterns = [
['tan', 'tan'],
['tan', 'sin'],
['sin', 'tan'],
['tan', 'cos'],
]
const controlPanel = new ControlPanel({
p,
id: metadata.name,
controls: {
noiseScale: new Range({
name: 'noiseScale',
value: 0.0001,
min: 0.0001,
max: 1,
step: 0.0001,
}),
velocity: new Range({
name: 'velocity',
value: 0.01,
min: 0.01,
max: 10,
step: 0.01,
}),
particleAlpha: new Range({
name: 'particleAlpha',
value: 128,
min: 0,
max: 255,
}),
pattern: new Select({
name: 'pattern',
value: 'sin,cos',
options: [
'sin,cos',
'sin,sin',
'cos,sin',
'cos,cos',
'tan,tan',
'tan,sin',
'sin,tan',
'cos,tan',
'tan,cos',
],
}),
},
})
function setup() {
controlPanel.init()
const canvas = p.createCanvas(w, h)
p.colorMode(p.HSB, 100)
for (let i = 0; i < nPoints; i++) {
points.push(p.createVector(p.random(w), p.random(h)))
}
return {
canvas,
}
}
function draw() {
const {
noiseScale: ns,
velocity: vel,
particleAlpha,
} = controlPanel.values()
p.background(0, 5)
p.stroke(255, particleAlpha)
p.strokeWeight(1)
const [fnA, fnB] = patterns[patternIndex]
for (let i = 0; i < nPoints; i++) {
const point = points[i]
p.point(point.x, point.y)
const n = p[fnA](point.x * ns) + p[fnB](point.y * ns)
const a = p.TWO_PI * 6 * n
point.x += Math.cos(a) * vel
point.y += Math.sin(a) * vel
if (!onScreen(point)) {
point.x = p.random(w)
point.y = p.random(h)
}
}
if (p.frameCount % 300 === 0) {
p.noiseSeed(p.millis())
patternIndex = (patternIndex + 1) % patterns.length
}
}
function onScreen(v) {
return v.x >= 0 && v.x <= w && v.y >= 0 && v.y <= h
}
return {
setup,
draw,
destroy() {
controlPanel.destroy()
},
metadata,
}
}