Skip to content

Instantly share code, notes, and snippets.

View zz85's full-sized avatar

Joshua Koo zz85

View GitHub Profile
@zz85
zz85 / otsu.js
Last active February 4, 2022 18:31
Otsu's method - Automatic Histogram Based Image Thresholding
// Read https://en.wikipedia.org/wiki/Otsu%27s_method (added this since JS examples in wikipedia didn't work)
function otsu(histData /* Array of 256 greyscale values */, total /* Total number of pixels */) {
let sum = 0;
for (let t=0 ; t<256 ; t++) sum += t * histData[t];
let sumB = 0;
let wB = 0;
let wF = 0;
@zz85
zz85 / lights.html
Last active December 10, 2016 08:02
Christmas Lights for Novation Launchpad
<html>
<head>
</head>
<body>
<script>
// Christmas light hack for Web Audio Hack Day 2016
// with vanilla JS tested on chrome and the LaunchPad
let device;
let stop = 0;
@zz85
zz85 / video_record.html
Created December 12, 2016 15:24
Video PlaybackRate Not Respected with MediaRecorder
<html>
<body>
<script>
const video = document.createElement('video');
document.body.appendChild(video);
video.autoplay = true;
video.src = 'videos.mp4';
video.playbackRate = 2;
.glitch {
width: 100%
}
.hero-wrap .glitch:after,
.hero-wrap .glitch:before {
content: attr(data-text);
position: absolute;
width: 100%;
top: 0;
color: #222222;
@zz85
zz85 / README
Last active August 31, 2018 07:00
OSX Terminal Shortcuts (/Working with Long Terminal Commands)
Movements
Esc - F (Forward a word)
Esc - B (Back a word)
Ctrl - A (Start of line)
Ctrl - E (End of line)
Ctrl - F (Next line)
Ctrl - B (Previous line)
Deletions
@zz85
zz85 / Cache.js
Created July 8, 2017 18:38
Generic Cache
class GenericCache {
constructor() {
this.cache = new Map();
}
set(key, promise, options) {
if (typeof promise === 'function') {
promise = promise();
}
@zz85
zz85 / poc.js
Last active July 11, 2017 17:24
Distributed Consistent Hashing
const { EventEmitter } = require('events');
// const jumphash = require('jumphash');
const _jumphash = require('@ceejbot/jumphash'),
crypto = require('crypto');
function jumphash(key, buckets, algo='md5') {
const buffer = crypto
.createHash(algo)
.update(key)
@zz85
zz85 / lag.js
Last active August 8, 2017 10:04
Node Event Loop Lag
// Also see https://github.com/tj/node-blocked/blob/master/index.js and https://stackoverflow.com/questions/28563354/how-to-detect-and-measure-event-loop-blocking-in-node-js
const blocked = require('blocked');
blocked(function(ms) {
console.log("Blocked", ms);
}, { threshold: 10 });
var blockDelta = 1;
var interval = 500;
@zz85
zz85 / counter.js
Last active August 23, 2017 04:26
Simple Counter
class Counter {
constructor() {
this.map = new Map();
}
add(key) {
this.map.set(key, this.map.has(key) ? this.map.get(key) + 1 : 1);
}
print() {
@zz85
zz85 / draw.js
Created September 27, 2017 20:59
minimal dom
function draw(path, current) {
const navs = [
elm('h5', { class: 'nav-group-title' }, `${path.join('/')} ${format(current.value)}`, {
onclick: () => State.navigateTo(path.slice(0, -1))
})
]
// let str = '----------\n'
const nodes = current._children || current.children || []