Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
gordonbrander / event-delegate.js
Last active October 16, 2018 07:11
addEventDelegate() - easy event delegation
// Copyright 2018 Gordon Brander
// Released under MIT License https://opensource.org/licenses/MIT
const closest = (el, selector) =>
el.closest ? el.closest(selector) : null;
/**
* Delegate event handling to a parent element.
* @arg {Element} el - the parent element that we will be delegating handling to.
* @arg {string} selector - CSS selector of elements that should receive events.
@gordonbrander
gordonbrander / ruleset.example.js
Last active November 6, 2022 09:20
ruleset.js — parse style-attribute-like strings.
import {Schema, string, bool, number, listOf, optional, cssUnit4} from './ruleset.js';
const parse = Schema({
title: string,
isHidden: optional(bool, false),
numbers: listOf(number)
});
parse("title: 8 Beautiful Notes; numbers: 1, 2, 3, 4, 5, 6, 7, 8")
// > {title: "8 Beautiful Notes", numbers: [1, 2, 3...], isHidden: false}
@gordonbrander
gordonbrander / select.js
Last active November 6, 2022 07:35
select — sugarfree D3-style enter/update/exit DOM manipulation, using a simple function instead of method chaining
// D3-style select-baed DOM updating.
//
// Rather than relying on jQuery-style method-chaining to update the DOM,
// we use a "view" object, an ordinary object that contains the following
// functions:
//
// - `enter(datum) -> Element` returns a DOM node for new elements.
// Element will be appended to parent.
// - `update(el, datum, old)` handles mutating an element in response to
// changes in data. `old` is the last-known data for this element.
@gordonbrander
gordonbrander / preload.js
Last active November 6, 2022 09:19
Preload images with promises
// Tip: use with await.
// const preloaded = await preloadAll(['img1.jpg', 'img2.jpg'])
export const preload = src => new Promise((resolve, reject) => {
const img = new Image()
img.onload = resolve
img.onerror = reject
img.src = src
})
@gordonbrander
gordonbrander / frame.js
Last active November 6, 2022 07:36
frame.js
// Promise for an animation frame.
// Tip: use with `await`.
export const frame = () => new Promise(requestAnimationFrame)
@gordonbrander
gordonbrander / dom.js
Last active November 6, 2022 09:18
Dom from simple VDOM
const dom = ({tag='div', attributes={}, styles={}, classes=[], text=null, children=null}) => {
const el = document.createElement(tag)
el.className = classes.join(' ')
for (let [key, value] of Object.entries(attributes)) {
if (value) {
el.setAttribute(key, value)
}
}
@gordonbrander
gordonbrander / hollywood-vs-sw.md
Last active July 21, 2019 20:12
Hollywood vs Software
@gordonbrander
gordonbrander / webvr-daydream.md
Last active January 31, 2023 22:21
How to get WebVR working with Daydream

How to get WebVR working with your brand-new Daydream View & Pixel.

  1. Update Daydream app to latest (Play Store)
  2. Update Chrome app to latest (Play Store)
    • Tip: if you're feeling brave, download Chrome Canary instead. Better perf and new features.
  3. Update Google VR Services (Play Store)
  4. Open chrome://flags/#enable-webvr
    • Click "Enable"
  5. Open chrome://flags/#enable-vr-shell
    • Set dropdown to "Enabled"
@gordonbrander
gordonbrander / vendor_all_the_firmware.py
Last active March 24, 2017 15:11
Vendor all openag firmware in one fell swoop
"""
This script fetches the latest master of all of the openag firmware repositories, then removes the `.git`, etc.
"""
import subprocess
import argparse
from os import path
LIBS = set((
"https://github.com/OpenAgInitiative/openag_firmware_module.git",
"https://github.com/OpenAgInitiative/rosserial_arduino_libs.git",
@gordonbrander
gordonbrander / general_greens.json
Last active February 9, 2017 09:22
General Greens
{
"_id": "general_greens",
"recipe_format": "phased",
"version": "1.0",
"optimization": [
"general purpose"
],
"date_created": "2017-02-08",
"author": "openag",
"stages": [