This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** MIT License github.com/pushkine/ */ | |
function cubicBezier(x1: number, y1: number, x2: number, y2: number) { | |
if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1)) | |
throw new Error(`CubicBezier x1 & x2 values must be { 0 < x < 1 }, got { x1 : ${x1}, x2: ${x2} }`); | |
const ax = 1.0 - (x2 = 3.0 * (x2 - x1) - (x1 *= 3.0)) - x1, | |
ay = 1.0 - (y2 = 3.0 * (y2 - y1) - (y1 *= 3.0)) - y1; | |
let i = 0, r = 0.0, s = 0.0, d = 0.0, x = 0.0; | |
return (t: number) => { | |
for (r = t, i = 0; 32 > i; i++) | |
if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1) - t))) return r * (r * (r * ay + y2) + y1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** MIT License github.com/pushkine/ */ | |
interface SpringParams { | |
mass?: number; // = 1.0 | |
damping?: number; // = 10.0 | |
stiffness?: number; // = 100.0 | |
soft?: boolean; // = false | |
} | |
type seconds = number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type RGBA = [number, number, number, number]; | |
const rgb255 = (v: number) => (v < 255 ? (v > 0 ? v : 0) : 255); | |
const b1 = (v: number) => (v > 0.0031308 ? v ** (1 / 2.4) * 269.025 - 14.025 : v * 3294.6); | |
const b2 = (v: number) => (v > 0.2068965 ? v ** 3 : (v - 4 / 29) * (108 / 841)); | |
const a1 = (v: number) => (v > 10.314724 ? ((v + 14.025) / 269.025) ** 2.4 : v / 3294.6); | |
const a2 = (v: number) => (v > 0.0088564 ? v ** (1 / 3) : v / (108 / 841) + 4 / 29); | |
function fromHCL(h: number, c: number, l: number): RGB { | |
const y = b2((l = (l + 16) / 116)); | |
const x = b2(l + (c / 500) * Math.cos((h *= Math.PI / 180))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare global { | |
interface ObjectConstructor { | |
keys<O extends object>(o: O): (keyof O & string)[]; | |
} | |
} | |
/** | |
* Returns tuple types that include every string in union | |
* TupleUnion<keyof { bar: string; leet: number }>; | |
* ["bar", "leet"] | ["leet", "bar"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface LifecycleFunction { | |
(): void; | |
} | |
interface EventCallback<T = unknown> { | |
(event: CustomEvent<T>): void; | |
} | |
export namespace Svelte { | |
export interface Fragment { | |
key?: string | null; | |
first?: Comment | null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** MIT License github.com/pushkine */ | |
export const color = (function <C extends readonly any[], M extends {}>(colors: C, mods: M) { | |
const fn = (c1: number, c2: number, str: string) => `\x1b[${c1}m${str}\x1b[${c2}m`; | |
const fnc = (c1: number, str: string) => fn(c1, 39, str.replace(/\x1b\[39m/g, `\x1b[${c1}m`)); | |
const obj = { unstyle: (str: string) => str.replace(/\x1b\[[0-9]{1,2}m/g, ""), grey: fnc.bind(null, 90) }; | |
for (let i = 0; i < colors.length; i++) obj[colors[i]] = fnc.bind(null, 30 + i); | |
for (const key in mods) obj["" + key] = fn.bind(null, mods[key][0], mods[key][1]); | |
return obj as { [K in C[any] | keyof M | keyof typeof obj]: (str: string) => string }; | |
})( | |
["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"] as const, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function beep() { | |
const ctx = new AudioContext(); | |
const oscillator = ctx.createOscillator(); | |
const gainNode = ctx.createGain(); | |
oscillator.connect(gainNode); | |
gainNode.connect(ctx.destination); | |
gainNode.gain.value = 0.25; | |
oscillator.frequency.value = 350; |