All pastes #29649 Raw Edit

Someone

public javascript v1 · immutable
#29649 ·published 2005-11-20 17:08 UTC
rendered paste body
/*Script to size three lines of header to fit in the same width.*/if (document.getElementById("header").clientWidth != undefined) {	debug = false;	minLetterSpacing = 0;	scaleHeader();}function scaleHeader() {	var windowWidth = getWindowWidth();	var margin = 30;	var target = windowWidth - 2 * margin;		if (debug) {info("TARGET: " + target);}		if (debug) {info("------------Element: logo");}		scaleToWidth(document.getElementById("logo"),target);		if (debug) {info("------------Element: tag-line");}	scaleToWidth(document.getElementById("tag-line"),target);	if (debug) {info("------------Element: header-address");}	scaleToWidth(document.getElementById("header-address"),target);}function getWindowWidth() {	if (typeof(self.innerHeight) != "undefined") {				// All except Internet Explorer:		var width = self.innerWidth;	} else if (typeof(document.documentElement.clientHeight) != "undefined") {				// Internet Explorer 6 Strict Mode:		var width = document.documentElement.clientWidth;	} else if (typeof(document.body) != "undefined") {		// Other Internet Explorers:		var width = document.body.clientWidth;	}	if (opera.postError !="undefined") {opera.postError("Window width: " + width);}	return width;}function widthAtFontSize(element,ems) {	element.style.fontSize = ems + "em";	var width = element.clientWidth;	if (opera.postError != "undefined") {opera.postError("clientWidth: " + width + " pixelWidth: " + element.style.pixelWidth);}	return width;}function widthAtLetterSpacing(element,ems) {	element.style.letterSpacing = ems + "em";	var width = element.clientWidth;	return width;}function sizeToWidth(element,target) {		// Define variables;	var iterand = 0.01;	var lBound = 0;	var uBound = iterand;	var midPoint = iterand;	var count = 0;				// Find upper bound:		while (widthAtFontSize(element,uBound) < target) {		uBound = uBound  + iterand;	}		if (debug) {info("Found uBound: " + uBound);}	// Binary search:		var widthAtMidPoint = widthAtFontSize(element,midPoint);	do {		count++;				if (count > 10 && (target - widthAtMidPoint) > - 1) {			break;		} else if (count > 20) {						if (debug) {info("Went on too long.");}						break;			}				midPoint = (lBound + uBound)/2;		widthAtMidPoint = widthAtFontSize(element,midPoint);		if (debug) {info("FONT SIZE count: " + count + " midPoint: " + midPoint + " widthAtMidPoint: " + widthAtMidPoint + " target: " + target);}					if ((target - widthAtMidPoint) < 1 && (target - widthAtMidPoint) > -1) {						if (debug) {info("Broke with difference " + Math.abs(target - widthAtMidPoint));}						break;		} else if (widthAtMidPoint > target) {			uBound = midPoint;		} else if (widthAtMidPoint < target) {			lBound = midPoint;		}			} while ((target - widthAtMidPoint) > 1 || (target - widthAtMidPoint) < -1)		if (debug) {info("Font size: " + element.style.fontSize);}}function spaceToWidth(element,target) {		// Define variables;	var iterand = 0.01;	var lBound = minLetterSpacing;	var uBound = iterand;	var midPoint = iterand;	var count = 0;				// Find upper bound:		while (widthAtLetterSpacing(element,uBound) < target) {		uBound = uBound  + iterand;	}			if (debug) {info("Found uBound: " + uBound);}	// Binary search:		var widthAtMidPoint = widthAtLetterSpacing(element,midPoint);	do {		count++;				if (count > 10 && (target - widthAtMidPoint) > - 1) {			break;		} else if (count > 20) {						if (debug) {info("Went on too long.");}						break;		}				midPoint = (lBound + uBound)/2;		widthAtMidPoint = widthAtLetterSpacing(element,midPoint);		if (debug) {info("LETTER SPACING count: " + count + " midPoint: " + midPoint + " widthAtMidPoint: " + widthAtMidPoint + " target: " + target);}					if ((target - widthAtMidPoint) < 1 && (target - widthAtMidPoint) > -1) {			if (debug) {info("Broke with difference " + Math.abs(target - widthAtMidPoint));}						break;		} else if (widthAtMidPoint > target) {			uBound = midPoint;		} else if (widthAtMidPoint < target) {			lBound = midPoint;		}			} while ((target - widthAtMidPoint) > 1 || (target - widthAtMidPoint) < -1)		if (debug) {info("Letter spacing: " + element.style.letterSpacing);}}function floatElement(element) {	if (typeof(element.style.cssFloat) != "undefined") {				// Not Internet Explorer:		element.style.cssFloat = "left";	} else if (typeof(element.style.styleFloat) != "undefined") {				// Internet Explorer:		element.style.styleFloat = "left";	}	}	function unfloatElement(element) {			if (typeof(element.style.cssFloat) != "undefined") {						// Not Internet Explorer:			element.style.cssFloat = "none";		} else if (typeof(element.style.styleFloat) != "undefined") {						// Internet Explorer:			element.style.styleFloat = "none";		}			}		function scaleToWidth(element,target) {		// Set to minimum letter-spacing:		element.style.letterSpacing = minLetterSpacing + "em";		// Style element for scaling:		floatElement(element);		element.style.lineHeight = "110%";		element.style.textAlign = "center";		element.style.whiteSpace = "nowrap";				// Scale the element:		sizeToWidth(element,target);		spaceToWidth(element,target);				// Restyle element after scaling:		unfloatElement(element);		element.style.lineHeight = "110%";		element.style.textAlign = "center";		element.style.whiteSpace = "normal";	}