-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrop2.mjs
More file actions
136 lines (122 loc) · 2.84 KB
/
drop2.mjs
File metadata and controls
136 lines (122 loc) · 2.84 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
// https://www.youtube.com/watch?v=p7IGZTjC008&t=613s
// https://people.csail.mit.edu/jaffer/Marbling/Dropping-Paint
import chroma from 'chroma-js'
import { createControlPanel } from '../lib/ControlPanel/index.mjs'
import { mapTimes, times } from '../util.mjs'
import AnimationHelper from '../lib/AnimationHelper.mjs'
import { Drop } from './drop.mjs'
/**
* @param {import('p5')} p
*/
export default function (p) {
const metadata = {
name: 'drop2',
frameRate: 30,
// WARNING! This is probably too big
// if recording video but perfect for images
pixelDensity: 6,
}
const [w, h] = [500, 500]
const center = p.createVector(w / 2, h / 2)
const drops = []
const colorScale = chroma.scale(['black', 'mistyrose', 'azure']).mode('lab')
const ah = new AnimationHelper({ p, frameRate: metadata.frameRate, bpm: 134 })
const cp = createControlPanel({
p,
id: metadata.name,
controls: [
{
type: 'Range',
name: 'resolution',
value: 100,
min: 3,
max: 1000,
},
{
type: 'Range',
name: 'radius',
value: 200,
min: 3,
max: 500,
},
{
type: 'Range',
name: 'tines',
value: 4,
min: 1,
max: 40,
step: 1,
},
{
type: 'Range',
name: 'displacement',
},
{
type: 'Range',
name: 'falloff',
},
],
})
function setup() {
cp.init()
const canvas = p.createCanvas(w, h)
p.colorMode(p.RGB, 255, 255, 255, 1)
for (let i = 0; i < 20; i++) {
const drop = new Drop(p, center.x, center.y, 20, cp.resolution)
drop.color = colorScale(p.random()).rgba()
drops.push(drop)
}
return {
canvas,
}
}
function draw() {
p.background(chroma.mix('black', colorScale(0)).rgba())
p.noStroke()
const tines = ah.animate({
keyframes: [1, cp.tines, 1],
duration: 8,
})
for (const [index, drop] of drops.entries()) {
drop.update({
radius: p.map(
(drops.length - index) * cp.radius,
0,
cp.radius * drops.length,
0,
cp.radius,
),
resolution: cp.resolution,
})
for (let i = 0; i < tines; i++) {
const x = i * (w / tines)
const line = p.createVector(x, center.y)
drop.tine(
line,
p.createVector(0, -ah.getPingPongLoopProgress(12)),
cp.displacement,
cp.falloff,
)
drop.tine(
line,
p.createVector(-1, ah.getPingPongLoopProgress(8)),
cp.displacement,
cp.falloff,
)
}
}
for (const drop of drops) {
p.noFill()
p.stroke(drop.color)
drop.display()
}
}
return {
setup,
draw,
destroy() {
cp.destroy()
},
metadata,
}
}