Skip to content

Instantly share code, notes, and snippets.

@shivanarrthine
Created October 16, 2012 15:29
Show Gist options
  • Save shivanarrthine/3899981 to your computer and use it in GitHub Desktop.
Save shivanarrthine/3899981 to your computer and use it in GitHub Desktop.
An attempt to improve the user experience of the text box, by allowing the text to shrink to fit when it is full... and a slightly different focus effect.
<input type='text' id='resizer' placeholder='Fill me with text.'>
<!--
Resizing text, text box
A little experiment to improve the user experience of the text box by:
1. Using a focusing shadow effect when the box is being used
2. Shrinking the font size when the user fills the box... so you can get in those few extra characters.
by Tim Holman - @twholman
Details at: http://tholman.com/experiments/javascript/resizer/
-->
function Resizer( element ) {
var inputBox = element;
var cssRules = window.getComputedStyle(inputBox);
var maxFontSize = parseInt(cssRules.getPropertyValue("font-size"));
var minFontSize = 11; // 11 is pretty damn small!
var currentFontSize = maxFontSize;
var maxScrollWidth = parseInt(cssRules.getPropertyValue("width"))
var fontFamily = cssRules.getPropertyValue("font-family");
var currentText = inputBox.value;
// Canvas used to check text widths.
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var initialize = function() {
inputBox.oninput = onUpdate;
}
var onUpdate = function(event) {
var width;
// Some text has been deleted!
if (inputBox.value.length < currentText.length) {
width = checkTextWidth(inputBox.value, currentFontSize + 1);
while (width < maxScrollWidth && currentFontSize < maxFontSize) {
currentFontSize += 1;
inputBox.style.fontSize = currentFontSize + 'px';
width = checkTextWidth(inputBox.value, currentFontSize + 1);
}
currentText = inputBox.value;
return;
}
var width = checkTextWidth(inputBox.value, currentFontSize);
console.log( currentFontSize, maxScrollWidth )
// Shrink
while (currentFontSize > minFontSize && width > maxScrollWidth) {
currentFontSize -= 1;
inputBox.style.fontSize = currentFontSize + 'px';
width = checkTextWidth(inputBox.value, currentFontSize);
}
currentText = inputBox.value;
}
var checkTextWidth = function(text, size) {
context.font = size + "px " + fontFamily;
if (context.fillText) {
console.log("THIS: ",context.measureText(text).width )
return context.measureText(text).width;
} else if (context.mozDrawText) {
console.log("THIS: ",context.mozMeasureText(text) )
return context.mozMeasureText(text);
}
}
// Initialize the auto adapt functionality.
initialize();
}
Resizer( document.getElementById( 'resizer' ) );
/*
CSS
- None of this is required for resizer to work.
*/
body {
background-image: url(http://i.imgur.com/iWUM6.jpg);
background-size: 100%;
}
input[type='text']{
color: #333;
width: 280px;
height: 38px;
margin-left: -150px;
margin-top: -19px;
position: fixed;
left: 50%;
top: 50%;
padding-left: 10px;
padding-right: 10px;
transition: box-shadow 320ms;
box-shadow: 0px 0px 8px 10px rgba(0,0,0,0.1);
border-radius: 3px;
font-size: 18px;
border: 0px;
}
input[type='text']:focus {
outline: 0px;
outline-offset: 0px;
box-shadow: 0px 0px 1px 5px rgba(0,0,0,0.12);
}
input:-moz-placeholder {
color: #cccccc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment