-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
353 lines (321 loc) · 12 KB
/
index.html
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
osc-css | creating css animations using oscillators
</title>
<link rel="stylesheet" href="./osc.css">
<link rel="stylesheet" href="./assets/demo.css">
<link rel="canonical" href="https://osc.style">
<script defer data-domain="osc.style" src="https://plausible.io/js/script.js"></script>
<script src="./assets/demo.js" type="module"></script>
<script type="module">
// turn every headline into a link
['h2', 'h3', 'h4', 'h5', 'h6'].forEach(tag => {
document.querySelectorAll(tag).forEach(h => {
const id = h.textContent.toLowerCase().replace(/[^a-z]/g, '-')
h.id = id
h.innerHTML = `<a href="#${id}">${h.innerHTML}</a>`
})
})
</script>
</head>
<body>
<header>
<h1>
<span>OSC</span> <span>.</span> <span>CSS</span>
</h1>
<p>
CSS properties to animate numeric values where they are needed using oscillators and envelopes.
Source available on <a href="http://github.com/nocksock/osc-css">GitHub</a>
</p>
</header>
<main>
<article>
<h3>Basics</h3>
<p>
An oscillator is a function that returns a value between -1 and 1 over time.
Or in different words: it oscillates between -1 and 1.
You are likely already familar with the most common oscillator: <code>sin</code>.
</p>
<p>
While oscillators are usually associated with audio synthesis, they are also great for animations.
Combining oscillators with envelopes can create complex and organic looking animations with very few lines of CSS.
</p>
<p>
Another benefit is, that instead of defining a several keyframes manually, you can use an oscillator to animate any numeric value right in place:
</p>
<figure id="basic">
<css-editor>
<style contenteditable="plaintext-only" spellcheck="false">
#basic osc-ball {
animation: var(--osc);
translate: 0 calc(var(--osc-sin) * 100px);
}
</style>
</css-editor>
<osc-stage class="ch-x">
<osc-ball></osc-ball>
</osc-stage>
</figure>
</article>
<article>
<h3>Different kinds of oscillators</h3>
<p>
There are 4 oscillators: <code>sin</code>, <code>cos</code>, <code>saw</code> and <code>tri</code>.
And there are two versions of each:
</p>
<ul class="numbered">
<li><code>-1...1</code>: through zero are written in lower case
<li><code> 0...1</code>: normalised are written in upper case
</ul>
<figure id="compare" class="nocode">
<style>
#compare {
osc-stage {
display: flex;
align-items: stretch;
justify-content: space-around;
padding: 5%;
font-family: var(--font-mono);
z-index: -2;
box-sizing: border-box;
container: stage / size;
}
osc-lane {
flex-direction: column;
font-size: 6cqb;
animation: var(--osc);
z-index: 1;
}
}
</style>
<osc-stage>
<osc-lane style="--use-osc: var(--osc-sin)">sin</osc-lane>
<osc-lane style="--use-osc: var(--osc-cos)">cos</osc-lane>
<osc-lane style="--use-osc: var(--osc-saw)">saw</osc-lane>
<osc-lane style="--use-osc: var(--osc-tri)">tri</osc-lane>
<osc-lane style="--use-osc: var(--osc-SIN)">SIN</osc-lane>
<osc-lane style="--use-osc: var(--osc-COS)">COS</osc-lane>
<osc-lane style="--use-osc: var(--osc-SAW)">SAW</osc-lane>
<osc-lane style="--use-osc: var(--osc-TRI)">TRI</osc-lane>
</osc-stage>
</figure>
</article>
<article>
<h3>Using an envelope</h3>
<p>
Envelopes are used to control the amplitude of an oscillator.
In audio synthesis, one common use is to control the volume of a sound.
Here we can use it to control the amplitude of an animation.
</p>
<p>
The builtin envelope is a so called
<abbr title="delay attack hold decay">DAHD</abbr>
envelope generator.
It has 4 stages:
</p>
<ul class="numbered">
<li><code>delay</code>: time before the attack starts
<li><code>attack</code>: time to reach full amplitude
<li><code>hold</code>: time to hold full amplitude
<li><code>decay</code>: time to reach zero amplitude
</ul>
<p>
Note how a simple setup with 2 oscillators, the envelope and a single element can already create a complex animation.
</p>
<figure id="envelope">
<css-editor>
<style contenteditable="plaintext-only" spellcheck="false">
#envelope osc-ball {
animation: var(--osc);
translate:
calc(var(--osc-cos) * 100px * var(--env-amp))
calc(var(--osc-tri) * 280px)
;
}
</style>
</css-editor>
<osc-stage class="ch">
<osc-ball></osc-ball>
</osc-stage>
</figure>
</article>
<article>
<h3>Visualising the waveform</h3>
<p>
Useful to visualise a calculation.
But since ther are many elements, this gets rather heavy on the browser.
This site uses a Web Component with <a href="./assets/demo.js">IntersectionObserver</a> to set <code>--osc-state</code> to <code>running</code> when the element is in view.
</p>
<p>
Adjust the different parameters to see how the waveform changes.
Envelope parameters are durations in seconds, frequency is in Hz.
</p>
<figure id="waveform">
<style>
#waveform osc-stage{
display: flex;
align-items: center;
justify-content: space-around;
padding: 5%;
}
</style>
<css-editor>
<style contenteditable="plaintext-only" spellcheck="false">
#waveform osc-dot {
--osc-frequency: 2;
--env-delay: 1;
--env-attack: 1;
--env-hold: 1;
--env-decay: 1;
translate: 0 calc(var(--osc-sin) * 40px * var(--env-amp));
animation: var(--osc);
}
</style>
</css-editor>
<osc-stage class="ch-x">
<script type="module">
for (let i = 0; i < 75; i++) {
const dot = document.createElement('osc-dot')
dot.style.setProperty('--osc-phase', `${i / 20}s`)
document.querySelector('#waveform osc-stage').append(dot)
}
</script>
</osc-stage>
</figure>
</article>
<article id="creative">
<h3>Get creative!</h3>
<p>
So far we have only used the <code>translate</code> property to animate an element.
But you can use any property that accepts a numeric value!
And you can use multiple oscillators and envelopes in a single animation, and of course nest them.
</p>
<p>
And don't forget all the math functions that CSS supports!
Like <code>pow</code>, <code>sqrt</code>, <code>log</code>, <code>round</code> and many more.
</p>
<p>
As usual MDN has a list of all <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Values_and_Units/Using_CSS_math_functions#advanced_css_math_functions">advanced CSS math functions</a>.
Make sure to check them out, don't worry about understanding all the math; just try things out!
This approach is prone to happy accidents.
</p>
<figure id="creative-01">
<figcaption>
<p>
Here I threw <code>var(--env-amp)</code> into <code>log()</code> not sure what it would do.
But I like what it does!
</p>
</figcaption>
<css-editor>
<style>
#creative-01 osc-dot {
animation: var(--osc);
--osc-frequency: 0.5;
--osc-sin-frequency: 1;
border-radius: 0;
transform: skew(calc(var(--osc-sin) * 100deg) calc(var(--osc-cos) * 20deg));
translate:
calc(var(--osc-sin) * 50px * var(--env-amp))
calc(var(--osc-cos) * 50px * log(var(--env-amp)))
;
rotate: calc(var(--osc-saw) * 360deg);
scale: calc(var(--osc-tri) * 10);
}
</style>
</css-editor>
<osc-stage create="osc-dot" count="8" phased>
</osc-stage>
</figure>
<figure id="create-02">
<figcaption>
<p>
Combining <code>min()</code> and <code>--osc-sin</code>? Sure, why not!
Looks like a ball doing a stomp attack to me.
</p>
</figcaption>
<css-editor>
<style contenteditable="plaintext-only" spellcheck="false">
#create-02 osc-ball {
animation: var(--osc);
translate: 0 calc(min(var(--osc-sin), var(--osc-saw)) * -150px);
scale:
calc(2 + var(--osc-tri) * 20)
calc(2 + var(--osc-sin) * 10)
;
}
</style>
</css-editor>
<osc-stage>
<osc-ball></osc-ball>
</osc-stage>
</figure>
<figure id="creative-03">
<figcaption>
Don't forget about colors and gradients and make trippy visuals for your next big gig.
</figcaption>
<style>
#creative-03 osc-stage {
animation: var(--osc);
/* incorporating primes to repeat less often */
--osc-sin-frequency: calc(1/30);
--osc-saw-frequency: calc(1/50);
--osc-cos-frequency: calc(1/70);
/* change blend for other interesting aesthetics */
background-blend-mode: screen;
/* using fractions to make it easier to control */
background-size:
calc(100%/1) calc(100%/8),
calc(100%/2) calc(100%/2),
calc(100%/1) calc(100%/1)
;
filter: hue-rotate(calc(var(--osc-saw) * 360deg));
background-image:
repeating-conic-gradient(
from calc(var(--osc-saw) * 90deg),
red 30deg,
green 60deg,
blue 90deg
),
repeating-conic-gradient(
from calc(var(--osc-cos) * 180deg),
red 60deg,
green 120deg,
blue 180deg
),
repeating-conic-gradient(
from calc(var(--osc-sin) * 360deg),
red 90deg,
green 180deg,
blue 270deg
)
;
}
</style>
<osc-stage>
</osc-stage>
</figure>
</article>
<article>
<h3>More examples</h3>
<p>
More examples are added over the next weeks and months.
Meanwhile, there are a few more in <a href="https://codepen.io/collection/OLrGvm">this codepen collection</a>.
</p>
<p>
If you create something cool with it, make sure to tag it with <code>osc-css</code> or share it with me on <a href="https://bsky.app/profile/nocksock.dev">bsky</a>.
</p>
</article>
</main>
<footer>
<img src="https://bleepbloop.studio/public/logo.svg" alt="" />
<p>
made by <a href="https://bleepbloop.studio">Nils Riedemann</a>
</p>
</footer>
<script type="module">
</script>
</body>
</html>