-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
336 lines (259 loc) · 9.33 KB
/
main.js
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
var FrameExporter = function() {
this.player = document.getElementById('player');
this.demogl = document.getElementById('demogl');
this.wrapper = this.player.parentNode;
this.createUi();
};
FrameExporter.prototype.enablePreview = function() {
this.preview = true;
this.frameUpdated = true;
// Start frame counter
this.frameCounter = new FrameCounter(this.fpsInput.value, this.secondsInput.value);
this.frameCounter.start();
this.startPatch();
// Seek to the beginning
gShaderToy.resetTime();
gShaderToy.mTo = 0;
};
FrameExporter.prototype.disablePreview = function() {
this.preview = false;
this.stopPatch();
};
FrameExporter.prototype.startRecording = function() {
this.record = true;
this.frameUpdated = true;
// Start frame counter
this.frameCounter = new FrameCounter(this.fpsInput.value, this.secondsInput.value);
this.frameCounter.start();
this.startPatch();
// Start playback if it's currently paused
this.original_mIsPaused = gShaderToy.mIsPaused;
if (gShaderToy.mIsPaused) {
gShaderToy.pauseTime();
}
// Seek to the beginning
gShaderToy.resetTime();
gShaderToy.mTo = 0;
// Store current settings
this.prefix = this.prefixInput.value;
};
FrameExporter.prototype.stopRecording = function() {
this.record = false;
if ( ! this.preview) {
this.stopPatch();
}
// Pause playback if it was originally paused
if (this.original_mIsPaused) {
gShaderToy.pauseTime();
}
};
FrameExporter.prototype.startPatch = function() {
if (this.patched) {
return;
}
this.patched = true;
// Add canvas layout styles
this.addClass(this.wrapper, 'sfe-recording');
// Resize canvas to desired size
var width = parseInt(this.widthInput.value, 10);
var height = parseInt(this.heightInput.value, 10);
this.demogl.style.width = width + 'px';
this.demogl.style.height = height + 'px';
// Patch the time counter, so we can step through frames
this.original_getRealTime = window.getRealTime;
window.getRealTime = this.getRealTime.bind(this);
// Patch raf-loop, so we can intercept renders
this.original_RequestAnimationFrame = gShaderToy.mEffect.RequestAnimationFrame;
gShaderToy.mEffect.RequestAnimationFrame = this.RequestAnimationFrame.bind(this);
};
FrameExporter.prototype.stopPatch = function() {
if ( ! this.patched) {
return;
}
this.patched = false;
// Remove canvas layout styles
this.removeClass(this.wrapper, 'sfe-recording');
// Reset canvas to original size
this.demogl.style.width = null;
this.demogl.style.height = null;
// Remove time counter patch
window.getRealTime = this.original_getRealTime;
// Remove raf-loop patch
gShaderToy.mEffect.RequestAnimationFrame = this.original_RequestAnimationFrame;
};
/* Shadertoy patches
========================================================================== */
// Will be called for every animation frame, but we don't always want to draw.
// When previewing, only draw when we reach a new frame to simulate reduced
// frame rate.
// When recording, only increment the frame and draw, after each save is complete.
FrameExporter.prototype.render = function(original_render) {
if ( ! this.patched) {
original_render();
return;
}
if (this.preview) {
if (this.frameUpdated) {
original_render();
} else {
this.RequestAnimationFrame(original_render);
}
var lastFrame = this.frameCounter.frameNumber;
this.frameCounter.updateTime();
this.frameUpdated = lastFrame !== this.frameCounter.frameNumber;
}
if (this.record) {
if (this.frameUpdated) {
this.frameUpdated = false;
original_render();
this.saveFrame(gShaderToy.mCanvas, function() {
this.frameCounter.incrementFrame();
if (this.frameCounter.looped) {
this.stopRecording();
}
this.frameUpdated = true;
}.bind(this));
} else {
this.RequestAnimationFrame(original_render);
}
}
};
// Inject our own render function
FrameExporter.prototype.RequestAnimationFrame = function(original_render) {
var render = this.render.bind(this, original_render);
this.original_RequestAnimationFrame.call(gShaderToy.mEffect, render);
};
FrameExporter.prototype.getRealTime = function() {
return this.frameCounter.milliseconds();
};
/* Frame Counter
========================================================================== */
var FrameCounter = function(fps, loopSeconds) {
this.fps = parseFloat(fps);
this.loopSeconds = parseFloat(loopSeconds);
this.frameLength = 1 / this.fps;
this.totalFrames = Math.floor(this.fps * this.loopSeconds);
};
FrameCounter.prototype.start = function() {
this.startTime = performance.now();
this.looped = false;
this.frameNumber = 0;
};
FrameCounter.prototype.updateTime = function() {
var timeSeconds = (performance.now() - this.startTime) / 1000;
this.frameNumber = Math.floor(timeSeconds / this.frameLength);
this.loopFrames();
};
FrameCounter.prototype.incrementFrame = function() {
this.frameNumber += 1;
this.loopFrames();
};
FrameCounter.prototype.loopFrames = function() {
if (this.frameNumber > this.totalFrames - 1) {
this.looped = true;
this.startTime = performance.now();
this.frameNumber = 0;
}
};
FrameCounter.prototype.milliseconds = function() {
return (this.frameNumber * this.frameLength) * 1000;
};
/* User interface
========================================================================== */
FrameExporter.prototype.createUi = function() {
this.controls = document.createElement('div');
this.addClass(this.controls, 'sfe-controls');
this.insertAfter(this.controls, this.player);
this.widthInput = this.createInput('width', 'number', 500);
this.heightInput = this.createInput('height', 'number', 500);
this.fpsInput = this.createInput('fps', 'number', 30);
this.secondsInput = this.createInput('seconds', 'number', 1);
this.prefixInput = this.createInput('prefix', 'text', 'img');
var previewInput = this.createInput('preview', 'checkbox');
previewInput.addEventListener('click', function() {
if (previewInput.checked) {
this.enablePreview();
} else {
this.disablePreview();
}
}.bind(this));
var button = document.createElement('button');
button.textContent = 'Save frames';
this.addClass(button, 'sfe-save');
this.controls.appendChild(button);
button.addEventListener('click', this.startRecording.bind(this));
this.settingsChanged();
};
FrameExporter.prototype.settingsChanged = function() {
var settings = {
width: this.widthInput.value,
height: this.heightInput.value,
fps: this.fpsInput.value,
seconds: this.secondsInput.value
};
if (this.preview && JSON.stringify(this.settings) != JSON.stringify(settings)) {
this.disablePreview();
this.enablePreview();
}
this.settings = settings;
};
FrameExporter.prototype.createInput = function(name, type, value) {
var id = name;
var label = document.createElement('label');
label.textContent = name;
label.setAttribute('for', id);
this.addClass(label, 'sfe-label');
var input = document.createElement('input');
input.id = id;
input.type = type;
input.value = value;
this.addClass(input, 'sfe-input');
input.addEventListener('change', this.settingsChanged.bind(this));
input.addEventListener('blur', this.settingsChanged.bind(this));
var control = document.createElement('div');
this.addClass(control, 'sfe-control');
this.addClass(control, 'sfe-control--' + type);
control.appendChild(label);
control.appendChild(input);
this.controls.appendChild(control);
return input;
};
/* Utilities
========================================================================== */
FrameExporter.prototype.saveFrame = function(canvas, done) {
var totalFrames = this.frameCounter.totalFrames;
var digits = totalFrames.toString().length;
var frameString = this.pad(this.frameCounter.frameNumber, digits);
var filename = this.prefix + frameString + '.png';
canvas.toBlob(function(blob) {
saveAs(blob, filename);
setTimeout(done, 100);
});
};
FrameExporter.prototype.insertAfter = function(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
};
FrameExporter.prototype.addClass = function(el, className) {
if (el.classList) {
el.classList.add(className);
} else {
el.className += ' ' + className;
}
};
FrameExporter.prototype.removeClass = function(el, className) {
if (el.classList) {
el.classList.remove(className);
} else {
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
};
FrameExporter.prototype.pad = function(number, length) {
var str = '' + number;
while (str.length < length) {
str = '0' + str;
}
return str;
};
/* Init
========================================================================== */
window.frameExporter = new FrameExporter();