Skip to content

Instantly share code, notes, and snippets.

const updateReason = fn =>
function(prevProps, prevState) {
const isEqual = (a, b) => {
if (a === null || b === null || a === undefined || b === undefined) {
return a === b;
} else if (a.constructor === Object && b.constructor === Object) {
const keysA = Object.keys(a).sort();
const keysB = Object.keys(b).sort();
if (keysA.join(',') === keysB.join(',')) {
return keysA.every(key => isEqual(a[key], b[key]));
const slots = [50, 40, 200, 300, 400].sort((a, b) => a < b);
const slotSum = slots.reduce((sum, s) => sum + s, 0);
const random = () => {
const r = Math.random() * slotSum;
for (let i = 0, c = 0; i < slots.length; i++) {
c += slots[i];
if (r < c) {
return i;
}
}
@httnn
httnn / example.js
Created January 19, 2017 12:34
MobX: pattern for updating data when other data changes?
class Store {
constructor() {
reaction(
() => [this.filter, this.limit, this.order],
() => this.getItems()
);
this.getItems();
}
@observable filter = '';
@httnn
httnn / waveforms.html
Last active December 2, 2021 10:00
Drawing waveforms with the Web Audio API
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Waveform drawer</title>
</head>
<body>
<input type="file" /><br />
<svg preserveAspectRatio="none" width="2000" height="100" style="width:900px;height:50px;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<linearGradient id="Gradient" x1="0" x2="0" y1="0" y2="1">
@httnn
httnn / matches.js
Created July 22, 2015 09:12
fuzzy searching with JavaScript
var matches = function(string, q) {
q = q.toLowerCase().replace(/[^\w]/g, '');
var inner = function(string, q) {
if (q.length) {
var i = string.indexOf(q[0]);
return i > -1 ? inner(string.substr(i), q.substr(1)) : false;
}
return true;
};
return inner(string.toLowerCase(), q);
@httnn
httnn / gist:a6ab15cbba9c82a5065d
Created April 9, 2015 16:28
Calculate average color of Android Bitmap
/*
pixelSpacing tells how many pixels to skip each pixel.
If pixelSpacing > 1: the average color is an estimate, but higher values mean better performance
If pixelSpacing == 1: the average color will be the real average
If pixelSpacing < 1: the method will most likely crash (don't use values below 1)
*/
public int calculateAverageColor(android.graphics.Bitmap bitmap, int pixelSpacing) {
int R = 0; int G = 0; int B = 0;
int height = bitmap.getHeight();
int width = bitmap.getWidth();
@httnn
httnn / gist:b1d772caf76cdc0c11e2
Created April 9, 2015 16:23
Calculate brightness of Android Bitmap
/*
Calculates the estimated brightness of an Android Bitmap.
pixelSpacing tells how many pixels to skip each pixel. Higher values result in better performance, but a more rough estimate.
When pixelSpacing = 1, the method actually calculates the real average brightness, not an estimate.
This is what the calculateBrightness() shorthand is for.
Do not use values for pixelSpacing that are smaller than 1.
*/
public int calculateBrightnessEstimate(android.graphics.Bitmap bitmap, int pixelSpacing) {
int R = 0; int G = 0; int B = 0;
int height = bitmap.getHeight();