Click here to Skip to main content
Licence MIT
First Posted 2 Apr 2007
Views 32,615
Downloads 150
Bookmarked 7 times

See your name in Braille

By | 2 Apr 2007 | Article
Simple Javascript to display Braille in web pages using pictures

Introduction

This article offers some JavaScript to display Braille in your web pages. It can be used to familiarize sighted people with the Braille system, not for real Braille transcription (that would be a much bigger project).

In its simplest form (Grade 1) Braille is just a different character set:

Screenshot - BrailleAlphabet.gif

Displaying Braille using pictures and JavaScript

From the table above, we can easily display a Braille translation of any text by using one picture per character. It is even easier when we call each picture by the name of the character it represents.

The following quick and dirty JavaScript will do the trick.

function SimpleBraille(message) 
{ 
    var myHTML = ""; 
    for (var i=0; i<message.length; i++) 
    {
        if (message.charAt(i)==" ")
            myHTML += "<IMG  SRC=\"braillepix/sp.gif\">";
        else 
            myHTML += "<IMG SRC=\"braillepix/" + message.charAt(i).
                toLowerCase() + ".gif\">";
    } 
    return myHTML;
}

Improving the JavaScript

Of course, this was an over-simplification to demonstrate the way it works. Let's now make a few improvements the code.

There are a few extra rules we should add to our script

  • Uppercases must be prefixed by a special Braille character
  • Numbers are displayed using "a" for "1", "b" for "2"... prefixed with a special character
  • Punctuation signs use more special characters

While we are at it, we can also optimize the code

  • Let's add the "ALT" attribute in the generated "IMG" tags, to see the translation of each character on mouse-over.
  • Let's use and array, push and join instead of string concatenation, so it will be much faster.

The improved JavaScript function is now much longer but will produce more accurate Braille.

function Braille(message) 
{ 
    var brailleArr = []; 
    var myChar, prevCharNum, inQuote

        function BrailleChar(bPix,bAlt) 
    {
        return ["<IMG HEIGHT=27 WIDTH=19 vspace=5 hspace=1 SRC=\"braillepix/", 
            bPix, ".gif\" ALT=\"", bAlt, "\">"].join("");
    }

    for (var i=0; i<message.length; i++) 
    {
        myChar = message.charAt(i);
        if ((myChar>="a") && (myChar<="z")) 
        {       // a to z
            brailleArr.push(BrailleChar(myChar, myChar));
            prevCharNum = false;            
        } else if((myChar>="A") && (myChar<="Z")) 
        {       // A to Z
            brailleArr.push(BrailleChar("cap", "Caps"));
            brailleArr.push(BrailleChar(myChar, myChar));            
            prevCharNum = false;         
        } else if((myChar>"0") && (myChar<="9")) 
        {
            if (!prevCharNum)
                brailleArr.push(BrailleChar("num", "Number"));
            brailleArr.push(BrailleChar(String.fromCharCode(myChar.
                charCodeAt(0) + 48), myChar)); 
            prevCharNum = true;            
        } else 
        {
            switch (myChar) 
            {
            case " ": 
                brailleArr.push(BrailleChar("sp", "Space"));
                prevCharNum = false;
                break;
            case "0":
                if (!prevCharNum)
                    brailleArr.push(BrailleChar("num", "Number")); 
                brailleArr.push(BrailleChar("j", "0"));      
                prevCharNum = true;
                break;
            case "\n":
                brailleArr.push("<br><br>");
                nbCharsInLine = -1;
                prevCharNum = false;
                break;
            case ".":
                if (prevCharNum)
                    brailleArr.push(BrailleChar("dec", ".")); 
                else
                    brailleArr.push(BrailleChar("period", "."));  
                break;
            case "$":
                brailleArr.push(BrailleChar("period", "$"));
                prevCharNum = false;
                break;
            case "%":
                brailleArr.push(BrailleChar("col", "%"));
                brailleArr.push(BrailleChar("p", ""));
                prevCharNum = false;
                break;
                case "'":
                    brailleArr.push(BrailleChar("qs", "'")); 
                    prevCharNum = false;
                    break;
            case ",":
                brailleArr.push(BrailleChar("comma", ","));
                prevCharNum = false;
                break;
            case "?":
                brailleArr.push(BrailleChar("qu", "?")); 
                prevCharNum = false;
                break;
            case "(":
            case ")":
                brailleArr.push(BrailleChar("par", "parenthesis")); 
                prevCharNum = false;
                break;
            case "*":
                brailleArr.push(BrailleChar("ast", "*"));
                brailleArr.push(BrailleChar("ast", "*")); 
                prevCharNum = false;
                break;
                case "//":
                    brailleArr.push(BrailleChar("sla", "//")); 
                    prevCharNum = false;
                    break;
            case "!":
                brailleArr.push(BrailleChar("ex", "!")); 
                prevCharNum = false;
                break;
                case "'": 
                    if (inQuote)
                        brailleArr.push(BrailleChar("qc", "Close Quote")); 
                    else
                        brailleArr.push(BrailleChar("qo", "Open Quote"));  
                    inQuote = !inQuote;
                    prevCharNum = false;
                    break;
            case ":":
                brailleArr.push(BrailleChar("col", ":"));
                prevCharNum = false;
                break;
            case ";":
                brailleArr.push(BrailleChar("sc", ";")); 
                prevCharNum = false;
                break;
            case "[":
                brailleArr.push(BrailleChar("cap", "["));
                brailleArr.push(BrailleChar("par", ""));
                break;
            case "]":
                brailleArr.push(BrailleChar("par", "]"));
                brailleArr.push(BrailleChar("qs", "")); 
                break;
            }
        }
    }
    return brailleArr.join("");
}

I'm not a Braille expert, but I know there are other rules that could be added. There is still room for improvement.

Using the code

The JavaScript is rather straight forward and simple enough to work on any browser. What took a while was making the pictures of all the different Braille characters, but don't worry these are included in the download, and they even come in three different flavors. Simply change the directory name in the JavaScript to use the set of pictures you prefer.

Screenshot - braille-black.gifBlack

Screenshot - braille-3d.gif3D

Screenshot - braille-big.gifBig

Using the Braille function, we can display the classic "Hello World" as follow:

document.write(Braille("Hello World"));

Otherwise, you may use it to show your users how their name looks like in Braille.

Screenshot - seeyourname.gif

Try this code live at EvolUtility

Eventually, if you really want to learn Braille, I recommend starting with the book Braille for the Sighted from Schneider, and Kathy Kifer. Enjoy.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Olivier Giulieri

Other

United States United States

Member

I'm a software engineer living in California. I like to work on UI and databases. What I really enjoy is to describe UI in metadata, store that metadata (outside of the code) in a database or in XML, and then dynamically generate screens at run-time... which I do with my open source Evolutility.
 
Articles on Evolutility:
 
Beside Evolutility.org, my other creative project is ChakraDesign.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFun w/ Braille - Fun w/ Morse PinmemberEvoluteur22:07 2 Jun '10  
Questionhtml code PinmemberNohamanuna19:50 20 Feb '10  
GeneralReversed Braille PinmemberGuy Winters14:12 26 Jan '08  
GeneralRe: Reversed Braille PinmemberOlivier Giulieri12:14 27 Jan '08  
QuestionA question on the application's usability Pinmembervolkan.ozcelik21:59 11 Apr '07  
AnswerRe: A question on the application's usability PinmemberOlivier Giulieri7:23 12 Apr '07  
AnswerRe: A question on the application's usability PinmemberOlivier Giulieri19:31 12 Apr '07  
Also, for information, it is possible to emboss Braille on regular paper too but using thicker paper makes it easier to read and it last longer.
GeneralThank you! Pinmemberaraud18:43 9 Apr '07  
GeneralRe: Thank you! PinmemberOlivier Giulieri20:42 9 Apr '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 2 Apr 2007
Article Copyright 2007 by Olivier Giulieri
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid