Skip to content

Instantly share code, notes, and snippets.

@paulcnichols
Created January 13, 2014 01:35
Show Gist options
  • Save paulcnichols/8393280 to your computer and use it in GitHub Desktop.
Save paulcnichols/8393280 to your computer and use it in GitHub Desktop.
A jquery plugin to create a flanel shirt looking background. Original I was using taupes as the color, but then I moved to more pastels. Hence, the name taupeify().
(function ($) {
var globalArgs = {
minWidth: 150,
minHeight: 150,
}
var colorIndex = 0;
var taupeifyRec = function (args) {
var x = .5*args.width, //(Math.random()*.50 + .25)*args.width,
y = .5*args.height, //(Math.random()*.50 + .25)*args.height,
c = ["#546172", "#87A581", "#C0BD86", "#D6C08F", "#DA6A44", "#324152", "#47535E", "#796466", "#C1836A", "#DEA677"];
var w = (x > globalArgs.minWidth && (args.width - x) > globalArgs.minWidth),
h = (y > globalArgs.minHeight && (args.height - y) > globalArgs.minHeight);
if (w && h) {
// (0,0)
taupeifyRec({left: args.left, width: x, top: args.top, height: y, parent: args.parent});
// (1, 0)
taupeifyRec({left: args.left + x, width: args.width - x, top: args.top, height: y, parent: args.parent});
// (0, 1)
taupeifyRec({left: args.left, width: x, top: args.top + y, height: args.height - y, parent: args.parent});
// (1, 1)
taupeifyRec({left: args.left + x, width: args.width - x, top: args.top + y, height: args.height - y, parent: args.parent});
}
else if (w && !h) {
// (0, *)
taupeifyRec({left: args.left, width: x, top: args.top, height: args.height, parent: args.parent});
// (1, *)
taupeifyRec({left: args.left + x, width: args.width - x, top: args.top, height: args.height, parent: args.parent});
}
else if (!w && h) {
// ((*, 0)
taupeifyRec({left: args.left, width: args.width, top: args.top, height: y, parent: args.parent});
// (*, 1)
taupeifyRec({left: args.left, width: args.width, top: args.top + y, height: args.height - y, parent: args.parent});
}
else {
$('<div class="taupeified"></div>')
.css('top', args.top)
.css('left', args.left)
.css('height', args.height)
.css('width', args.width)
.css('background-color', c[++colorIndex%c.length]) //c[Math.floor(Math.random()*c.length)])
.appendTo(args.parent);
return;
}
};
$.fn.taupeify = function (args) {
var parent = this,
resizeTimer,
startTaupeify = function () {
// remove previous
colorIndex = 0;
$('.taupeified').remove();
// redo
taupeifyRec({
parent: parent,
width: parent.width(),
height: parent.height(),
top: parent.offset().top,
left: parent.offset().left
});
};
$(window).resize(function () {
if (resizeTimer) {
clearTimeout(resizeTimer);
}
resizeTimer = setTimeout(function () {
startTaupeify();
resizeTimer = null;
}, 100);
});
startTaupeify();
};
})(jQuery);
// example invocation
$('.body').taupeify();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment