Skip to content

Instantly share code, notes, and snippets.

@jacksonwillis
Created June 3, 2012 04:21
Show Gist options
  • Save jacksonwillis/2861868 to your computer and use it in GitHub Desktop.
Save jacksonwillis/2861868 to your computer and use it in GitHub Desktop.
baobob
(function () {
var root = this,
previousDollar = root["$"];
var $ = function (it) {
return document.getElementById(it);
};
root.baobob = root["$"] = $;
$.noConflict = function () {
root["$"] = previousDollar;
};
// Combinators
var S, K, I, M, Y;
$.comb = {
// Starling \abc.ac(bc)
S: (S = function (a) {
return function (b) {
return function (c) {
return (a(c))(b(c));
};
};
}),
// Kestrel \ab.a
K: (K = function (a) {
return function (b) {
return a;
};
}),
// Idiot Bird \a.a
I: (I = S(K)(K)),
// Mockingbird \a.aa
M: (M = S(I)(I)),
// Why Bird \a.a(\a)
Y: (Y = function (f) {
return (function (x) {
return f(function (v) {
return x(x)(v);
});
})
(function (x) {
return f(function (v) {
return x(x)(v);
});
});
})
};
// DOM Manipulation
$.ready = function(W,D){ // https://github.com/airportyh/onready
function e(){f=!0;c(e);if(b){for(var a=0;a<b.length;a++)b[a]();b=null}}
var d,c,E=W.addEventListener,R=W.removeEventListener,A=D.attachEvent,
T=D.detachEvent,L="load",C="DOMContentLoaded",O="onreadystatechange";
E?(d=function(a){E(C,a,!1);E(L,a,!1)},c=function(a){R(C,a,!1);R(L,a,!1)}
):A&&(d=function(a){A(O,a);A(L,a)},c=function(a){T(O,a);T(L,a)});
var b=null,f=!1;return function(a){if(f)a();else{if(!b){b=[];d(e)}
b.push(a)}}}(window,document);
// chain.js <https://github.com/chriso/chain.js>
// Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>. MIT Licensed
(function(a){a=a||{};var b={},c,d;c=function(a,d,e){var f=a.halt=!1;a.error=function(a){throw a},a.next=function(c){c&&(f=!1);if(!a.halt&&d&&d.length){var e=d.shift(),g=e.shift();f=!0;try{b[g].apply(a,[e,e.length,g])}catch(h){a.error(h)}}return a};for(var g in b){if(typeof a[g]==="function")continue;(function(e){a[e]=function(){var g=Array.prototype.slice.call(arguments);if(e==="onError"){if(d){b.onError.apply(a,[g,g.length]);return a}var h={};b.onError.apply(h,[g,g.length]);return c(h,null,"onError")}g.unshift(e);if(!d)return c({},[g],e);a.then=a[e],d.push(g);return f?a:a.next()}})(g)}e&&(a.then=a[e]),a.call=function(b,c){c.unshift(b),d.unshift(c),a.next(!0)};return a.next()},d=a.addMethod=function(d){var e=Array.prototype.slice.call(arguments),f=e.pop();for(var g=0,h=e.length;g<h;g++)typeof e[g]==="string"&&(b[e[g]]=f);--h||(b["then"+d.substr(0,1).toUpperCase()+d.substr(1)]=f),c(a)},d("chain",function(a){var b=this,c=function(){if(!b.halt){if(!a.length)return b.next(!0);try{null!=a.shift().call(b,c,b.error)&&c()}catch(d){b.error(d)}}};c()}),d("run",function(a,b){var c=this,d=function(){c.halt||--b||c.next(!0)},e=function(a){c.error(a)};for(var f=0,g=b;!c.halt&&f<g;f++)null!=a[f].call(c,d,e)&&d()}),d("defer",function(a){var b=this;setTimeout(function(){b.next(!0)},a.shift())}),d("onError",function(a,b){var c=this;this.error=function(d){c.halt=!0;for(var e=0;e<b;e++)a[e].call(c,d)}})})($);
var head = document.getElementsByTagName("head")[0] || document.documentElement;
$.addMethod('load', function (args, argc) {
for (var queue = [], i = 0; i < argc; i++) {
(function (i) {
queue.push(function (src) {
return function (onload, onerror) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.onload = onload;
script.onerror = onerror;
script.onreadystatechange = function () {
var state = this.readyState;
if (state === 'loaded' || state === 'complete') {
script.onreadystatechange = null;
onload();
}
};
head.insertBefore(script, head.firstChild);
}
}(args[i]));
}(i));
}
this.call('run', queue);
});
$.addMethod('css', function (args, argc) {
for (var queue = [], i = 0; i < argc; i++) {
(function (i) {
queue.push(function (href) {
return function (onload, onerror) {
var link = document.createElement('link');
link.type = 'text/css';
link.href = href;
head.insertBefore(script, head.firstChild);
}
}(args[i]));
}(i));
}
this.call('run', queue);
});
// HTMLCanvasElement
$.animate = function (frameRate, callback) {
var options = { frameRate: frameRate };
Y(function (f) {
return function (frame) {
callback.call(options, frame) && setTimeout(function () {
f.call(options, frame + 1);
}, 1000 / options.frameRate);
};
})(1);
};
$.mousemove = function (canvas, callback) {
return canvas.addEventListener("mousemove", function (event) {
var totalOffset = { x: 0, y: 0 };
do {
totalOffset.x += canvas.offsetLeft;
totalOffset.y += canvas.offsetTop;
}
while (canvas = canvas.offsetParent);
callback(event.pageX - totalOffset.x, event.pageY - totalOffset.y);
});
};
$.MousePos = function (canvas, startX, startY) {
var self = this;
this.canvas = canvas;
this.x = (startX || 0);
this.y = (startY || 0);
Y(function (f) {
$.mousemove(canvas, function (x, y) {
self.x = x;
self.y = y;
f();
});
});
};
// Time helpers
$.inAbout = function(n) {
return {
milliseconds: function(f) {
return setTimeout(f, n);
},
seconds: function(f) {
return setTimeout(f, n * 1000);
}
};
};
$.every = function(n) {
return {
milliseconds: function(f) {
return setInterval(f, n);
},
seconds: function(f) {
return setInterval(f, n * 1000);
}
};
};
// Vector Math
$.Vector = (function () {
function Vector (x, y, z) {
this.x = (x || 0);
this.y = (y || 0);
this.z = (z || 0);
};
Vector.fromArray = function (arr) {
return new Vector(arr[0], arr[1], arr[2]);
};
Vector.fromObject = function (obj) {
return new Vector(obj.x, obj.y, obj.z);
};
var def = Vector.prototype;
def.toString = function () {
return "<" + this.x + ", " + this.y + ", " + this.z + ">";
};
def.toArray = function () {
return [this.x, this.y, this.z];
};
def.equals = function (other) {
var a = this.toArray(), b = other.toArray();
return !(a<b || b<a);
};
def.plus = function (other) {
return new Vector(this.x + other.x, this.y + other.y, this.z + other.z);
};
def.minus = function (other) {
return this.plus(other.times(-1));
};
def.times = function (n) {
return new Vector(this.x * n, this.y * n, this.z * n);
};
def.dot = function (other) {
return (this.x * other.x) + (this.y * other.y) + (this.z * other.z);
};
def.magnitude = function () {
return Math.sqrt(this.dot(this));
};
def.unit = function () {
var magnitude = this.magnitude();
return new Vector(this.x / magnitude, this.y / magnitude, this.z / magnitude);
};
def.cross = function (other) {
var i = this.y * other.z - other.y * this.z;
var j = this.z * other.x - other.z * this.x;
var k = this.x * other.y - other.x * this.y;
return new Vector(i, j, k);
};
def.angleBetween = function (other) {
return Math.acos(this.dot(other) / (this.magnitude() * other.magnitude())); // radians
};
return Vector;
})();
$.V = function() { return $.Vector.fromArray(arguments); };
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment