Skip to content

Instantly share code, notes, and snippets.

View greggman's full-sized avatar
😁

Greggman greggman

😁
View GitHub Profile
@greggman
greggman / test.jslib
Last active February 4, 2016 10:51
unity js issue
var test = {
useClosure: (function() {
var callCount_ = 0;
return function() {
console.log("been called: ", ++callCount_);
};
}()),
};
function PseudoRandom() {
var randomSeed_ = 0;
var RANDOM_RANGE_ = Math.pow(2, 32);
/**
* Returns a deterministic pseudorandom number between 0 and 1
* @return {number} a random number between 0 and 1
*/
this.random = function() {
@greggman
greggman / debug.js
Last active December 13, 2016 09:16
A colorful JavaScript logger - similar to npm/debug
// example of making something like npm's debug module inside electron
const makeLogFunc = require('./logger');
const s_debugRE = function(debug) {
debug = debug || '-----';
const patterns = debug.split(',').map(pattern => {
return pattern.replace(/\*/, '.*?') || '----';
});
const pattern = '^(' + patterns.join('|') + ')$';
@greggman
greggman / sed-in-place.sh
Created December 14, 2016 14:38
osx sed in place with newline of files matching grep
grep -l -R --include="*.js" --include="*.html" webglLessonsHelper.setupSlider . | xargs -n 1 sed -E -i '' $'s/<script src="([^"]+)\\/webgl-utils.js"/<script src="\\1\\/webgl-lessons-ui.js"><\\/script>\\\n<script src="\\1\\/webgl-utils.js"/'
@greggman
greggman / example.js
Created August 3, 2017 09:32
mod your shader in js
function getShaderSource(needsBGRA) {
const twizzle = needsBGRA ? '.bgra' : '';
return `
...
imageColor = texture2D(u_image, v_image_coordinates)${twizzle};
...
`;
}
@greggman
greggman / GameObjectPooler.cs
Created April 2, 2018 15:25
Generic GameObject pool for Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
This is an attempt at a generic GameObject Pool system.
To Use:
@greggman
greggman / userContent.css
Created December 4, 2018 07:23
Hide score on hacker news in firefox
/*
For me I know my score on HN gamifies me into wanting to spend more time on HN.
Anytime I see my score go up feel a tiny hit of pleasure and click to see why.
Looking at my comments anytime I see the score I feel proud someone liked my
comment.
While that is probably the intent I feel it's compelling me in unhealthy ways
and promotes my addiction to HN.
@greggman
greggman / mousecapture.md
Last active May 22, 2019 20:09
Mouse Capture JavaScript

Mouse Capture

Mouse capture in this document means you want it so when the user clicks and drags on some element that element continues to receive events until the user releases the mouse button even if they drag outside of the element or even outside the window.

Solution:

  1. On element mousedown add events to document or window for mousemove and mouseup.
@greggman
greggman / webglresourcetracker.js
Created June 17, 2019 03:16
Find any webgl resources leaks and where they were allocated
const WebGLResourceTracker = function() {
let webglResources = new Map();
const stack = [];
[
'Buffer',
'Renderbuffer',
'Texture',
'Framebuffer',
'Shader',
'Program',
@greggman
greggman / coroutinerunner.js
Created July 3, 2019 09:32
Coroutine runner
function* waitSeconds(duration) {
while (duration > 0) {
duration -= globals.deltaTime;
yield;
}
}
function* waitFrames(numFrames) {
while (numFrames > 0) {
--numFrames;