Skip to content

Instantly share code, notes, and snippets.

@zachstronaut
zachstronaut / gist:1184900
Created Aug 31, 2011
Stretch HTML5 canvas to fill window, preserving aspect ratio
View gist:1184900
/**
* fullscreenify()
* Stretch canvas to size of window.
*
* Zachary Johnson
* http://www.zachstronaut.com/
*
* See also: https://gist.github.com/1178522
*/
@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');
@zachstronaut
zachstronaut / gist:1184831
Created Aug 31, 2011
Get a relative time string in PHP without a lot of if/else or switch/case
View gist:1184831
<?php
/**
* time_elapsed_string()
*
* Zachary Johnson
* http://www.zachstronaut.com/posts/2009/01/20/php-relative-date-time-string.html
*/
function time_elapsed_string($ptime) {
$etime = time() - $ptime;
@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
*/