flow: some funcs

This commit is contained in:
cha0s 2021-03-17 23:48:17 -05:00
parent 06f6cf7114
commit 15a60f12a0

View File

@ -58,35 +58,6 @@ export const angleRange = (angle, delta) => ({
min: 360 + (angle - delta),
max: 360 + (angle + delta),
});
export const mod = (l, r) => ((l % r) + r) % r;
export const mul = (l, r) => l * r;
export const normalizeAngleRange = (min, max) => {
/* eslint-disable no-param-reassign */
if (min < 0) {
min += Math.PI * 2;
max += Math.PI * 2;
}
if (min > max) {
[min, max] = [max, min + Math.PI * 2];
}
while (max >= Math.PI * 2) {
min -= Math.PI * 2;
max -= Math.PI * 2;
}
return [min, max];
/* eslint-enable no-param-reassign */
};
export const randomNumber = (min, max) => {
if (min > max) {
// eslint-disable-next-line no-param-reassign
[max, min] = [min, max];
}
return min + Math.random() * (max - min);
};
export const spontaneous = (elapsed, period) => Math.random() < 1 / (period / elapsed);
export const sub = (l, r) => l - r;
export const toRad = (a) => (a % 360) * PI_180;
const binaryOp = (label) => ({
label,
type: 'number',
@ -101,7 +72,6 @@ const binaryOp = (label) => ({
},
],
});
export function children() {
return {
add: binaryOp('add'),
@ -177,3 +147,37 @@ export function children() {
},
};
}
export const clamp = (v, min, max) => Math.max(min, Math.min(max, v));
export const lerp = (v, s, e) => s + v * (e - s);
export const map = (v, os, oe, as, ae) => as + (v - os) * ((ae - as) / (oe - os));
export const mod = (l, r) => ((l % r) + r) % r;
export const mul = (l, r) => l * r;
export const normalizeAngleRange = (min, max) => {
/* eslint-disable no-param-reassign */
if (min < 0) {
min += Math.PI * 2;
max += Math.PI * 2;
}
if (min > max) {
[min, max] = [max, min + Math.PI * 2];
}
while (max >= Math.PI * 2) {
min -= Math.PI * 2;
max -= Math.PI * 2;
}
return [min, max];
/* eslint-enable no-param-reassign */
};
export const randomNumber = (min, max) => {
if (min > max) {
// eslint-disable-next-line no-param-reassign
[max, min] = [min, max];
}
return min + Math.random() * (max - min);
};
// smooth(er)?step assumes unit
export const smoothstep = (x) => x * x * (3 - 2 * x);
export const smootherstep = (x) => x * x * x * (x * (x * 6 - 15) + 10);
export const spontaneous = (elapsed, period) => Math.random() < 1 / (period / elapsed);
export const sub = (l, r) => l - r;
export const toRad = (a) => a * PI_180;