Skip to content

Instantly share code, notes, and snippets.

View shaneriley's full-sized avatar

Shane Riley shaneriley

View GitHub Profile
@shaneriley
shaneriley / gist:3800176
Created September 28, 2012 14:26
Ruby-like iteration in Javascript
var œ = function(number) {
return {
times: {
length: number,
"do": function(fn) {
var len = this.length;
for (var i = 0; i < len; i++) {
fn(i);
}
}
@shaneriley
shaneriley / gist:3853159
Created October 8, 2012 15:38
jQuery Exact Contains
// Problem: jQuery's :contains expression does not do an exact match.
// Solution: Exact contains `$("div").filter(":econtains(smart)");`
$.extend($.expr[":"], {
econtains: function(obj, index, meta, stack) {
return (obj.textContent || obj.innerText || $(obj).text() || "").toLowerCase() == meta[3].toLowerCase();
}
});
@shaneriley
shaneriley / gist:3879386
Created October 12, 2012 14:16
jQuery.createPlugin
// Use: passing in an object representing a jQuery plugin. The object is expected to have
// a name attribute (your plugin name in string form, e.g. name: "accordion") and an init
// method. The createPlugin method will take each element in the collection when the
// plugin method is called and initialize the plugin individually, writing a data object
// to the element with the same name as the plugin and storing the plugin object for
// later reference.
(function($) {
$.createPlugin = function(plugin) {
$.fn[plugin.name] = function(opts) {
var $els = this,
@shaneriley
shaneriley / mountain.json
Created November 15, 2012 14:33
Render Tiled Scene in Canvas
{ "height":15,
"layers":[
{
"data":[79, 80, 95, 96, 79, 80, 95, 96, 79, 80, 95, 96, 79, 28, 60, 61, 62, 30, 95, 96, 95, 96, 79, 80, 95, 96, 79, 80, 95, 96, 79, 80, 95, 28, 76, 77, 78, 30, 79, 80, 79, 80, 95, 96, 79, 80, 95, 96, 79, 80, 95, 96, 79, 28, 76, 15, 78, 30, 95, 96, 95, 96, 79, 80, 95, 96, 79, 80, 95, 96, 79, 80, 95, 28, 76, 16, 78, 30, 79, 80, 79, 80, 95, 96, 79, 80, 95, 96, 79, 80, 95, 96, 79, 28, 76, 31, 78, 30, 95, 96, 95, 96, 79, 80, 95, 156, 157, 158, 95, 96, 79, 80, 95, 28, 76, 32, 78, 112, 13, 13, 79, 80, 95, 96, 79, 172, 173, 144, 158, 80, 95, 96, 79, 28, 76, 47, 48, 61, 61, 62, 95, 96, 79, 80, 156, 143, 138, 160, 190, 96, 12, 13, 13, 111, 76, 16, 15, 16, 31, 78, 13, 13, 14, 96, 172, 138, 173, 174, 79, 80, 44, 45, 45, 127, 92, 93, 93, 93, 93, 94, 109, 110, 30, 80, 188, 189, 189, 190, 95, 96, 79, 80, 95, 44, 45, 45, 45, 45, 45, 45, 141, 142, 30, 96, 79, 80, 95, 96, 79, 80, 95, 96, 79, 80, 95, 96, 79, 80, 95, 96, 45, 45, 46, 80, 95, 96, 79, 80, 95, 96, 79, 80, 95, 96, 79, 80,
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="toggleText.js"></script>
<meta charset=utf-8 />
<title>Character Limit</title>
</head>
<body>
<a href="#" data-alt_text="Unfriend">Add to friends</a>
@shaneriley
shaneriley / gist:5048623
Created February 27, 2013 15:11
BinaryObject constructor
BinaryObject = function(a, b) {
var o = {
0: a,
1: b,
val: function(bool) { return bool ? this[1] : this[0]; }
};
return o;
};
var bin = new BinaryObject("no", "yes");
@shaneriley
shaneriley / gist:5266479
Last active December 15, 2015 13:19
Tiled Scene Prototype
Scene = function(opts) {
var instance = {
layers: [],
renderLayer: function(layer) {
// data: [array of tiles, 1-based, position of sprite from top-left]
// height: integer, height in number of sprites
// name: "string", internal name of layer
// opacity: integer
// type: "string", layer type (tile, object)
// visible: boolean
@shaneriley
shaneriley / gist:5406862
Created April 17, 2013 19:05
DOMTokenList Prototype Extensions
// Usage: `dom_element.classList.addAll(["foo", "bar"], "baz")` and `dom_element.classList.removeAll("foo", ["bar", "baz"])`
// See http://jsbin.com/obenin for demo
(function() {
var args;
var flattenArgs = function(collection) {
if (typeof collection === "string") {
args.push(collection);
return;
}
collection = Array.prototype.slice.call(collection);
@shaneriley
shaneriley / gist:5729791
Created June 7, 2013 14:48
Lighten/darken hex color
// shadeColor("#f0f0f0", -20) returns "#bdbdbd"
function shadeColor(color, percent) {
var num = parseInt(color.slice(1), 16),
amt = Math.round(2.55 * percent),
R = (num >> 16) + amt,
B = (num >> 8 & 0x00FF) + amt,
G = (num & 0x0000FF) + amt;
return "#" + (
0x1000000 + (R < 255 ? R < 1 ? 0 : R : 255) * 0x10000
@shaneriley
shaneriley / gist:5729805
Created June 7, 2013 14:50
$().cssGradient
$.fn.cssGradient = function(start, end) {
end = end || start;
if (!start) { return this; }
return this.css({
"background-image": "-moz-linear-gradient(100% 100% 90deg, " + end + ", " + start + ")",
"background-image": "-webkit-gradient(linear, 0% 0%, 0% 100%, from(" + start + "), to(" + end + "))",
"-ms-filter": "progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr=" + start + ", endColorstr=" + end + ")"
});
};