-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimateStudy.mjs
More file actions
154 lines (132 loc) · 3.6 KB
/
animateStudy.mjs
File metadata and controls
154 lines (132 loc) · 3.6 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
// @ts-check
import chroma from 'chroma-js'
import ControlPanel, { Range, Checkbox } from '../lib/ControlPanel/index.mjs'
import AnimationHelper from '../lib/AnimationHelper.mjs'
/**
* @param {import("p5")} p
*/
export default function (p) {
const metadata = {
name: 'animateStudy',
frameRate: 60,
}
const [w, h] = [500, 500]
const ah = new AnimationHelper({
p,
frameRate: metadata.frameRate,
bpm: 134,
latencyOffset: -24,
})
const colorScale = chroma.scale(['red', 'teal'])
const controlPanel = new ControlPanel({
p,
id: metadata.name,
attemptReload: true,
controls: {
amplitude: new Range({
name: 'amplitude',
value: 20,
min: 0,
max: 1000,
}),
animateAmplitude: new Checkbox({
name: 'animateAmplitude',
value: false,
}),
mixWeight: new Range({
name: 'mixWeight',
value: 0.5,
min: 0,
max: 1,
step: 0.01,
}),
backgroundAlpha: new Range({
name: 'backgroundAlpha',
value: 1,
min: 0,
max: 1,
step: 0.001,
}),
},
})
function setup() {
controlPanel.init()
const canvas = p.createCanvas(w, h)
p.colorMode(p.RGB, 255, 255, 255, 1)
p.textAlign(p.CENTER, p.CENTER)
p.textSize(16)
return {
canvas,
}
}
const diameter = w / 8
const count = 7
const space = w / count
function draw() {
const { amplitude, animateAmplitude, backgroundAlpha, mixWeight } =
controlPanel.values()
p.background(230, 230, 230, backgroundAlpha)
p.noStroke()
for (let i = 0, duration = 0.25; i < count; i++, duration += 0.25) {
const x = space / 2 + i * space
drawCircle({
x,
amplitude: animateAmplitude
? ah.animate({
keyframes: [10, amplitude + x / 4, 10],
duration: 8,
})
: amplitude,
chromaColor: colorScale(i / (count - 1)),
mixWeight,
animation: ah.animate({
keyframes: [0, 1],
// multiplying by 2 so it hits the tick on the up and down
// instead of just the down or up (depending on phaseOffset)
duration: duration * 2,
}),
})
p.fill(255)
p.text(`${i + 1}/16`, x, h / 2)
}
}
function drawCircle({ x, amplitude, chromaColor, mixWeight, animation }) {
// Start the sine wave at the bottom by adding a positive phase offset
// const phaseOffset = p.HALF_PI
const phaseOffset = 0
// Sine wave key points for reference:
// - At 0: starts at 0 ("0r"), rising up
// - At π/2: reaches peak at 1
// - At π: crosses 0 again ("0f"), falling down
// - At 3π/2: reaches bottom at -1
//
// With no phase offset (as explained above):
// (0r, 1, 0f, -1)
//
// Positive phase offset explanation (like rotating an array):
// Adding +π/2 shifts the sine wave to start from -1:
// (-1, 0r, 1, 0f)
//
// A negative phase offset would shift it backwards:
// (1, 0f, -1, 0r)
// Calculate the sine value based on the animation state
const sineValue = p.sin(animation * p.TWO_PI + phaseOffset)
// Calculate the interpolation factor (t) based on the sine value
const t = Math.abs(sineValue) * mixWeight
const interpolatedColor = chroma.mix(chromaColor, 'black', t, 'rgb').rgba()
p.fill(interpolatedColor)
p.circle(
x,
h / 2 + amplitude * p.sin(animation * p.TWO_PI + phaseOffset),
diameter,
)
}
return {
setup,
draw,
destroy() {
controlPanel.destroy()
},
metadata,
}
}