audio-vis/raw/javascript/visual.js

114 lines
3 KiB
JavaScript
Raw Permalink Normal View History

2020-04-07 21:44:46 +02:00
class Visual {
constructor() {
this.data = []; //for drawing
this.dataArray = [];
2020-08-06 23:44:37 +02:00
this.name = "Default";
2020-04-07 21:44:46 +02:00
}
2020-08-01 21:51:54 +02:00
updateData() {
2020-08-06 23:44:37 +02:00
}
2020-08-01 21:51:54 +02:00
2020-08-06 23:44:37 +02:00
updateFFT(fftSize) {
2020-08-01 21:51:54 +02:00
}
2020-04-07 21:44:46 +02:00
draw() {
}
setup() {
}
}
class VisualDrawer {
2020-08-01 21:51:54 +02:00
constructor() {
this.visuals = {
2020-08-06 23:44:37 +02:00
//"sphere": new Sphere(),
2020-08-01 21:51:54 +02:00
"wave": new Wave(),
2020-08-06 23:44:37 +02:00
"wave2d": new Wave2D(),
//"water": new Water()
}
this.lastMainColor = {
base: '#-1',
color: [0, 0, 0]
};
this.lastSecondColor = {
base: '#-1',
color: [0, 0, 0]
2020-08-01 21:51:54 +02:00
}
}
2020-04-07 21:44:46 +02:00
2020-08-01 21:51:54 +02:00
init() {
2020-08-06 23:44:37 +02:00
this.switch(pConf.get("visual", "wave2d"));
2020-08-01 21:51:54 +02:00
this.updateLoop();
}
switch(visual) {
if (this.visuals[visual] != null) {
this.c = visual;
vConf.loadConfigByName(this.c);
2020-08-01 21:51:54 +02:00
this.visuals[this.c].setup();
2020-08-06 23:44:37 +02:00
pConf.set("visual", this.c);
pConf.save();
2020-08-01 21:51:54 +02:00
}
}
updateLoop() {
let self = this;
let vis = self.visuals[self.c];
2020-08-06 23:44:37 +02:00
let pro = shaderHandler.use(self.c);
this.updateSeekbar();
this.prepare(pro);
2020-08-01 21:51:54 +02:00
vis.updateData();
vis.draw(pro);
requestAnimationFrame(self.updateLoop.bind(self))
}
2020-08-06 23:44:37 +02:00
updateSeekbar() {
cInfo.width = window.innerWidth;
cInfo.height = window.innerHeight;
let audioFile = audioHandler.audioFile;
ctx.clearRect(0, 0, cInfo.width, cInfo.height);
if (!audioFile.paused && pConf.get("showSeekbar", true)) {
//show seekbar
let dur = audioFile.duration,
cur = audioFile.currentTime,
percent = cur / dur * cInfo.width;
ctx.fillStyle = pConf.get("seekColor", '#fff');
ctx.fillRect(0, c.height - 10, percent, c.height);
}
}
prepare(pro) {
c.width = window.innerWidth;
c.height = window.innerHeight;
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(0, 0, 0, parseFloat(pConf.get("alphaValue", 0)));
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.enable(gl.CULL_FACE);
2020-08-06 23:44:37 +02:00
// u_baseColor || u_maxColor
this.setColor(pro);
}
setColor(program) {
let baseColor = gl.getUniformLocation(program, "u_baseColor"),
maxColor = gl.getUniformLocation(program, "u_maxColor"),
self = this,
mainColor = self.lastMainColor,
secondColor = self.lastSecondColor;
this.updateColor('lastMainColor', 'baseColor');
this.updateColor('lastSecondColor', 'gradientToColor');
gl.uniform3fv(baseColor, mainColor.color);
gl.uniform3fv(maxColor, secondColor.color);
}
updateColor(index, col) {
let color = this[index],
value = vConf.get(col, '#ffffff')
if(value !== color.base) {
color.color = hexToRgb(value);
color.base = value;
}
}
2020-04-07 21:44:46 +02:00
}