![]() |
Web Development »
Client side scripting »
General
Intermediate
License: The MIT License
See your name in BrailleBy Olivier GiulieriSimple Javascript to display Braille in web pages using pictures |
Javascript, XML, Windows, .NET, ASP.NET, WebForms, Ajax, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
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:
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;
}
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
While we are at it, we can also optimize the code
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.
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.
Black
3D
Big
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.
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 2 Apr 2007 Editor: Sean Ewington |
Copyright 2007 by Olivier Giulieri Everything else Copyright © CodeProject, 1999-2009 Web13 | Advertise on the Code Project |