Skip to content

Instantly share code, notes, and snippets.

View getify's full-sized avatar
💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / the-rafpolyfill.js
Created June 27, 2012 06:44
setTimeout vs. nested-RAF
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
@getify
getify / naf.js
Created June 27, 2012 14:19
requestNextAnimationFrame() and cancelNextAnimationFrame()
// requires raf.js (polyfil)
(function(){
var ids = {};
function requestId(){
var id;
do {
id = Math.floor(Math.random() * 1E9);
} while (id in ids);
@getify
getify / canvas-dashed-lines.js
Created June 30, 2012 04:04
polyfill for dashed-lines in canvas
// note: uses the native "mozDash" or "webkitLineDash" functionality if present, polyfills with manual dashed-lines drawing
// note #2: this polyfill does *not* currently work with arcs, bezier-curves, and quadratic-curves. that functionality would be theoretically possible, but is probably very hard to implement.
// note #3: it also doesn't work with rect() or strokeRect() yet, though those should be relatively straightforward to do, by manually drawing the lines using lineTo().
(function(){
var _moveTo = CanvasRenderingContext2D.prototype.moveTo,
_lineTo = CanvasRenderingContext2D.prototype.lineTo,
pos = {
@getify
getify / gist:3057796
Created July 6, 2012 02:58
exploring javascript type auto-coercion (and its pitfalls)
// **********
// NOTE: this is a stupid trivial example illustrating automatic
// type-coercion, not an example of the kind of data-structure logic
// you'd ever actually use. don't miss the forest for the trees.
// Read down in the comment thread for 'myCoolString' for a slightly
// more sensible example.
//
// NOTE #2: this gist thread was spawned from a twitter discussion
// about `parseInt(0/1,19)==18`:
// https://twitter.com/getify/status/221009838006747136
@getify
getify / EventEmitter.either.js
Created July 17, 2012 16:19
extending the `EventEmitter` API with `either/or` pattern
EventEmitter.prototype.either = function(evt,cb) {
var self = this, orAPI = {or: bindHandler}, handlers = {};
function bindHandler(evt,cb) {
if (!handlers || handlers[evt]) return;
handlers[evt] = function(){
for (var e in handlers) {
self.removeListener(e,handlers[e]);
}
var xhr, form_data = new FormData();
xhr = new XMLHttpRequest();
xhr.open("POST","http://barfoo.com/upload");
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.setRequestHeader("File-Name",file.name);
xhr.setRequestHeader("File-Size",(file.size || file.fileSize));
xhr.setRequestHeader("File-Type",file.type);
xhr.setRequestHeader("Session-ID",12345);
@getify
getify / gist:3185473
Created July 27, 2012 00:45
attempting to use jParser to extract dimensions of JPG image file
var fs = require("fs");
var jParser = require("jParser");
var eof = false;
fs.readFile("sample.jpg",function(err,data) {
if (!err) {
var view = new jDataView(data, undefined, undefined, false);
var parser = new jParser(view,{
// JPG
@getify
getify / gist:3189596
Created July 27, 2012 18:25 — forked from rexmac/gist:3189232
attempting to use jParser to extract dimensions of JPG image file
var fs = require("fs");
var jParser = require("jParser");
var eof = false;
fs.readFile("sample.jpg",function(err,data) {
if (!err) {
var view = new jDataView(data, undefined, undefined, false);
var parser = new jParser(view,{
// JPG
@getify
getify / ex1.php
Created August 12, 2012 14:15
exploring inline decision-logic in templates
<!-- here's the PHP'ish way of making inline decisions while constructing HTML markup -->
<select name="foobar">
<option value="bam" <?=($foobar==="bam"?"selected":"")?>>Bam</option>
<option value="baz" <?=($foobar==="baz"?"selected":"")?>>Baz</option>
</select>
<input type="radio" name="foobar" value="bam" <?=($foobar==="bam"?"checked":"")?>> Bam
<input type="radio" name="foobar" value="baz" <?=($foobar==="baz"?"checked":"")?>> Baz
@getify
getify / test.js
Created August 16, 2012 21:25
object JSON serialization that's circular-ref safe
// all this `toJSON()` does is filter out any circular refs. all other values/refs,
// it passes through untouched, so it should be totally safe. see the test examples.
// only extend the prototype if `toJSON` isn't yet defined
if (!Object.prototype.toJSON) {
Object.prototype.toJSON = function() {
function findCircularRef(obj) {
for (var i=0; i<refs.length; i++) {
if (refs[i] === obj) return true;
}