-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
415 lines (375 loc) · 12.5 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="cache-control" content="no-cache" />
<title>kampos | Tiny and fast effects compositor on WebGL</title>
<link
href="https://fonts.googleapis.com/css?family=Nova+Round&display=swap"
rel="stylesheet"
/>
<script async defer src="https://buttons.github.io/buttons.js"></script>
<style>
body {
min-height: 100vh;
margin: 0;
background-image: linear-gradient(
-45deg,
peachpuff,
palegreen,
plum,
peachpuff,
palegreen,
plum
),
linear-gradient(
45deg,
purple,
darkblue,
rebeccapurple,
purple,
darkblue,
rebeccapurple
),
url('./kampos.svg');
background-blend-mode: screen;
background-repeat: no-repeat;
background-position: center;
font-family: 'Nova round', cursive, fantasy;
font-size: 16px;
color: darkslateblue;
}
nav > ul {
display: flex;
width: 100vw;
margin: 0;
padding: 0;
list-style: none;
}
nav li {
flex: 1 auto;
padding: 1rem 0.5rem;
}
nav li.nav-items {
font-size: 16px;
font-weight: bold;
}
nav a,
nav a:visited {
color: darkslateblue;
}
nav a:hover {
color: darkslateblue;
}
main {
margin: 30vh auto 0;
text-align: center;
}
h1,
h2 {
color: #bada;
}
h1 {
font-size: 96px;
margin: 0;
text-shadow: 0 6px 10px darkslateblue;
}
h2 {
font-size: 28px;
margin: 0;
text-shadow: 0 4px 4px darkslateblue;
}
header > span {
position: absolute;
top: 50px;
right: 50px;
}
.target-canvas {
position: fixed;
top: 0;
left: 0;
z-index: -2;
pointer-events: none;
}
#target2 {
left: 50%;
}
#target3 {
position: fixed;
top: 10vh;
left: 5vw;
z-index: -1;
pointer-events: none;
}
#target4 {
position: fixed;
bottom: 10vh;
right: 5vw;
z-index: -1;
pointer-events: none;
}
#video,
#video2 {
position: fixed;
top: 0;
content-visibility: hidden;
}
@media (min-width: 641px) {
html,
body {
margin: 0;
}
main {
width: -moz-fit-content;
width: fit-content;
}
h1 {
font-size: 128px;
}
h2 {
font-size: 32px;
}
nav > ul {
width: -moz-fit-content;
width: fit-content;
margin: 0 auto;
}
nav li.nav-items {
font-size: 20px;
padding: 1rem 2rem;
}
}
</style>
</head>
<body>
<header>
<nav>
<ul>
<li class="nav-items">
<a href="./docs/">API Reference</a>
</li>
<li class="nav-items">
<a href="./demo/">Demos</a>
</li>
<li class="nav-items">
<a href="https://github.com/wix-incubator/kampos"
>Source</a
>
</li>
<li>
<a
class="github-button"
href="https://github.com/wix-incubator/kampos"
data-size="large"
aria-label="Star wix/kampos on GitHub"
>Star</a
>
</li>
</ul>
</nav>
</header>
<main>
<canvas class="target-canvas" id="target"></canvas>
<canvas class="target-canvas" id="target2"></canvas>
<canvas id="target3"></canvas>
<canvas id="target4"></canvas>
<h1>kampos</h1>
<h2>Tiny and fast effects compositor</h2>
<video
id="video"
src="https://video.wixstatic.com/video/11062b_6ceb80744f45401caf9eb666caeb9887/480p/mp4/file.mp4"
loop
muted
preload="auto"
crossorigin="anonymous"
playsinline
autoplay
></video>
<video
id="video2"
src="./demo/starry-night.mp4"
loop
muted
preload="auto"
crossorigin="anonymous"
playsinline
autoplay
></video>
</main>
</body>
<script type="module">
import { Kampos, effects, noise } from './index.js';
const target = document.querySelector('#target');
const targetCtx = target.getContext('bitmaprenderer');
const target2 = document.querySelector('#target2');
const target2Ctx = target2.getContext('bitmaprenderer');
const target3 = document.querySelector('#target3');
const target3Ctx = target3.getContext('bitmaprenderer');
const target4 = document.querySelector('#target4');
const target4Ctx = target4.getContext('bitmaprenderer');
const body = document.body;
const width = body.clientWidth / 2;
const height = body.clientHeight;
target.width = width;
target.height = height;
target2.width = width;
target2.height = height;
const offscreen = new OffscreenCanvas(width, height);
const mousePosition = [0.5, 0.5];
const mousePosition2 = [0.5, 0.5];
const turbulence = effects.turbulence({
noise: noise.simplex,
output: '\n',
});
const getRender = (c, d, mouse) => ({
fragment: {
uniform: {
u_resolution: 'vec2',
u_pointer: 'vec2',
},
constant: `
vec3 A = vec3(0.5);
vec3 B = vec3(0.5);
vec3 C = vec3(${c});
vec3 D = vec3(${d});
vec3 palette(float t, vec3 a, vec3 b, vec3 c, vec3 d) {
return a + b * cos(6.28318 * (c * t + d));
}`,
main: `
vec2 st = gl_FragCoord.xy / u_resolution.xy;
float dist = distance(u_pointer, st);
alpha = smoothstep(turbulenceValue * 0.2, 0.2 + turbulenceValue * 0.2, dist);
color = palette(turbulenceValue, A, B, C, D);`,
},
uniforms: [
{
name: 'u_resolution',
type: 'f',
data: [width, height],
},
{
name: 'u_pointer',
type: 'f',
data: mouse,
},
],
get mouse() {
return this.uniforms[1].data;
},
set mouse(data) {
this.uniforms[1].data[0] = data[0];
this.uniforms[1].data[1] = data[1];
},
});
const render = getRender(
[1.0, 0.7, 0.4],
[0.0, 0.15, 0.2],
mousePosition,
);
const render2 = getRender(
[0.0, 0.9, 0.6],
[0.6, 0.15, 0.5],
mousePosition2,
);
const FREQUENCY = 2 / (width > height ? width : height);
turbulence.frequency = { x: FREQUENCY, y: FREQUENCY };
turbulence.octaves = 6;
const instance = new Kampos({
target: offscreen,
effects: [turbulence, render],
noSource: true,
});
const instance2 = new Kampos({
target: offscreen,
effects: [turbulence, render2],
noSource: true,
});
const source = { width, height };
instance.play(
(time) => {
instance.setSource(source, true);
render.mouse = mousePosition;
turbulence.time = time * 2;
},
() => {
const bitmap = offscreen.transferToImageBitmap();
targetCtx.transferFromImageBitmap(bitmap);
},
);
instance2.play(
(time) => {
instance.setSource(source, true);
render2.mouse = mousePosition2;
turbulence.time = time * 3;
},
() => {
const bitmap = offscreen.transferToImageBitmap();
target2Ctx.transferFromImageBitmap(bitmap);
},
);
document.body.addEventListener('mousemove', (e) => {
mousePosition[0] = e.clientX / width;
mousePosition2[0] = (e.clientX - width) / width;
mousePosition[1] = (height - e.clientY) / height;
mousePosition2[1] = (height - e.clientY) / height;
});
const video = document.querySelector('#video');
video.addEventListener(
'timeupdate',
() => {
const duotone = effects.duotone();
const instance3 = new Kampos({
target: offscreen,
effects: [duotone],
});
const width = video.videoWidth;
const height = video.videoHeight;
const source = { media: video, width, height };
target3.width = width;
target3.height = height;
instance3.setSource(source);
instance3.play(
(time) => {
instance3.setSource(source, true);
},
() => {
const bitmap = offscreen.transferToImageBitmap();
target3Ctx.transferFromImageBitmap(bitmap);
},
);
},
{ once: true },
);
const video2 = document.querySelector('#video2');
video2.addEventListener(
'timeupdate',
() => {
const duotone = effects.duotone({
light: [1, 1, 1, 1],
dark: [0, 0, 0, 1],
});
const instance4 = new Kampos({
target: offscreen,
effects: [duotone],
});
const width = video2.videoWidth;
const height = video2.videoHeight;
const source = { media: video2, width, height };
target4.width = width;
target4.height = height;
instance4.setSource(source);
instance4.play(
(time) => {
instance4.setSource(source, true);
},
() => {
const bitmap = offscreen.transferToImageBitmap();
target4Ctx.transferFromImageBitmap(bitmap);
},
);
},
{ once: true },
);
</script>
</html>