Skip to content

Instantly share code, notes, and snippets.

declare module 'measure-text' {
function measureText({
text,
fontFamily,
fontSize,
lineHeight,
fontWeight,
fontStyle,
canvas,
}: {
@nkint
nkint / commatize-millions.js
Created September 22, 2018 10:36
commatize millions number
// taken actually from https://github.com/adamwdraper/Numeral-js/blob/master/src/numeral.js#L241
function commatizeMillions(number, lang) {
const str = (number).toFixed(0).toString()
const separator = lang === ITA ? '.' : ','
const out = str.replace(/(\d)(?=(\d{3})+(?!\d))/g, `$1${separator}`)
return out
}
@nkint
nkint / gist:3ea570bbc8ed4d42cc932a51249082f8
Last active September 27, 2018 12:29
Download canvas content as png
// TODO: does not handle `URL.revokeObjectURL`
function downloadCanvasContent(canvas: HTMLCanvasElement, filename: string) {
const link = document.createElement('a')
link.href = canvas.toDataURL()
link.download = filename
link.click()
}
@nkint
nkint / 1.js
Created November 6, 2018 08:50 — forked from getify/1.js
tag function for formatting console.log(..) statements
function logger(strings,...values) {
var str = "";
for (let i = 0; i < strings.length; i++) {
if (i > 0) {
if (values[i-1] && typeof values[i-1] == "object") {
if (values[i-1] instanceof Error) {
if (values[i-1].stack) {
str += values[i-1].stack;
continue;
}
@nkint
nkint / random-point-spike.ts
Created March 7, 2019 12:33
random points in a circle with mbostock and umbrella
import { renderOnce, clearDOM } from "@thi.ng/hdom";
import * as svg from "@thi.ng/hiccup-svg";
import * as tx from "@thi.ng/transducers";
import * as isec from "@thi.ng/geom-isec";
import { add2, subN2 } from "@thi.ng/vectors";
import { poissonDiscSampler } from "./poisson-disc-sampler";
const width = 600;
const height = 500;
@nkint
nkint / gist:0436719e2c6cae01e9cd27009cf5e884
Last active May 15, 2019 22:32
Quantum Harmonic Oscillator Animation
// use typescript
// use math.js
import math from "mathjs";
// a
// a1 a2 b c d
// (2^n*n!)^(-1/2) * Pi^(-1/4) * Exp[-x^2/2] * HermiteH[n, x];
function psi(n: number, x: number) {
@nkint
nkint / WIP.ts
Last active July 15, 2019 15:00
add raf stream
import * as tx from "@thi.ng/transducers";
import { clearDOM } from "@thi.ng/hdom";
import {
stream,
Stream,
sync,
fromRAF,
sidechainToggle
} from "@thi.ng/rstream";
import { updateDOM } from "@thi.ng/transducers-hdom";
@nkint
nkint / a-test.ts
Last active August 19, 2019 19:08
1566240473 shader ast, broken test
import { clearDOM } from '@thi.ng/hdom'
import { Vec } from '@thi.ng/vectors'
import { buildSketch3d } from './sketch-skelton'
import { adaptDPI, CanvasHandlers } from '@thi.ng/hdom-components'
import { compileModel, draw, ModelSpec, shader, quad } from '@thi.ng/webgl'
import {
defMain,
assign,
FLOAT0,
FLOAT1,
@nkint
nkint / array-buffer-XMLHttpRequest.ts
Created September 28, 2019 14:45
Load array buffer via XMLHttpRequest, the old good way
function ab2str(buf: any) {
return Buffer.from(buf).toString("utf8");
}
console.log("hello");
var oReq = new XMLHttpRequest();
console.log(oReq);
oReq.addEventListener("load", reqListener);
oReq.open("GET", "./test.env");
oReq.responseType = "arraybuffer";
@nkint
nkint / use-script.ts
Created October 17, 2019 10:37
useScript
import { useState, useEffect } from 'react';
import { noop } from 'lodash';
/** original code from
* https://usehooks.com/useScript/
* https://stackoverflow.com/a/51242436/433685
* */
function isScriptLoaded(src: string) {
return document.querySelector('script[src="' + src + '"]') ? true : false;