audio-vis/raw/javascript/utils.js
2020-08-07 19:31:30 +02:00

269 lines
6.3 KiB
JavaScript

class VTUtils {
static random(min, max) {
let rand = Math.random();
if (typeof min === 'undefined') {
return rand;
} else if (typeof max === 'undefined') {
if (min instanceof Array) {
return min[Math.floor(rand * min.length)];
} else {
return rand * min;
}
} else {
if (min > max) {
let tmp = min;
min = max;
max = tmp;
}
return rand * (max - min) + min;
}
};
static randomInt(min, max) {
return Math.floor(VTUtils.random(min, max));
}
static normalize(val, max, min) {
return (val - min) / (max - min);
};
static distance(x, y, x2, y2) {
let a = x - x2;
let b = y - y2;
return Math.sqrt(a * a + b * b);
}
static map(n, start1, stop1, start2, stop2, withinBounds) {
let newVal = (n - start1) / (stop1 - start1) * (stop2 - start2) + start2;
if (!withinBounds) {
return newVal;
}
if (start2 < stop2) {
return this.constrain(newVal, start2, stop2);
} else {
return this.constrain(newVal, stop2, start2);
}
};
static constrain(n, low, high) {
return Math.max(Math.min(n, high), low);
}
static hsvToRgb(h, s, v) {
let r, g, b,
i = Math.floor(h * 6),
f = h * 6 - i,
p = v * (1 - s),
q = v * (1 - f * s),
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0:
r = v, g = t, b = p;
break;
case 1:
r = q, g = v, b = p;
break;
case 2:
r = p, g = v, b = t;
break;
case 3:
r = p, g = q, b = v;
break;
case 4:
r = t, g = p, b = v;
break;
case 5:
r = v, g = p, b = q;
break;
}
return {r: r, g: g, b: b};
}
static peakRGB(peak) {
return {
r: peak,
g: 1 - peak,
b: 0
};
}
}
class VTVector {
constructor(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
//helper
static createRandom(x, y, z) {
x = x || 1;
y = y || 1;
z = z || 0;
return new VTVector(VTUtils.random(-x, x), VTUtils.random(-y, y), VTUtils.random(-z, z));
}
mult(times) {
this.x *= times;
this.y *= times;
this.z *= times;
}
set(vector) {
this.x = vector.x;
this.y = vector.y;
this.z = vector.z;
}
add(vector) {
this.x = this.x + vector.x;
this.y = this.y + vector.y;
this.z = this.z + vector.z;
}
addXYZ(x, y, z) {
this.x += x;
this.y += y;
this.z += z;
}
setXYZ(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
clone() {
return new VTVector(this.x, this.y, this.z);
}
}
function $(sel, s) {
s = s || document;
return s.querySelector(sel);
}
function $$(sel, s) {
s = s || document;
return s.querySelectorAll(sel);
}
Node.prototype.addDelegatedEventListener = function (type, aim, cb) {
this.addEventListener(type, (event) => {
let target = event.target;
if (target.matches(aim)) {
cb(event, target);
} else {
let parent = target.closest(aim);
if (parent) {
try {
cb(event, parent);
} catch (e) {
NotificationHandler.createNotification("FATAL ERROR WITHIN HANDLER!", "error", 1000);
//nothing!
}
}
}
})
};
Node.prototype.hasClass = function (className) {
let items = className.split(','),
has = null;
for (let item of items) {
if (has === false) {
break;
}
has = this.classList.contains(item.trim());
}
return has === true;
}
Node.prototype.addClass = function (className) {
let items = className.split(',');
for (let item of items) {
this.classList.add(item.trim());
}
return this;
}
Node.prototype.removeClass = function (className) {
let items = className.split(',');
for (let item of items) {
this.classList.remove(item.trim());
}
return this;
}
Node.prototype.toggleClass = function (className, force) {
let items = className.split(',');
for (let item of items) {
this.classList.toggle(item.trim(), force);
}
}
Node.prototype.switchClass = function (clOne, clTwo, twoOne) {
let cl = this.classList;
if (twoOne) {
cl.remove(clOne);
cl.add(clTwo)
} else {
cl.remove(clTwo)
cl.add(clOne)
}
}
Node.prototype.toggleCheck = function (className, force) {
let cl = this.classList;
let items = className.split(',');
for (let item of items) {
let clOne = item.trim();
if (force) {
cl.add(clOne);
} else {
cl.remove(clOne)
}
}
}
String.prototype.firstUpper = function () {
return this.charAt(0).toUpperCase() + this.slice(1);
}
File.prototype.toBase64 = function (cb) {
const reader = new FileReader();
reader.onloadend = cb;
reader.readAsDataURL(this);
}
function b64toBlob(b64Data, type) {
const byteCharacters = atob(b64Data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
return new Blob([byteArray], {type: type});
}
function create(name, content) {
let d = document.createElement(name);
if (content) {
d.innerHTML = content;
}
return d;
}
function append(to, array) {
for (let item of array) {
to.appendChild(item);
}
}
function hexToRgb(hex) {
hex = hex.replace("#", "");
let bigint = parseInt(hex, 16),
r = (bigint >> 16) & 255,
g = (bigint >> 8) & 255,
b = bigint & 255;
return [r / 255, g / 255, b / 255];
}