Skip to content

Instantly share code, notes, and snippets.

@zachstronaut
zachstronaut / gist:1184831
Created August 31, 2011 21:53
Get a relative time string in PHP without a lot of if/else or switch/case
<?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:1184900
Created August 31, 2011 22:22
Stretch HTML5 canvas to fill window, preserving aspect ratio
/**
* fullscreenify()
* Stretch canvas to size of window.
*
* Zachary Johnson
* http://www.zachstronaut.com/
*
* See also: https://gist.github.com/1178522
*/
@zachstronaut
zachstronaut / gist:1208947
Created September 10, 2011 23:30
Update NimbleKit's NKLog with proper variable interrogation
//
// 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));
}
@zachstronaut
zachstronaut / gist:1705796
Created January 30, 2012 18:24
An HTMLSAFE and SLUGIFY in JavaScript
/**
* 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:3838175
Created October 5, 2012 04:55
javascript string object property chain to actual property
function resolve(obj, path) {
var prop;
path = path.split('.');
while (obj = obj[path.shift()]) {
prop = obj;
}
return prop;
@zachstronaut
zachstronaut / gist:3843393
Created October 6, 2012 01:34
WordPress get_posts() vs query_posts() MySQL behavior
<?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:3955874
Created October 25, 2012 22:28
Example Parameterized Generation of XYZ Points for 3D Objects
// 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:4118766
Created November 20, 2012 15:56
JavaScript Networked Games: x,y game coordinates in combined 4 byte integer
// 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:4640634
Created January 26, 2013 06:50
quick and dirty preloading
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:4659962
Created January 28, 2013 22:38
Add TextMate math bundle style math evaluation to Sublime Text 2
#!/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