views:

58

answers:

2

Hi

I'm trying to use Jquery/Javascript to mimic a broken typewriter font (since I could not find one). But I want to make it random which letter gets broken. I was able to split the string of the id that I wanted and use a bit of code I found to get a random number between 0 and the total length of the string. What I'm having problem with now is doing something with that specific character. I want to push it down or up a few pixels. I was trying to give it a class so I could add some margin or padding, but it doesn't work. So I'm stuck where I am now.

here's the page, I'm trying to do it to the word "ABOUT":
http://www.franciscog.com/bs/about.php

here's the script:

<script type="text/javascript">

        function randomXToY(minVal,maxVal,floatVal)
            {
              var randVal = minVal+(Math.random()*(maxVal-minVal));
              return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
            }


        var str = $('#typehead').text();
                var strcnt = str.length;
        var exploded = str.split('');
        var rdmltr =randomXToY(0,strcnt); 
        var theLetter = exploded[rdmltr];
        theLetter.addClass("newClass");
        var toffset = $('.newClass').offset();
        alert(toffset.left + "," + toffset.top);

     </script>
+4  A: 

EDIT: Updated to ensure that the matched character is not a space character, and added a little style suggested by @abelito.

How about this: http://jsfiddle.net/cgXa3/4/

function randomXToY(minVal,maxVal,floatVal){
    var randVal = minVal+(Math.random()*(maxVal-minVal));
    return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}


var exploded = $('#typehead').text().split('');
var rdmltr = randomXToY(0,exploded.length);

    // Make sure we don't get a space character
while(exploded[rdmltr] == ' ') {
    rdmltr = randomXToY(0,exploded.length);
}
    // Wrap the letter with a span that has the newClass
    //   and update it in the array
exploded[rdmltr] = '<span class="newClass">' + exploded[rdmltr] + '</span>';

    // Update the content
$('#typehead').html(exploded.join(''));
var toffset = $('.newClass').offset();
alert(toffset.left + "," + toffset.top);​

Update: If you want to apply it to several: http://jsfiddle.net/cgXa3/5/

patrick dw
Really simple, really good. @OP: position: relative; top: -1px; in the CSS
abelito
@abelito - Thanks. Here's a new version that adds some style like you suggested, also ensures that the character match is not a space character. http://jsfiddle.net/cgXa3/3/
patrick dw
this worked beautifully! and so simple too. thank you.
Francisc0
A: 

I like Patrick's answer, but as an alternative I would alter the same letter throughout the text. And maybe rotate it a tiny bit as well (although this won't work in IE). I made a demo which I forked off of Patrick's.

CSS

.newClass {
 left: 0px;
 top: -1px;
 color: red;
 position:relative;
 -webkit-transform: rotate(-5deg); 
 -moz-transform: rotate(-5deg); 
}

Code

function randomLetter(cased){
 // case = true for uppercase, false for lowercase
 var base = (cased) ? 65 : 97;
 // convert HTML escape code into a letter
 var rand = $('<span>&#' + parseInt(base+(Math.random()*25),10) + ';</span>');
 return rand.text();
};

$(document).ready(function(){
 var ltr = randomLetter(false);
 var reg = new RegExp( ltr, 'g');
 $('#typehead').html(function(i,html){
  return html.replace(reg, '<span class="newClass">' + ltr + '</span>');
 });
});

Update: This is the code needed to apply to multiple h1 tags (updated demo):

function randomXToY(minVal,maxVal,floatVal){
 var randVal = minVal+(Math.random()*(maxVal-minVal));
 return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}

$('.typehead').each(function() {                  
 //access the text and characters within the tag with the id typehead 
 var exploded = $(this).text().split('');
 var rdmltr = randomXToY(0,exploded.length);

 // Make sure we don't get a space character or undefined
 while(exploded[rdmltr] == ' ' || exploded[rdmltr] == undefined) {
  rdmltr = randomXToY(0,exploded.length);
 }

 // Wrap the letter with a span that has the new class brokenType
 //   and update it in the array
 exploded[rdmltr] = '<span class="brokenType">' + exploded[rdmltr] + '</span>'; 

 // Update the content
 $(this).html(exploded.join(''));
});
fudgey
I like this idea but the text in the document is not going to be in a typewriter-like font, only the header tags will be, most likely just the h1 tags. But thank you for the code!
Francisc0
i did add the rotation by the way. thanks again
Francisc0
No Problem! Btw, I found a problem with the code (use 25 instead of 26) and a better way to get a random letter here: http://www.codehouse.com/javascript/tips/random_letter/
fudgey
turns out i'm not finished in the end. I had tested the code on just 1 tag (changed the id to a class). But it doesn't work properly on multiple tag/classes of the same type. I tried doing a .each() jquery loop but that did nothing. I still want to keep it to 1 random letter in the h1 tag, but in all the h1 tags. see my problem here:www.franciscog.com/bs/zine.phpso i guess what I need it to do is basically scan the page from top to bottom and for each h1 tag if finds, run the function separately on each h1 as it encounters it.
Francisc0
I can't get that url to work (404 error), but after looking at your site I think adding a mousewheel function to scroll your content horizontally would be nice :P... check out this answer: http://stackoverflow.com/questions/2834004/slider-with-buttons-how-to-improve/2834891#2834891
fudgey
d'oh! i forgot one letter, it's zines, not zine. Now this should work: www.franciscog.com/bs/zines.php Notice how it repeats "The Zinesh1 test", well that's only two h1 tags, one placed above the links("The Zines") and one placed below "h1 test." But the code I am using now combines them.thanks for that slider code, I was looking for something like that just the other day.
Francisc0
Ok, I've updated my answer above and Patrick's demo.
fudgey
so I just had the wrong selector in the beginning .each statement and should've used "this" within in the function. Damn I was racking my brain on that. Thank you so much!
Francisc0