03
Aug
08

Minor JS String Variation

Variation on a theme: String wipe effect where each character appears as a random ascii char.

/*
Inserts random character at specified position in string,
and sets subsequent characters to blank.

toneburst 2008
https://machinesdontcare.wordpress.com
*/

function (__string outputString, __structure outputStruct) main (__string inputString, __number Position)
{
	// Initialise vars
	var result = new Object();
	var outString = "";
	var outArray = new Array();
	
	if(!_testMode) {
		// Set character count to length of string
		var chars = inputString.length;

		// Set threshold start position for character randomisation
		var wipePos = Math.floor(Position * chars);
		
		// Loop through characters
		for (i = 0; i < chars; i++) {
			// Generate random number in 65 > 122 range
			var rand = Math.random() * 57 + 65;
			
			// Accumulate outString by picking from inputString, space character or random ASCII character
			var char;
			if (i < wipePos) {
				// Current char is less than threshold wipe position
				char = inputString.charAt(i);
			} else if (i > wipePos) {
				// Current char is more than threshold wipe position
				char = " ";
			} else {
				// Current char is at threshold wipe position in input string
				// so character will be randomised
				char = String.fromCharCode(rand);
			}
			
			outString += char;
			// Set output array at index i
			outArray[i] = char;
		}

	}
	
	// Set output string + struct
	result.outputString = outString;
	result.outputStruct = outArray;
	// Output
	return result;
}

Updated version of the QTZ uploaded to the Box widget.
‘tb JS-ASCII Demos 1.0.4’

Advertisements

0 Responses to “Minor JS String Variation”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Twitter

August 2008
M T W T F S S
« Jul   Sep »
 123
45678910
11121314151617
18192021222324
25262728293031

Links

Blog Stats

  • 468,884 hits

%d bloggers like this: