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
View http-rollup-server.cjs
#!/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
View mini-reset.css
/* 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
View imgCompress.js
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 / basic.eslintrc.json
Last active August 6, 2023 18:16
eslintrc.json - root template, config and rules for prettier, helpful extras
View basic.eslintrc.json
{
"root": true,
"env": {
"es2022": true,
"browser": true,
"node": true
},
"parserOptions": {
"ecmaVersion": 2022,
"sourceType": "module"
@epreston
epreston / FastSimplexNoise.js
Created April 28, 2023 21:49
noise 2D - fast simplex noise utility function
View FastSimplexNoise.js
/*
* 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 / isTranferable.js
Created April 30, 2023 05:18
javascript - compatibility - determine if an object is transferable to worker thread
View isTranferable.js
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
View addObjectPool.js
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
View runWhenBrowserEnvReady.js
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 / resolveWhenIdle.js
Last active April 28, 2023 22:01
wait for idle promise - runs async right before paint - safari fallback
View resolveWhenIdle.js
const idleOptions = { timeout: 500 };
const request = window.requestIdleCallback || window.requestAnimationFrame;
const cancel = window.cancelIdleCallback || window.cancelAnimationFrame;
const resolveWhenIdle = {
request,
cancel,
promise: (num) => new Promise((resolve) => request(resolve, Object.assign({}, idleOptions, num))),
};
@epreston
epreston / hash2d.js
Created April 28, 2023 21:38
hash2d - javascript version of the cheap 2D hash often used in GLSL shaders
View hash2d.js
// 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));
}