954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Make Binary Dots From Strings Using Javascript

I was looking at a page called Javascript: Convert Strings to Binary (and representing in a nerdy way!) where you can convert a String into binary dots using Javascript and I loved that, but how I can do that? Since the code isn't for making that grid...

Nathan Campos
Junior Poster
190 posts since Jul 2009
Reputation Points: 33
Solved Threads: 6
 

Nathan,

View the page's source and search for "fillsdfkalsjdfak". You will find the function that writes the dots on the page.

You will also need a small stylesheet, slightly higher up in the source. Search for "img.zerosafdfasd".

You might like to grab your own copy of the dot image and store it locally. Then edit the function to use the local copy with a relative url, rather than the original over the internet.

Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

I've tried this it worked in part, since the dots wont get smaller there are just a lot of big dots

Nathan Campos
Junior Poster
190 posts since Jul 2009
Reputation Points: 33
Solved Threads: 6
 

Nathan,

The small dots are achieved with css.

If they are all large, then either the stylesheet is not properly in place or the class="....." statement in the code is not correct.

After posting last night, I played with the code and ended up with this implementation, which is a bit more complex but a lot more fun:

<!DOCTYPE html>
<head>
<title>ASCII dots</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#asciiPattern img.zero { width: 6px; height: 6px; padding: 5px; }
</style>
<script type="text/javascript">
onload = function(){
	function convertToDots(elID, str){
	  var i,j,d,row,rowArray,clss,el = document.getElementById(elID);
	  el.innerHTML = '';
	  for(i=0; i<str.length; i++){
	    d = str.charCodeAt(i);
	    row = document.createElement('div');
	    el.appendChild(row);
	    rowArray = [];
	    for(j=0; j<8; j++) {
	      clss = (d%2 == 0) ? "zero" : ""; 
	      d = Math.floor(d/2);
	      rowArray.unshift('<img class="'+clss+'" src="images/dotaj8.png" />');
	    }
	    row.innerHTML = rowArray.join('');
	  }
	}
	var txt = document.getElementById("txt");
	var goButton = document.getElementById("goButton");
	goButton.onclick = txt.onkeyup = function(){
		convertToDots('asciiPattern', txt.value);
	};
	goButton.onclick();
}
</script>
</head>
<body>

<div id="controls">
	<input type="text" id="txt" value="Welcome" />&nbsp;<button id="goButton">Go</button>
</div>
<div id="asciiPattern"></div>

</body>
</html>

The "go" button is included for any browser that does not support the onkeyup event.Airshow

Airshow
WiFi Lounge Lizard
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: