TextFX functions from Quest

You can add this to a JS file (or to the game itself) to add this stuff to a Squiffy game:

    var $ = jQuery;
	(function ($) {

		function shuffle(a) {
			var i = a.length, j;
			while (i) {
				var j = Math.floor((i--) * Math.random());
				var t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}

		function randomAlphaNum() {
			var rnd = Math.floor(Math.random() * 62);
			if (rnd >= 52) return String.fromCharCode(rnd - 4);
			else if (rnd >= 26) return String.fromCharCode(rnd + 71);
			else return String.fromCharCode(rnd + 65);
		}

		$.fn.rot13 = function () {
			this.each(function () {
				$(this).text($(this).text().replace(/[a-z0-9]/ig, function (chr) {
					var cc = chr.charCodeAt(0);
					if (cc >= 65 && cc <= 90) cc = 65 + ((cc - 52) % 26);
					else if (cc >= 97 && cc <= 122) cc = 97 + ((cc - 84) % 26);
					else if (cc >= 48 && cc <= 57) cc = 48 + ((cc - 43) % 10);
					return String.fromCharCode(cc);
				}));
			});
			return this;
		};

		$.fn.scrambledWriter = function () {
			this.each(function () {
				var $ele = $(this), str = $ele.text(), progress = 0, replace = /[^\s]/g,
					random = randomAlphaNum, inc = 3;
				$ele.text('');
				var timer = setInterval(function () {
					$ele.text(str.substring(0, progress) + str.substring(progress, str.length).replace(replace, random));
					progress += inc
					if (progress >= str.length + inc) clearInterval(timer);
				}, 100);
			});
			return this;
		};

		$.fn.typewriter = function (speed) {
			this.each(function () {
				var $ele = $(this), str = $ele.text(), progress = 0;
				$ele.text('');
				var timer = setInterval(function () {
					$ele.text(str.substring(0, progress++) + ((progress & 1) && progress < str.length ? '_' : ''));
					if (progress >= str.length) clearInterval(timer);
				}, speed);
			});
			return this;
		};

		$.fn.unscramble = function (speed, reveal) {
			this.each(function () {
				var $ele = $(this), str = $ele.text(), replace = /[^\s]/,
					state = [], choose = [], random = randomAlphaNum;

				for (var i = 0; i < str.length; i++) {
					if (str.charAt(i).match(replace)) {
						state.push(random());
						choose.push(i);
					} else {
						state.push(str.charAt(i));
					}
				}

				shuffle(choose);
				$ele.text(state.join(''));

				var timer = setInterval(function () {
					var i, r = reveal;
					while (r-- && choose.length) {
						i = choose.pop();
						state[i] = str.charAt(i);
					}
					for (i = 0; i < choose.length; i++) state[choose[i]] = random();
					$ele.text(state.join(''));
					if (choose.length == 0) clearInterval(timer);
				}, speed);
			});
			return this;
		};

	})(jQuery);
	var TextFX = new function() {
		var fxCount = 0;

		function addFx(text, font, color, size) {
			fxCount++;
			var style = "font-family:" + font + ";color:" + color + ";font-size:" + size;
			var html = "<span id=\"fx" + fxCount + "\" style=\"" + style + "\">" + text + " </span><br />";
			squiffy.ui.write(html);
			return $("#fx" + fxCount);
		}

		this.Typewriter = function(text, speed, font, color, size) {
			var el = addFx(text, font, color, size);
			el.typewriter(speed);
		}

		this.Unscramble = function(text, speed, reveal, font, color, size) {
			var el = addFx(text, font, color, size);
			el.unscramble(speed, reveal);
		}
	};    var $ = jQuery;
	(function ($) {

		var shuffle = function(a) {
			var i = a.length, j;
			while (i) {
				var j = Math.floor((i--) * Math.random());
				var t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}

		var randomAlphaNum = function() {
			var rnd = Math.floor(Math.random() * 62);
			if (rnd >= 52) return String.fromCharCode(rnd - 4);
			else if (rnd >= 26) return String.fromCharCode(rnd + 71);
			else return String.fromCharCode(rnd + 65);
		}

		$.fn.rot13 = function () {
			this.each(function () {
				$(this).text($(this).text().replace(/[a-z0-9]/ig, function (chr) {
					var cc = chr.charCodeAt(0);
					if (cc >= 65 && cc <= 90) cc = 65 + ((cc - 52) % 26);
					else if (cc >= 97 && cc <= 122) cc = 97 + ((cc - 84) % 26);
					else if (cc >= 48 && cc <= 57) cc = 48 + ((cc - 43) % 10);
					return String.fromCharCode(cc);
				}));
			});
			return this;
		};

		$.fn.scrambledWriter = function () {
			this.each(function () {
				var $ele = $(this), str = $ele.text(), progress = 0, replace = /[^\s]/g,
					random = randomAlphaNum, inc = 3;
				$ele.text('');
				var timer = setInterval(function () {
					$ele.text(str.substring(0, progress) + str.substring(progress, str.length).replace(replace, random));
					progress += inc
					if (progress >= str.length + inc) clearInterval(timer);
				}, 100);
			});
			return this;
		};

		$.fn.typewriter = function (speed) {
			this.each(function () {
				var $ele = $(this), str = $ele.text(), progress = 0;
				$ele.text('');
				var timer = setInterval(function () {
					$ele.text(str.substring(0, progress++) + ((progress & 1) && progress < str.length ? '_' : ''));
					if (progress >= str.length) clearInterval(timer);
				}, speed);
			});
			return this;
		};

		$.fn.unscramble = function (speed, reveal) {
			this.each(function () {
				var $ele = $(this), str = $ele.text(), replace = /[^\s]/,
					state = [], choose = [], random = randomAlphaNum;

				for (var i = 0; i < str.length; i++) {
					if (str.charAt(i).match(replace)) {
						state.push(random());
						choose.push(i);
					} else {
						state.push(str.charAt(i));
					}
				}

				shuffle(choose);
				$ele.text(state.join(''));

				var timer = setInterval(function () {
					var i, r = reveal;
					while (r-- && choose.length) {
						i = choose.pop();
						state[i] = str.charAt(i);
					}
					for (i = 0; i < choose.length; i++) state[choose[i]] = random();
					$ele.text(state.join(''));
					if (choose.length == 0) clearInterval(timer);
				}, speed);
			});
			return this;
		};

	})(jQuery);
	var TextFX = new function() {
		var fxCount = 0;

		function addFx(text, font, color, size) {
			fxCount++;
			var style = "font-family:" + font + ";color:" + color + ";font-size:" + size;
			var html = "<span id=\"fx" + fxCount + "\" style=\"" + style + "\">" + text + " </span><br />";
			squiffy.ui.write(html);
			return $("#fx" + fxCount);
		}

		this.Typewriter = function(text, speed, font, color, size) {
			var el = addFx(text, font, color, size);
			el.typewriter(speed);
		}

		this.Unscramble = function(text, speed, reveal, font, color, size) {
			var el = addFx(text, font, color, size);
			el.unscramble(speed, reveal);
		}
	};

Now you can do this:

    TextFX.Typewriter("This is a test.", 100, "inherit", "inherit", "inherit");

That's very cool! And it should be put into the next version of Quest.


That's very cool!

Thanks! (But I just copied it from Quest.)


it should be put into the next version of Quest.

It's already in there.

TextFX_Typewriter (text, delay)

image


image


http://docs.textadventures.co.uk/quest/functions/corelibrary/textfx_typewriter.html


Log in to post a reply.

Support

Forums