244 pixel art effects, every one a small piece of code. Plus a Claude Code skill to make your own.
// Numbers, easing, randomness and noise. Everything here is pure and deterministic,
// so an effect drawn at time t always looks the same.
export const TAU = Math.PI * 2;
export const clamp = (x, a = 0, b = 1) => (x < a ? a : x > b ? b : x);
export const lerp = (a, b, t) => a + (b - a) * t;
export const invLerp = (a, b, x) => (b === a ? 0 : (x - a) / (b - a));
export const remap = (x, a0, a1, b0, b1) => lerp(b0, b1, invLerp(a0, a1, x));
export const fract = (x) => x - Math.floor(x);
export const wrap = (x, m) => ((x % m) + m) % m;
/** Progress 0..1 of t through the window [t0, t1]: 0 before it, 1 after it. */
export const seg = (t, t0, t1) => clamp((t - t0) / (t1 - t0));
/** Hermite smoothstep of x between e0 and e1. */
export const smooth = (e0, e1, x) => {
const t = seg(x, e0, e1);
return t * t * (3 - 2 * t);
};
/** 0 → 1 → 0 over the window [t0, t1] (sine bump). */
export const bump = (t, t0, t1) => (t <= t0 || t >= t1 ? 0 : Math.sin(Math.PI * seg(t, t0, t1)));
export const pingpong = (x) => 1 - Math.abs(wrap(x, 2) - 1);
export const dist = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
export const angleTo = (x0, y0, x1, y1) => Math.atan2(y1 - y0, x1 - x0);
/** Easing curves: input 0..1, output 0..1 (back and elastic overshoot). */
export const ease = {
linear: (t) => t,
inQuad: (t) => t * t,
outQuad: (t) => 1 - (1 - t) * (1 - t),
inOutQuad: (t) => (t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2),
inCubic: (t) => t * t * t,
outCubic: (t) => 1 - Math.pow(1 - t, 3),
inOutCubic: (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2),
inQuart: (t) => t * t * t * t,
outQuart: (t) => 1 - Math.pow(1 - t, 4),
inExpo: (t) => (t <= 0 ? 0 : Math.pow(2, 10 * t - 10)),
outExpo: (t) => (t >= 1 ? 1 : 1 - Math.pow(2, -10 * t)),
inSine: (t) => 1 - Math.cos((t * Math.PI) / 2),
outSine: (t) => Math.sin((t * Math.PI) / 2),
inOutSine: (t) => -(Math.cos(Math.PI * t) - 1) / 2,
inBack: (t, s = 1.70158) => (t <= 0 ? 0 : t >= 1 ? 1 : (s + 1) * t * t * t - s * t * t),
outBack: (t, s = 1.70158) => (t <= 0 ? 0 : t >= 1 ? 1 : 1 + (s + 1) * Math.pow(t - 1, 3) + s * Math.pow(t - 1, 2)),
outElastic: (t) => (t <= 0 ? 0 : t >= 1 ? 1 : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * ((2 * Math.PI) / 3)) + 1),
outBounce: (t) => {
const n = 7.5625, d = 2.75;
if (t < 1 / d) return n * t * t;
if (t < 2 / d) return n * (t -= 1.5 / d) * t + 0.75;
if (t < 2.5 / d) return n * (t -= 2.25 / d) * t + 0.9375;
return n * (t -= 2.625 / d) * t + 0.984375;
},
};
/** 32-bit integer hash of any number of integers. */
export function hash(...ns) {
let h = 0x9e3779b9;
for (const n of ns) {
h = Math.imul(h ^ (n | 0), 0x85ebca6b);
h ^= h >>> 13;
h = Math.imul(h, 0xc2b2ae35);
h ^= h >>> 16;
}
return h >>> 0;
}
/** Deterministic random number in [0, 1) from integers (seed, particle index, which value...). */
export const rand = (...ns) => hash(...ns) / 4294967296;
/** Seeded random generator (mulberry32). */
export class Rng {
constructor(seed = 1) {
this.s = seed >>> 0 || 1;
}
next() {
let t = (this.s = (this.s + 0x6d2b79f5) >>> 0);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
range(a, b) {
return a + (b - a) * this.next();
}
int(a, b) {
return Math.floor(this.range(a, b + 1));
}
chance(p) {
return this.next() < p;
}
sign() {
return this.next() < 0.5 ? -1 : 1;
}
pick(list) {
return list[Math.floor(this.next() * list.length)];
}
/** Roughly normal, mean 0, sd 1. */
gauss() {
return (this.next() + this.next() + this.next() + this.next() - 2) * 1.732;
}
}
/** A generator for one particle: rng(seed, i) gives the same numbers every frame. */
export const rng = (...ns) => new Rng(hash(...ns));
/** Smooth 1D value noise in [-1, 1]. */
export function noise(x, seed = 0) {
const i = Math.floor(x), f = x - i, u = f * f * (3 - 2 * f);
return lerp(rand(i, seed), rand(i + 1, seed), u) * 2 - 1;
}
/** Smooth 2D value noise in [-1, 1]. */
export function noise2(x, y, seed = 0) {
const ix = Math.floor(x), iy = Math.floor(y), fx = x - ix, fy = y - iy;
const ux = fx * fx * (3 - 2 * fx), uy = fy * fy * (3 - 2 * fy);
const a = rand(ix, iy, seed), b = rand(ix + 1, iy, seed), c = rand(ix, iy + 1, seed), d = rand(ix + 1, iy + 1, seed);
return lerp(lerp(a, b, ux), lerp(c, d, ux), uy) * 2 - 1;
}
/** Fractal 2D noise in about [-1, 1]. */
export function fbm(x, y, seed = 0, octaves = 3) {
let s = 0, a = 0.5, n = 0;
for (let o = 0; o < octaves; o++) {
s += noise2(x, y, seed + o * 101) * a;
n += a;
x *= 2.03;
y *= 2.03;
a *= 0.5;
}
return s / n;
}
/**
* 2D value noise whose lattice repeats every px cells in x and py cells in y (0: no repeat). Scroll it by
* exactly py cells over a loop and the loop has no seam: tileNoise(x, y + phase * py, 0, py).
*/
export function tileNoise(x, y, px = 0, py = 0, seed = 0) {
const ix = Math.floor(x), iy = Math.floor(y), fx = x - ix, fy = y - iy;
const ux = fx * fx * (3 - 2 * fx), uy = fy * fy * (3 - 2 * fy);
const x0 = px ? wrap(ix, px) : ix, x1 = px ? wrap(ix + 1, px) : ix + 1;
const y0 = py ? wrap(iy, py) : iy, y1 = py ? wrap(iy + 1, py) : iy + 1;
const a = rand(x0, y0, seed), b = rand(x1, y0, seed), c = rand(x0, y1, seed), d = rand(x1, y1, seed);
return lerp(lerp(a, b, ux), lerp(c, d, ux), uy) * 2 - 1;
}
/** Fractal tileNoise: every octave keeps the repeat, so scrolled fields still loop. */
export function tileFbm(x, y, px = 0, py = 0, seed = 0, octaves = 3) {
let s = 0, a = 0.5, n = 0, k = 1;
for (let o = 0; o < octaves; o++) {
s += tileNoise(x * k, y * k, px * k, py * k, seed + o * 101) * a;
n += a;
a *= 0.5;
k *= 2;
}
return s / n;
}
/** Noise that loops: the same value at phase 0 and phase 1 (for seamless loops). */
export function loopNoise(phase, radius = 1, seed = 0) {
const a = phase * TAU;
return noise2(Math.cos(a) * radius + 50, Math.sin(a) * radius + 50, seed);
}