Skip to content

Instantly share code, notes, and snippets.

View nrkn's full-sized avatar

Nik nrkn

View GitHub Profile
@nrkn
nrkn / fittedSize.js
Last active October 8, 2019 18:29
Get best fontSize to use to fit text in bounds in PDFKit
var Pdf = require( 'pdfkit' );
var fs = require( 'fs' );
var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit. Ut nec accumsan nisl. Suspendisse rhoncus nisl posuere tortor tempus et dapibus elit porta. Cras leo neque, elementum a rhoncus ut, vestibulum non nibh. Phasellus pretium justo turpis. Etiam vulputate, odio vitae tincidunt ultricies, eros odio dapibus nisi, ut tincidunt lacus arcu eu elit. Aenean velit erat, vehicula eget lacinia ut, dignissim non tellus. Aliquam nec lacus mi, sed vestibulum nunc. Suspendisse potenti. Curabitur vitae sem turpis. Vestibulum sed neque eget dolor dapibus porttitor at sit amet sem. Fusce a turpis lorem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;';
var temp = new Pdf();
function measureH
@nrkn
nrkn / asm.js
Created October 7, 2019 06:17
Minimal instruction set
const add = (memory, dest, src) => {
memory[dest] += memory[src]
}
const addV = (memory, dest, value) => {
memory[dest] += value
}
const sub = (memory, dest, src) => {
memory[dest] -= memory[src]
@nrkn
nrkn / object-filter.js
Created October 3, 2019 01:00
object filter
export const objectFilter = ( obj, predicate ) => {
if ( obj === null || obj === undefined )
throw Error( 'Cannot convert null or undefined to an object' )
if ( typeof predicate !== 'function' )
throw Error( 'Expected predicate to be a function' )
obj = Object( obj )
return Object.keys( obj ).reduce(
// it's array-like, but it's not an array
const notAnArray = {
length: 3,
0: 'a',
1: 'b',
2: 'c'
}
console.log( Array.isArray( notAnArray ) ) // false
export const createRobot = ( name, job ) => ({ name, job })
export const isRobot = value =>
value && typeof value.name === 'string' && typeof value.job === 'string'
export const introduceRobot = ({ name, job }) =>
console.log( `Hi, I'm ${ name }. My job is ${ job }.` )
export const createPoint = ( x, y ) => ({ x, y })
export const isPoint = value =>
value && typeof value.x === 'number' && typeof value.y === 'number'
export const translatePoint = ( { x: x0, y: y0 }, { x: x1, y: y1 } ) =>
createPoint( x0 + x1, y0 + y1 )
@nrkn
nrkn / hedgehog.js
Last active May 27, 2019 23:30
Encapsulation with ES6 modules
export const Hedgehog = () => {
const speed = 10000
const name = 'Sonic'
const hedgehog = {
name,
zoom: () => zoom( hedgehog.name, speed )
}
return hedgehog
@nrkn
nrkn / index.html
Created January 22, 2019 04:14
WebGL CRT Neon Matrix Effect
<div><canvas width="640" height="480" /></div>
<!--
WebGL CRT monitor effect demo with a tiny surprise. Uses offscreen 2D canvas as buffer source.
Libraries and sources:
- ReGL (WebGL helper): http://regl.party/
- glMatrix (math): http://glmatrix.net/
@nrkn
nrkn / pokémon-strong-against.md
Created October 11, 2018 05:54
Mnemonics for remembering what Pokémon are strong against

Fighting is strong against Normal because:

  • A trained fighter will beat a normal person in combat

Fighting is strong against Rock because:

  • Fighting hits so hard it shatters rock

Fighting is strong against Steel because:

@nrkn
nrkn / GAME_MASTER_v0_1.protobuf
Created October 10, 2018 02:18 — forked from anonymous/GAME_MASTER_v0_1.protobuf
Pokemon Go decoded GAME_MASTER protobuf file v0.1
Result: 1
Items {
TemplateId: "BADGE_BATTLE_ATTACK_WON"
Badge {
BadgeType: BADGE_BATTLE_ATTACK_WON
BadgeRanks: 4
Targets: "\nd\350\007"
}
}
Items {