24 lines
1.2 KiB
TypeScript
24 lines
1.2 KiB
TypeScript
export const delay = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
export const nextFrame = async (): Promise<number> => new Promise((resolve) => requestAnimationFrame(resolve));
|
|
|
|
export const randInt = (min: number, max: number) => Math.round(min + (max - min - 1) * Math.random());
|
|
export const randBool = () => Math.random() < 0.5;
|
|
export const choice = (array: any[]) => array[randInt(0, array.length)];
|
|
|
|
export const range = (size: number | string) => Object.keys((new Array(+size)).fill(0)).map(k => +k);
|
|
export const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));
|
|
|
|
export const prevent = (e: Event) => (e.preventDefault(), false);
|
|
|
|
export const intHash = (seed: number, ...parts: number[]) => {
|
|
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
|
|
for (let i = 0; i < parts.length; i++) {
|
|
const ch = parts[i];
|
|
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
}
|
|
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
|
|
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
return h1;
|
|
};
|
|
export const sinHash = (...data: number[]) => data.reduce((hash, n) => Math.sin((hash * 123.12 + n) * 756.12), 0) / 2 + 0.5; |