Skip to content

Instantly share code, notes, and snippets.

Avatar

Zachary Johnson zachstronaut

View GitHub Profile
@zachstronaut
zachstronaut / example.js
Last active Nov 27, 2017
Support for 1 - 4 Gamepad Controllers in Impact.js for Local Multiplayer Games
View example.js
// bind keyboard and gamepad buttons
ig.input.bind( ig.KEY.X, 'shoot1');
ig.input.bind( ig.GAMEPAD1.FACE_1, 'shoot1');
ig.input.bind( ig.GAMEPAD2.FACE_1, 'shoot2');
ig.input.bind( ig.GAMEPAD3.FACE_1, 'shoot3');
ig.input.bind( ig.GAMEPAD4.FACE_1, 'shoot4');
View gist:8721419
/*
Ok, so let's say you've got a situation where one browser fires
two events and the other browser only fires one.
How about a specific example: IE does not fire the "change" event on
a text input when you press the enter key, whereas other browsers do.
Well, what if you want to execute some code both when an
input changes OR when a user presses enter? You don't want
@zachstronaut
zachstronaut / gist:4659962
Created Jan 28, 2013
Add TextMate math bundle style math evaluation to Sublime Text 2
View gist:4659962
#!/bin/sh
# Provide TextMate-like math eval support to Sublime Text 2
# { "keys": ["super+shift+m"], "command": "filter_through_command", "args": { "cmdline": "~/st_math.sh" } },
foo=$(cat /dev/stdin)
bar=$(echo "scale=4; $foo" | bc)
/bin/echo -n $bar
@zachstronaut
zachstronaut / gist:4640634
Created Jan 26, 2013
quick and dirty preloading
View gist:4640634
var preloadImages = [
'path/to/image1.png',
'path/to/image2.png',
'path/to/image3.png'
],
toLoad = 0,
preload = function () {
var i = preloadImages.length;
@zachstronaut
zachstronaut / gist:4118766
Created Nov 20, 2012
JavaScript Networked Games: x,y game coordinates in combined 4 byte integer
View gist:4118766
// Entity coordinates. Note requirement that 0 < x < 65535 ... same for y!
x = 54321;
y = 12345;
// x,y now stored in single 4 byte integer
pos = (x << 16) + y;
// Get back x and y
x = pos >>> 16; // unsigned right shift
y = pos & 65535;
@zachstronaut
zachstronaut / gist:3955874
Created Oct 25, 2012
Example Parameterized Generation of XYZ Points for 3D Objects
View gist:3955874
// Zachary Johnson
// http://www.zachstronaut.com
var x, y, z, i = 250;
while (i--) {
/* random distribution /*
x = -125 + Math.round(Math.random() * 125 * 2);
y = -125 + Math.round(Math.random() * 125 * 2);
z = -Math.round(Math.random() * 250);
*/
@zachstronaut
zachstronaut / gist:3843393
Created Oct 6, 2012
WordPress get_posts() vs query_posts() MySQL behavior
View gist:3843393
<?php
/**
* Am I missing something obvious? Is there some flag for caching attachments or something?
*
* My actual use case is pulling all the posts from a custom post type... but the behavior is the same with good old plain posts, too.
* Your test posts will need Featured Images.
*/
/**
@zachstronaut
zachstronaut / gist:3838175
Created Oct 5, 2012
javascript string object property chain to actual property
View gist:3838175
function resolve(obj, path) {
var prop;
path = path.split('.');
while (obj = obj[path.shift()]) {
prop = obj;
}
return prop;
@zachstronaut
zachstronaut / gist:1705796
Created Jan 30, 2012
An HTMLSAFE and SLUGIFY in JavaScript
View gist:1705796
/**
* SLUGIFY - Attempts to match Django slugify filter: "Converts to lowercase, removes non-word characters (alphanumerics and underscores) and converts spaces to hyphens. Also strips leading and trailing whitespace."
*/
function SLUGIFY(str) {
return str.toString().toLowerCase().replace(/^\s+|\s+$/g,'').replace(/\s/g, '-').replace(/[^-_a-z0-9]/g, '');
}
/**
* HTMLSAFE - Convert &<>" to HTML entities
*/
@zachstronaut
zachstronaut / gist:1208947
Created Sep 10, 2011
Update NimbleKit's NKLog with proper variable interrogation
View gist:1208947
//
// https://gist.github.com/1208947
// Include this JS right after NKit.js in your HTML files.
// Now NKLog() will show you the contents of arrays and objects!
//
var NKLog1 = NKLog;
var NKLog = function (arg) {
NKLog1('\n' + NKInterogate(arg));
}
You can’t perform that action at this time.