Skip to content

Instantly share code, notes, and snippets.

@ryanburnette
Created December 2, 2013 03:19
Show Gist options
  • Save ryanburnette/7744471 to your computer and use it in GitHub Desktop.
Save ryanburnette/7744471 to your computer and use it in GitHub Desktop.
An old script for squeezing in formatted text that I need to rewrite.
(function( $ ){
$.fn.shrunkFit = function( options ) {
return this.each(function() {
var settings = $.extend({
'increment' : 0.05,
'throttle' : 25
}, options);
// Set up our test elements
var shrunkWrap = false;
var scount = $('body').data('scount');
if ( scount === undefined ) {
scount = 1;
}
if ( $(this).hasClass("shrunkrun") === false ) {
$(this).addClass("shrunkrun");
$(this).addClass("shrunkrun-"+scount);
$('body').data('scount',scount);
$(this).html(function (i, html) {
return html.replace(/^[^a-zA-Z]*([a-zA-Z])/g, '<span class="shrunkWrap charTest">$1</span>');
});
var charTestHeight = $(this).children('.charTest').height();
$(this).css('font-size',charTestHeight+'px');
$(".shrunkrun-"+scount).data("charTestHeight",charTestHeight);
shrunkWrap = $(this).text().split(' ');
for ( var i = 0, len = shrunkWrap.length; i < len; i++ ) {
shrunkWrap[i] = '<span class="shrunkWrap word word-' + i + '">' + shrunkWrap[i] + '</span>';
}
$(this).html(shrunkWrap.join(' '));
scount = scount+1;
}
else {
// reset charTestHeight and remove previous settings
var charTestHeight = $(this).data("charTestHeight");
$(this).children('.shrunkWrap.word').css('font-size','1em');
}
// check each word for a height greater than charTestHeight
var tooTallTest = true;
var thisTooTallTest = false;
var latestEm = 1;
while ( tooTallTest === true ) {
thisTooTallTest = false;
$(this).children('.shrunkWrap.word').each(function() {
if ( $(this).height() > charTestHeight ) {
thisTooTallTest = true;
}
});
if ( thisTooTallTest === true ) {
latestEm = latestEm - settings.increment;
$(this).children('.shrunkWrap.word').css('font-size',latestEm+'em');
}
else {
break;
}
}
});//
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment