Skip to content

Commit e9293be

Browse files
committed
add example_hydra
1 parent 75edd26 commit e9293be

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

example_hydra.html

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>three.js animation example</title>
6+
<style>
7+
body {
8+
margin: 0;
9+
}
10+
#record {
11+
position: fixed;
12+
left: 2rem;
13+
bottom: 2rem;
14+
}
15+
</style>
16+
</head>
17+
<body>
18+
<button id="record" type="button" onclick="record()">record</button>
19+
<!-- import ccapture.js (v1.1.0) from a CDN -->
20+
<script src="https://unpkg.com/[email protected]/build/CCapture.all.min.js"></script>
21+
<!-- import hydra-synth from a CDN -->
22+
<script src="https://unpkg.com/hydra-synth"></script>
23+
<script type="module">
24+
const fps = 60; //animation framerate
25+
const duration = Math.PI; //animation duration (seconds)
26+
27+
//rendered video dimentions
28+
const width = 1920;
29+
const height = 1080;
30+
31+
let w, h;
32+
33+
let start, capturer, recording;
34+
35+
//set up hydra-synth
36+
let canvas = document.createElement('canvas');
37+
canvas.width = window.innerWidth / 2;
38+
canvas.height = window.innerHeight / 2;
39+
canvas.id = 'hydra-canvas';
40+
document.body.appendChild(canvas);
41+
42+
const hydra = new Hydra({
43+
canvas: canvas,
44+
detectAudio: false,
45+
enableStreamCapture: false,
46+
makeGlobal: false,
47+
autoLoop: false,
48+
});
49+
50+
var lastTime = 0;
51+
function animate() {
52+
requestAnimationFrame(animate);
53+
54+
const now = (performance || Date).now();
55+
if (start === undefined) start = now;
56+
const elapsed = now - start;
57+
const delta = now - lastTime;
58+
59+
//create our own time variable
60+
//const time = elapsed / 1000;
61+
62+
{
63+
//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)
64+
const {
65+
bpm,
66+
gradient,
67+
height,
68+
hush,
69+
mouse,
70+
noise,
71+
o0,
72+
o1,
73+
o2,
74+
o3,
75+
osc,
76+
prev,
77+
render,
78+
s0,
79+
s1,
80+
s2,
81+
s3,
82+
screencap,
83+
setFunction,
84+
setResolution,
85+
shape,
86+
solid,
87+
speed,
88+
src,
89+
stats,
90+
tick,
91+
time,
92+
update,
93+
voronoi,
94+
width,
95+
} = hydra.synth;
96+
97+
setResolution(w, h);
98+
99+
//------------------- PASE YOUR HYDRA SKETCH HERE -------------------
100+
101+
solid(0, 0, 1, 1)
102+
.add(shape(4).rotate(() => time % 360))
103+
.out(o0);
104+
105+
//-------------------------------------------------------------------
106+
}
107+
108+
hydra.tick(delta);
109+
110+
if (capturer) capturer.capture(canvas);
111+
112+
lastTime = now;
113+
}
114+
animate();
115+
116+
//set up ccapture.js with webm video format
117+
capturer = new CCapture({
118+
format: 'webm',
119+
framerate: fps,
120+
timeLimit: duration, //record exactly one loop
121+
display: true,
122+
});
123+
124+
//canvas sizing
125+
function resize() {
126+
if (recording) {
127+
w = width;
128+
h = height;
129+
} else {
130+
w = window.innerWidth;
131+
h = window.innerHeight;
132+
}
133+
134+
console.log({ w: Math.min(w, 1280), h: Math.min(h, 720) });
135+
//hydra.synth.setResolution(Math.min(w, 1280), Math.min(h, 720));
136+
}
137+
resize();
138+
139+
let resizeTimeout = -1;
140+
window.addEventListener('resize', () => {
141+
clearTimeout(resizeTimeout);
142+
resizeTimeout = setTimeout(resize, 200);
143+
});
144+
145+
//record using ccapture.js when the record button is pressed
146+
window.record = function () {
147+
console.log('begin recording');
148+
149+
start = undefined; //reset loop timing
150+
recording = true;
151+
resize(); //resize scene to video dimentions
152+
capturer.start();
153+
};
154+
</script>
155+
</body>
156+
</html>

media/example_hydra.gif

347 KB
Loading

media/example_hydra.webm

1.36 MB
Binary file not shown.

0 commit comments

Comments
 (0)