Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
gordonbrander / multimethod-example.js
Last active November 18, 2022 05:05
Javascript multimethods
import multimethod from './multimethod.js'
const foo = multimethod(x => typeof x)
foo.string = x => 'called string method'
foo.number = x => 'called number method'
foo('x')
// 'called string method'
foo(1)
@gordonbrander
gordonbrander / htmlin5.md
Last active November 6, 2022 07:36
HTML in 5 min

HTML in 5 min

The gist

  • HTML is just plain text.
  • You write it in any text editor. Be careful, though! Programs like Microsoft Word will trick you. They look like plain text, but they are not. I suggest using something like TextWrangler. It saves plain text, but also helpfully color-codes your code.
  • You save HTML in a text file, but instead of giving it a .txt extension, you give it a .html.

Tags

@gordonbrander
gordonbrander / hollywood-supply-chain.md
Last active January 13, 2016 10:45
Hollywood supply chain

In one Critical Path episode (can't remember which), Horace posits that Hollywood is like a distributed functional organization:

  • Workers are organized by specialty (acting,
@gordonbrander
gordonbrander / boing.js
Last active November 6, 2022 07:34
boing.js (second pass)
// Create a spring object.
export const Spring = (curr, prev, dest, stiffness, damping) => ({
curr, // The current position of spring
prev, // The position of spring at previous tick
dest, // The destination of spring
stiffness, // How hard it springs back
damping, // Friction. 1.0 means no bounce.
});
// Given numbers, calculate the next position for a spring.
@gordonbrander
gordonbrander / boing.js
Last active November 6, 2022 09:20
boing.js - minimal 2D spring physics
export const Spring = (x, y, mass, stiffness, viscosity) => ({
prevX: x,
prevY: y,
currX: x,
currY: y,
mass, stiffness, viscosity
});
Spring.copy = (spring) => ({
prevX: spring.prevX,
@gordonbrander
gordonbrander / yayquery.js
Created September 2, 2015 21:09
yayquery.js - All you really need
export const $$ = (selector) => document.querySelector(selector);
export const $ = (selector) => document.querySelectorAll(selector);
// Apply a side effect to `n` items.
export const sets = (f, n, ...rest) => {
for (var i = 0; i < n.length; i++) f(n[i], ...rest);
}
export const toggleClass = (el, classname, isAdding) =>
isAdding ? el.classList.add(classname) : el.classList.remove(classname);
@gordonbrander
gordonbrander / bus.js
Last active September 4, 2015 22:26
Simple app microframework based on a message bus
// A message bus. Dispatches messages to `receive` in order (FIFO).
const Bus = (receive) => {
var isDraining = false;
const queue = [];
// Define a function to send a message to the queue.
const send = (msg) => {
queue.push(msg);
// If we're not already draining the queue, start draining.
// We only want to kick this loop off once, since send can be called
@gordonbrander
gordonbrander / scene.js
Last active November 6, 2022 09:19
scene.js - basic Timeline-driven animation functions
export const progress = (t, start, end) =>
t < start ? 0 :
t > end ? 1 :
(t / (end - start));
export const tween = (start, end, step, state, t) =>
step(state, progress(t, start, end));
export const event = (time, step, state, t) =>
t < time ? step(state, 0) : step(state, 1);
@gordonbrander
gordonbrander / css-namespace.js
Last active November 6, 2022 09:18
CSS namespacing
/*
Automatically namespace all CSS selectors.
Eliminates most problems with style leak
.active {}
...becomes
.button-active {}
*/
@gordonbrander
gordonbrander / states.js
Created July 16, 2015 22:04
Finite state switcher
// Create a finite state switching function.
// Given a list of states, returns a function that will take a `prev` state
// and `next` state and will only advance state to `next` if it is one of
// the defined states.
const states = (...states) => (prev, next) =>
states.indexOf(next) !== -1 ? next : prev;