lettering.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. (function($) {
  2. function injector(t, splitter, klass, after) {
  3. var a = t.text().split(splitter);
  4. var html = "",
  5. clazz = "clock",
  6. letterSize = 32,
  7. count = 0,
  8. width = 0,
  9. height = 0,
  10. lineSize,
  11. letter, iLetter,
  12. i, x, y
  13. forceSmall = false;
  14. if( typeof customClazz != "undefined" ) {
  15. clazz = " clock " + customClazz;
  16. }
  17. if (a.length) {
  18. $(a).each(
  19. function(i, letter) {
  20. iLetter = (letter.charCodeAt(0) - 97);
  21. if( letter === " " ) {
  22. html += "<span class='blank'></span>";
  23. width += 16;
  24. count = count + 1;
  25. } else {
  26. if( letter.charCodeAt(0) > 47 && letter.charCodeAt(0) < 58 ) {
  27. letterSize = 32;
  28. if( forceSmall ) {
  29. letterSize = 16;
  30. }
  31. html += "<span class='" + clazz + (forceSmall ? "small" : "") + "' style='top: -50%;background-position: -" + ( parseInt( letter, null ) * letterSize) + "px -" + (forceSmall > -1 ? 128 : 0) +"px'></span>";
  32. count = count + 1;
  33. } else {
  34. if( ( letter.charCodeAt(0) >= 'a'.charCodeAt(0) && letter.charCodeAt(0) <= 'z'.charCodeAt(0)) ) {
  35. if( height < 16 ) {
  36. height = 16;
  37. }
  38. width += 16;
  39. lineSize = 20;
  40. x = (iLetter % lineSize) * 16;
  41. y = Math.floor(iLetter / lineSize) * 16 + 144;
  42. html += "<span class='" + clazz + " small' style='background-position: -" + x + "px -" + y + "px'></span>";
  43. count = count + 1;
  44. } else {
  45. if( letter.charCodeAt(0) >= 'A'.charCodeAt(0) && letter.charCodeAt(0) <= 'Z'.charCodeAt(0)) {
  46. iLetter = letter.charCodeAt(0) - 'A'.charCodeAt(0);
  47. if( height < 32 ) {
  48. height = 32;
  49. }
  50. width += 32;
  51. lineSize = 10;
  52. x = (iLetter % lineSize) * 32;
  53. y = Math.floor(iLetter / lineSize) * 32 + 32;
  54. html += "<span class='" + clazz + "' style='background-position: -" + x + "px -" + y + "px'></span>";
  55. count = count + 1;
  56. }
  57. }
  58. }
  59. }
  60. });
  61. t.empty().append(html);
  62. }
  63. }
  64. $.fn.lettering = function() {
  65. return injector( $(this), '', 'char', '' ); // always
  66. // pass
  67. // an
  68. // array
  69. };
  70. })(jQuery);