-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_hydra.html
153 lines (132 loc) · 4.79 KB
/
example_hydra.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>three.js animation example</title>
<style>
body {
margin: 0;
}
#record {
position: fixed;
left: 2rem;
bottom: 2rem;
}
</style>
</head>
<body>
<button id="record" type="button" onclick="record()">record</button>
<!-- import ccapture.js (v1.1.0) from a CDN -->
<script src="https://unpkg.com/ccapture.js@1.1.0/build/CCapture.all.min.js"></script>
<!-- import hydra-synth from a CDN -->
<script src="https://unpkg.com/hydra-synth"></script>
<script type="module">
const fps = 60; //animation framerate
const duration = Math.PI; //animation duration (seconds)
//rendered video dimentions
const width = 1920;
const height = 1080;
let w, h;
let start, capturer, recording;
//set up hydra-synth
let canvas = document.createElement('canvas');
canvas.width = window.innerWidth / 2;
canvas.height = window.innerHeight / 2;
canvas.id = 'hydra-canvas';
document.body.appendChild(canvas);
const hydra = new Hydra({
canvas: canvas,
detectAudio: false,
enableStreamCapture: false,
makeGlobal: false,
autoLoop: false,
});
var lastTime = 0;
function animate() {
requestAnimationFrame(animate);
const now = (performance || Date).now();
if (start === undefined) start = now;
const elapsed = now - start;
const delta = now - lastTime;
{
//expose some hydra globals to your sketch (you may need to add to this list if a function or var you need is not included)
const {
bpm,
gradient,
height,
hush,
mouse,
noise,
o0,
o1,
o2,
o3,
osc,
prev,
render,
s0,
s1,
s2,
s3,
screencap,
setFunction,
setResolution,
shape,
solid,
speed,
src,
stats,
tick,
time,
update,
voronoi,
width,
} = hydra.synth;
setResolution(w, h);
//------------------- PASE YOUR HYDRA SKETCH HERE -------------------
solid(0, 0, 1, 1)
.add(shape(4).rotate(() => time % 360))
.out(o0);
//-------------------------------------------------------------------
}
hydra.tick(delta);
if (capturer) capturer.capture(canvas);
lastTime = now;
}
animate();
//set up ccapture.js with webm video format
capturer = new CCapture({
format: 'webm',
framerate: fps,
timeLimit: duration, //record exactly one loop
display: true,
});
//canvas sizing
function resize() {
if (recording) {
w = width;
h = height;
} else {
w = window.innerWidth;
h = window.innerHeight;
}
console.log({ w: Math.min(w, 1280), h: Math.min(h, 720) });
//hydra.synth.setResolution(Math.min(w, 1280), Math.min(h, 720));
}
resize();
let resizeTimeout = -1;
window.addEventListener('resize', () => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(resize, 200);
});
//record using ccapture.js when the record button is pressed
window.record = function () {
console.log('begin recording');
start = undefined; //reset loop timing
recording = true;
resize(); //resize scene to video dimentions
capturer.start();
};
</script>
</body>
</html>