Skip to content

Instantly share code, notes, and snippets.

View epreston's full-sized avatar

Ed Preston epreston

View GitHub Profile
@epreston
epreston / http-rollup-server.cjs
Created November 26, 2023 03:32
tool: http server and rollup watcher dev env
#!/usr/bin/env node
var fs = require('fs');
var http = require('http');
var path = require('path');
var rollup = require('rollup');
var rollupConfig = require('../rollup.config');
var CONTENT_TYPES = {
'.html': 'text/html',
@epreston
epreston / mini-reset.css
Created August 8, 2023 08:34
css - minimal and opinionated css reset from kevin powell
/* makes sizing simpler */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* remove default spacing */
/* force styling of type through styling, rather than elements */
@epreston
epreston / imgCompress.js
Created August 8, 2023 07:05
javascript - node tool - deeply compress images in a folder
import imagemin from 'imagemin';
import imageminWebp from 'imagemin-webp';
import imageminPngquant from 'imagemin-pngquant';
const produceWebP = async () => {
const outputFolder = './images/webp';
await imagemin(['images/*.png'], {
destination: outputFolder,
plugins: [
@epreston
epreston / isTranferable.js
Created April 30, 2023 05:18
javascript - compatibility - determine if an object is transferable to worker thread
function isTranferable(obj) {
try {
return (
obj instanceof ArrayBuffer ||
obj instanceof MessagePort ||
obj instanceof ImageBitmap || // safari 15+ ios 15+
obj instanceof OffscreenCanvas // safari 16.4+ ios 16.4+
);
} catch {
return false;
@epreston
epreston / addObjectPool.js
Created April 29, 2023 09:22
add object pool support to a prototype - avoid small object allocation with reuse - alloc, get, release
const isFn = (a) => typeof a === 'function';
export function addObjectPool(objtype, resetFunction, maxSize = Number.MAX_SAFE_INTEGER) {
const pool = [];
function newObject() {
return new objtype();
}
if (isFn(resetFunction)) {
@epreston
epreston / runWhenBrowserEnvReady.js
Last active April 28, 2023 22:09
run callback when browser environment ready - run immediately outside of browser
const isRunningInBrowser = typeof window !== 'undefined';
function runWhenBrowserEnvReady(fn) {
if (isRunningInBrowser) {
const doc = document;
doc.readyState[0] === 'l' ? doc.addEventListener('DOMContentLoaded', fn) : fn();
} else {
fn();
}
}
@epreston
epreston / FastSimplexNoise.js
Created April 28, 2023 21:49
noise 2D - fast simplex noise utility function
/*
* Based on example code by Stefan Gustavson (stegu@itn.liu.se).
* Optimisations by Peter Eastman (peastman@drizzle.stanford.edu).
* Better rank ordering method by Stefan Gustavson in 2012.
*
* This code was placed in the public domain by its original author,
* Stefan Gustavson. You may use it as you see fit, but
* attribution is appreciated.
*/
@epreston
epreston / hash2d.js
Created April 28, 2023 21:38
hash2d - javascript version of the cheap 2D hash often used in GLSL shaders
// returns a number with the fractional part of the given number
function fract(num) {
return num - Math.floor(num);
}
// hash2d - not repeatable or stable between platforms
function hash2d(x, y) {
return fract(43758.5453 * Math.sin(x * 78.233 - 12.9898 * y));
}
@epreston
epreston / Semaphore.js
Created April 28, 2023 21:10
promise based semaphore - caller controllable promise interface
function Semaphore() {
// controllable promise
let _resolve;
let _reject;
const promiseSemaphore = new Promise((resolve, reject) => {
_resolve = resolve;
_reject = reject;
});
@epreston
epreston / resolveAfter.js
Created April 28, 2023 20:51
promise based delay - runs async after paint
function resolveAfter(ms) {
// intoduces a delay
return new Promise((resolve) => setTimeout(resolve, ms));
}