-
Notifications
You must be signed in to change notification settings - Fork 5
/
editor.js
452 lines (369 loc) · 10.8 KB
/
editor.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
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* eslint-env browser */
// @ts-ignore
import CodeMirror from "codemirror";
import * as Y from "yjs";
import { WebsocketProvider } from "y-websocket";
//import { WebrtcProvider } from 'y-webrtc'
import { CodemirrorBinding } from "y-codemirror";
import "codemirror/mode/clike/clike.js";
import 'codemirror/addon/lint/lint';
import {_fragmentShaderC, _vertexShaderC} from "./defaultShaders.js";
// Element storage
var gl;
var editor;
let glCanvas = null;
let _fragmentShader = _fragmentShaderC;
// Current state storage
var isDirty = false;
let shaderProgram;
// Aspect ratio and coordinate system
// details
let aspectRatio;
let resolution;
// Vertex information
let vertexArray;
let vertexBuffer;
let vertexNumComponents;
let vertexCount;
// Rendering data shared with the
// scalers.
let uResolution;
let uTime;
let uVol;
let aVertexPosition;
// Animation timing
let previousTime = 0.0;
// this script is from cut-ruby.glitch.me
// so good
var FFT_SIZE = 512;
var vol;
if (window.isProduction && window.location.protocol !== "https:") {
window.location = "https://" + window.location.hostname;
}
class Camera {
constructor() {
this.video = document.createElement("video");
this.video.setAttribute("muted", true);
this.video.setAttribute("playsinline", false);
this.selfie = false;
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}
_startCapture() {
return navigator.mediaDevices
.getUserMedia({
audio: true,
video: false,
})
.then((stream) => {
this.stream = stream;
var source = this.audioCtx.createMediaStreamSource(stream);
this.analyser = this.audioCtx.createAnalyser();
this.analyser.smoothingTimeConstant = 0.9;
this.analyser.fftSize = FFT_SIZE;
source.connect(this.analyser);
});
}
init() {
this._startCapture();
return this._startCapture();
}
flip() {
this.selfie = !this.selfie;
this._startCapture();
}
}
let button = document.querySelector("button");
let camera;// new Camera();
//document.querySelector("body").appendChild(camera.video);
// document.addEventListener("click", function (e) {
// camera
// .init()
// .then(start)
// .catch((e) => console.error(e));
// });
// (function () {
// camera
// .init()
// .then(start)
// .catch((e) => console.error(e));
// })();
function start() {}
// from here https://hackernoon.com/creative-coding-using-the-microphone-to-make-sound-reactive-art-part1-164fd3d972f3
// A more accurate way to get overall volume
function getRMS(spectrum) {
var rms = 0;
for (var i = 0; i < spectrum.length; i++) {
rms += spectrum[i] * spectrum[i];
}
rms /= spectrum.length;
rms = Math.sqrt(rms);
let norm = rms / 128;
return (norm - 0.99) * 100;
}
function isInPresentationMode() {
if (window.location.pathname.split('/').pop() == 'present.html') {
return true;
}
return false;
}
function isLockedPresent(){
const queryString = window.location.search;
console.log(queryString);
const urlParams = new URLSearchParams(queryString);
const pw = urlParams.get('pw')
const room = urlParams.get('room')
console.log(room)
// if you hack into my classroom I will cry in front of everyone :,(
if(pw != "WhyDoesDotEnvEvadeMe" && room.toLowerCase()== "classroom"){
return true;
console.log("youre a student, in student mode")
}
return false;
}
function addCodeMirrorPresentModifier() {
const codeMirrorDiv = document.querySelector(".CodeMirror");
if (codeMirrorDiv) {
codeMirrorDiv.classList.add("CodeMirror-present");
}
}
function addCodeMirrorEditorModifier() {
const codeMirrorDiv = document.querySelector(".CodeMirror");
if (codeMirrorDiv) codeMirrorDiv.classList.add("CodeMirror-editor");
}
function initYdoc() {
const ydoc = new Y.Doc();
const searchParams = new URLSearchParams(window.location.search);
var room = "";
if (searchParams.has("room")){
room = searchParams.get("room");
}
const provider = new WebsocketProvider(
`ws${location.protocol.slice(4)}//${location.host}/ws`,
room,
ydoc
);
// const provider = new WebsocketProvider('wss://localhost:8080', room, ydoc, { WebSocketPolyfill: require('ws') })
var editorContainer = document.getElementById("editor");
editor = CodeMirror(editorContainer, {
value: _fragmentShader,
lineNumbers: true,
mode: "x-shader/x-vertex",
gutters: ["CodeMirror-lint-markers"],
lint: true,
lineWrapping: !isInPresentationMode(),
readOnly: isLockedPresent()
//editable: false //!isLockedPresent()
});
const ytext = ydoc.getText("codemirror");
// const undoManager = new Y.UndoManager(ytext, { trackedOrigins: new Set([ydoc.clientID]) })
const binding = new CodemirrorBinding(ytext, editor, provider.awareness);
const setDefaultVal = () => {
if (ytext.toString() === "") {
ytext.insert(0, _fragmentShader);
}
};
if (provider.sync) {
setDefaultVal();
} else {
provider.on("sync", setDefaultVal);
}
setDefaultVal();
// editor.getDoc().markText(
// {
// line: 5,
// ch: 1
// },
// {
// line: 50,
// ch: 3
// },
// {
// css: "color : red"
// }
// );
if (isInPresentationMode()) {
addCodeMirrorPresentModifier();
} else {
addCodeMirrorEditorModifier();
}
// @ts-ignore
window.example = { provider, ydoc, ytext, binding, Y };
}
// this function will trigger a change to the editor
function onEdit() {
const fragmentCode = editor.getValue();
updateShader(fragmentCode);
}
function updateShader(fragmentCode) {
if (!checkFragmentShader(fragmentCode)) {
return;
}
_fragmentShader = fragmentCode;
isDirty = true;
}
window.onload = (event) => {
webgl_startup();
initYdoc();
}
function animateScene() {
gl.viewport(0, 0, glCanvas.width, glCanvas.height);
// This sets background color
gl.clearColor(1, 1, 1, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(shaderProgram);
uResolution =
gl.getUniformLocation(shaderProgram, "u_resolution");
uTime =
gl.getUniformLocation(shaderProgram, "u_time");
uVol =
gl.getUniformLocation(shaderProgram, "u_vol");
gl.uniform2fv(uResolution, resolution);
gl.uniform1f(uTime, previousTime);
if (camera && camera.analyser) {
var bufferLength = camera.analyser.frequencyBinCount;
var dataArray = new Uint8Array(bufferLength);
camera.analyser.getByteTimeDomainData(dataArray);
gl.uniform1f(uVol, getRMS(dataArray));
}
else{
gl.uniform1f(uVol, 0.0);
}
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
aVertexPosition =
gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(aVertexPosition);
gl.vertexAttribPointer(aVertexPosition, vertexNumComponents,
gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, vertexCount);
window.requestAnimationFrame(function(currentTime) {
previousTime = previousTime + .05;
// TODO here check dirty bit and recompile?
if (isDirty) {
// recompile and clear dirty bit
shaderProgram = buildShaderProgram();
isDirty = false;
}
animateScene();
});
}
function compileShader(type, code) {
let shader = gl.createShader(type);
gl.shaderSource(shader, code);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.log(`Error compiling ${type === gl.VERTEX_SHADER ? "vertex" : "fragment"} shader:`);
console.log(gl.getShaderInfoLog(shader));
}
return shader;
}
function buildShaderProgram() {
let program = gl.createProgram();
// Compile vertex shader
let shader = compileShader(gl.VERTEX_SHADER, vertexShader());
gl.attachShader(program, shader);
// Compile fragment shader
shader = compileShader(gl.FRAGMENT_SHADER, fragmentShader());
gl.attachShader(program, shader);
gl.linkProgram(program)
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.log("Error linking shader program:");
console.log(gl.getProgramInfoLog(program));
}
return program;
}
function webgl_startup() {
glCanvas = document.getElementById("glcanvas");
if (glCanvas.width != glCanvas.clientWidth) {
glCanvas.width = glCanvas.clientWidth;
}
if (glCanvas.height != glCanvas.clientHeight) {
glCanvas.height = glCanvas.clientHeight;
}
gl = glCanvas.getContext("webgl");
shaderProgram = buildShaderProgram();
aspectRatio = glCanvas.width/glCanvas.height;
resolution = [glCanvas.width, glCanvas.height];
vertexArray = new Float32Array([
-1, 1,
1, 1,
1, -1,
-1, 1,
1, -1,
-1, -1
]);
vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertexArray, gl.STATIC_DRAW);
vertexNumComponents = 2;
vertexCount = vertexArray.length/vertexNumComponents;
animateScene();
}
function vertexShader() {
return _vertexShaderC;
}
function fragmentShader() {
return _fragmentShader;
}
// this returns false if the fragment shader cannot compile
// true if it can
function checkFragmentShader(shaderCode, lint = false) {
if (!gl) {
return;
}
let shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(shader, shaderCode);
gl.compileShader(shader);
let infoLog = gl.getShaderInfoLog(shader);
let result = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
let ret = [];
if (!result) {
console.log(infoLog);
var errors = infoLog.split(/\r|\n/);
for (let error of errors){
var splitResult = error.split(":")
ret.push( {
message: splitResult[3] + splitResult[4],
character: splitResult[1],
line: splitResult[2]
})
}
}
if (result) {
console.log("did update");
_fragmentShader = shaderCode;
isDirty = true;
}
return ret;
}
(function(mod) {
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
function validator(text, options) {
var result = [];
var errors = checkFragmentShader(text, true);
if (errors) parseErrors(errors, result);
return result;
}
CodeMirror.registerHelper("lint", "x-shader/x-vertex", validator);
function parseErrors(errors, output) {
for ( var i = 0; i < errors.length; i++) {
var error = errors[i];
if (error) {
if (Number(error.line) <= 0) {
console.warn("Cannot display error (invalid line " + error.line + ")", error);
continue;
}
var start = error.character - 1, end = start + 1;
// Convert to format expected by validation service
var hint = {
message: error.message,
severity: "error",
from: CodeMirror.Pos(Number(error.line) - 1, start),
to: CodeMirror.Pos(Number(error.line) - 1, end)
};
output.push(hint);
}
}
}
});