Click here to Skip to main content
Click here to Skip to main content

See your name in Braille

, 2 Apr 2007 MIT
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Olivier Giulieri

United States United States
I'm a UI engineer for a startup in California. What I really enjoy is to build tools to describe UI in metadata, store that metadata (outside of the code) in a database, XML or JSON, and then dynamically generate the UI at run-time based on that metadata... which I do with my open source project Evolutility.

My articles on the topic:
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralBraille Poland Pinmemberrobert.michal29-Jun-14 6:00 
GeneralRe: Braille Poland PinmemberOlivier Giulieri8-Jul-14 10:49 
QuestionConvert from braille to text using matlab Pinmemberdinh van phuc21-Jan-13 16:30 
QuestionThank you!! PinmemberMember 960893318-Nov-12 3:33 
AnswerRe: Thank you!! PinmemberOlivier Giulieri18-Nov-12 18:18 
GeneralFun w/ Braille - Fun w/ Morse PinmemberEvoluteur2-Jun-10 23:07 
Questionhtml code PinmemberNohamanuna20-Feb-10 20:50 
GeneralReversed Braille PinmemberGuy Winters26-Jan-08 15:12 
GeneralRe: Reversed Braille PinmemberOlivier Giulieri27-Jan-08 13:14 
GeneralRe: Reversed Braille PinmemberMember 1109858521-Sep-14 8:25 
QuestionA question on the application's usability Pinmembervolkan.ozcelik11-Apr-07 22:59 
AnswerRe: A question on the application's usability PinmemberOlivier Giulieri12-Apr-07 8:23 
AnswerRe: A question on the application's usability PinmemberOlivier Giulieri12-Apr-07 20:31 
GeneralThank you! Pinmemberaraud9-Apr-07 19:43 
GeneralRe: Thank you! PinmemberOlivier Giulieri9-Apr-07 21:42 

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.

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