penguin/AstroJS

Javascript framework for my blog

botanjs/src/Astro/Mechanism/CharacterCloud.js

raw ยท 1960 bytes

(function(){
	var ns = __namespace( "Astro.Mechanism.CharacterCloud" );

	/** @type {Dandelion} */
	var Dand = __import( "Dandelion" );

	var CharacterCloud = {};

	// Character cloud creates a cloud of character with randomized properties
	var create = function( ch, char_class, size, cloudRange, charSize )
	{
		var cloudMap = Dand.wrapc( "characterCloud" )
			, charElmt
			, rx, ry, rs
			, cs = charSize || 15
			, l_l = cloudRange.lowerLimit
			// vertical range
			, vr = cloudRange.upperLimit - l_l
			, lf_l = cloudRange.leftLimit
			// horizontal range
			, hr = cloudRange.rightLimit

			, charStack = ( ch instanceof Array );
		;


		// The loop is CPU intensive
		// we need to focus on reducing the CPU usage
		if( charStack )
		{
			var l = ch.length;
			for(var i = 0; i < size; i ++)
			{
				rx = String( lf_l + Math.random()*hr ) + "%";
				ry = String( l_l + Math.random()*vr ) + "%";
				rs = Math.random()*cs;
				charElmt = Dand.wrap('span', null, char_class, ch[Math.floor(Math.random()*l)]);

				var cE_style = charElmt.style;

				cE_style.position = "absolute";
				cE_style.left = rx;
				cE_style.top = ry;
				// cE_style.opacity = Math.random() + "";
				cE_style.fontSize = rs + "em";
				cE_style.marginTop = marginLeft = String( -0.5*rs ) + "em";

				cloudMap.appendChild(charElmt);
			}
		}
		else
		{
			for(var i = 0; i < size; i ++)
			{
				rx = String(lf_l + Math.random()*hr) + "%";
				ry = String(l_l + Math.random()*vr) + "%";
				rs = Math.random()*15;
				charElmt = Dand.wrap('span', null, char_class, ch);

				var cE_style = charElmt.style;

				cE_style.position = "absolute";
				cE_style.left = rx;
				cE_style.top = ry;
				// cE_style.opacity = String(Math.random());
				cE_style.fontSize = String(rs) + "em";
				cE_style.marginTop = marginLeft = String( -0.5*rs ) + "em";

				cloudMap.appendChild( charElmt );
			}
		}
		
		return cloudMap;
	};

	ns[ NS_EXPORT ]( EX_FUNC, "create", create );
})();