<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scriptNode &#187; yui</title>
	<atom:link href="http://scriptnode.com/tag/yui/feed/" rel="self" type="application/rss+xml" />
	<link>http://scriptnode.com</link>
	<description>Tips and tricks for web developers.</description>
	<lastBuildDate>Tue, 01 Sep 2009 18:46:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>twittertyper</title>
		<link>http://scriptnode.com/article/twittertyper/</link>
		<comments>http://scriptnode.com/article/twittertyper/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 01:11:51 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[just-for-fun]]></category>
		<category><![CDATA[lab]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=157</guid>
		<description><![CDATA[



I unfortunately am not allowed to participate, but in anticipation of Yahoo!&#8217;s Hack Day 2008, I put together a little app I&#8217;ll hopefully be able to port to YAP: twittertyper!


The concept is very simple. twittertyper displays the latest tweet from Twitter. To read the next one, first type out the tweet. The text is highlighted [...]]]></description>
			<content:encoded><![CDATA[<p class="before">
<a href="/lab/twittertyper/"><img alt="twittertyper" src="http://scriptnode.com/lab/twittertyper/logo.png"/></a>
</p>
<p>
I unfortunately am not allowed to participate, but in anticipation of Yahoo!&#8217;s <a href="http://developer.yahoo.net/hackday/">Hack Day 2008</a>, I put together a little app I&#8217;ll hopefully be able to port to <a href="http://developer.yahoo.com/searchmonkey/" title="Yahoo! Application Platform">YAP</a>: <a href="/lab/twittertyper/">twittertyper</a>!
</p>
<p>
The concept is very simple. <strong>twittertyper</strong> displays the latest tweet from <a href="http://twitter.com">Twitter</a>. To read the next one, first <strong>type out the tweet</strong>. The text is highlighted as you go. If you come across a character your keyboard (or operating system) won&#8217;t let you type, just press <code>Esc</code> to move on to the next character.
</p>
<p>
<strong>twittertyper</strong> currently just pulls in from the <a href="http://twitter.com/public_timeline">public timeline</a>; no authentication is supported to pull from <em>your</em> timeline (maybe in a future version). This mini-app is built on <a href="http://developer.yahoo.com/yui/" title="Yahoo! User Interface Library">YUI</a>. Source code (uncompressed) is <a href="/lab/twittertyper/tt.js">free to peruse</a>.
</p>
<p>
<a href="/lab/twittertyper/">Try it out!</a>
</p>
<p>
<em><strong>Note:</strong> If you use Twitter, I&#8217;m sure you&#8217;re used to it being down. Errors happen often, but <strong>twittertyper</strong> attempts to handle them somewhat gracefully. Usually a page reload should suffice.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/twittertyper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shark!</title>
		<link>http://scriptnode.com/article/shark/</link>
		<comments>http://scriptnode.com/article/shark/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 10:56:42 +0000</pubDate>
		<dc:creator>Matt Hackett</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[just-for-fun]]></category>
		<category><![CDATA[lab]]></category>
		<category><![CDATA[script-sunday]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://scriptnode.com/?p=48</guid>
		<description><![CDATA[
Well, this year&#8217;s Shark Week is over. In its memory, I offer up a script that makes sharks swim around on the screen. You should see a few sharks swimming about right now! Here&#8217;s the source code:


/**
 * Global Shark constructor
 * Options for ops: dir, speed, url, x, y, zIndex
 * @constructor
 * @public
 [...]]]></description>
			<content:encoded><![CDATA[<p>
Well, this year&#8217;s <a href="http://discovery.com/sharkweek/">Shark Week</a> is over. In its memory, I offer up a script that makes sharks swim around on the screen. You should see a few sharks swimming about right now! Here&#8217;s the source code:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
/**<br />
 * Global Shark constructor<br />
 * Options for ops: dir, speed, url, x, y, zIndex<br />
 * @constructor<br />
 * @public<br />
 */<br />
Shark = function(ops) {</p>
<p>	var Y = YAHOO.util,<br />
		DEFAULT_SPEED = 2,<br />
		DEFAULT_ZINDEX = 1000,<br />
		DELAY = 25,<br />
		LEFT = &#8216;l&#8217;,<br />
		RIGHT = &#8216;r&#8217;,<br />
		SCREEN_WIDTH = Y.Dom.getViewportWidth(),<br />
		SCREEN_HEIGHT = Y.Dom.getViewportHeight(),<br />
		SHARK_WIDTH = 242,<br />
		SHARK_HEIGHT = 82;</p>
<p>	var interval,<br />
		that = this;</p>
<p>	that.active = true;<br />
	that.dir = LEFT;<br />
	that.el = {};<br />
	that.onOutLeft = function(){};<br />
	that.onOutRight = function(){};<br />
	that.x = 0;<br />
	that.y = 0;</p>
<p>	/**<br />
	 * Initializes the shark variables<br />
	 * @private<br />
	 */<br />
	var init = function() {</p>
<p>		ops = ops || {};<br />
		ops.speed = ops.speed || rand(DEFAULT_SPEED, (DEFAULT_SPEED * 2));<br />
		ops.url = ops.url || &#8216;./&#8217;;<br />
		ops.zIndex = ops.zIndex || DEFAULT_ZINDEX;</p>
<p>		if (ops.dir) {<br />
			that.dir = ops.dir;<br />
		} else {<br />
			that.dir = [LEFT, RIGHT][rand(0, 1)];<br />
		}</p>
<p>		if (ops.x === undefined) {<br />
			if (that.dir == LEFT) {<br />
				that.x = (SCREEN_WIDTH &#8211; 1);<br />
			} else {<br />
				that.x = -SHARK_WIDTH;<br />
			}<br />
		} else {<br />
			that.x = ops.x;<br />
		}</p>
<p>		that.el = document.createElement(&#8217;div&#8217;);<br />
		that.el.className = &#8217;shark&#8217;; // Doesn&#8217;t do anything. Just in case you want to use it<br />
		that.el.style.position = &#8216;absolute&#8217;;<br />
		that.el.style.height = SHARK_HEIGHT + &#8216;px&#8217;;<br />
		that.el.style.zIndex = ops.zIndex;</p>
<p>		Y.Event.addListener(that.el, &#8216;click&#8217;, onclick);</p>
<p>		document.body.appendChild(that.el);<br />
		reset();</p>
<p>		if (ops.y !== undefined) {<br />
			that.y = ops.y<br />
			plot();<br />
		}</p>
<p>		that.start();</p>
<p>	};</p>
<p>	/**<br />
	 * Kills the shark<br />
	 * @param {Boolean} fade Whether to fade the shark in or not<br />
	 */<br />
	that.kill = function(fade) {</p>
<p>		that.active = false;</p>
<p>		var done = function() {<br />
			document.body.removeChild(that.el);<br />
			stop();<br />
		};</p>
<p>		if (fade) {</p>
<p>			var anim = new Y.Anim(that.el, {opacity : {to : 0}}, 0.5);</p>
<p>			anim.onComplete.subscribe(done);<br />
			anim.animate();</p>
<p>		} else {<br />
			done();<br />
		}</p>
<p>	};</p>
<p>	/**<br />
	 * Move the shark<br />
	 */<br />
	var move = function() {</p>
<p>		if (!that.active) {<br />
			return;<br />
		}</p>
<p>		if (that.dir == LEFT) {</p>
<p>			that.x -= ops.speed;</p>
<p>			if (that.x &lt; -SHARK_WIDTH) {<br />
				that.dir = RIGHT;<br />
				that.onOutLeft();<br />
				reset();<br />
			}</p>
<p>		} else {</p>
<p>			that.x += ops.speed;</p>
<p>			if (that.x &gt; (SCREEN_WIDTH &#8211; 1)) {<br />
				that.dir = LEFT;<br />
				that.x = (SCREEN_WIDTH &#8211; 1);<br />
				that.onOutRight();<br />
				reset();<br />
			}</p>
<p>		}</p>
<p>		if (that.x &gt; (SCREEN_WIDTH &#8211; SHARK_WIDTH)) {<br />
			that.el.style.width = (SHARK_WIDTH &#8211; (that.x &#8211; (SCREEN_WIDTH &#8211; SHARK_WIDTH))) + &#8216;px&#8217;;<br />
		} else {<br />
			that.el.style.width = SHARK_WIDTH + &#8216;px&#8217;;<br />
		}</p>
<p>		plot();</p>
<p>	};</p>
<p>	/**<br />
	 * Kills the shark. Note: intended to be overwritten<br />
	 */<br />
	var onclick = function() {<br />
		that.onclick();<br />
	};</p>
<p>	/**<br />
	 * Default onclick behavior<br />
	 */<br />
	that.onclick = function() {<br />
		that.kill(true);<br />
	};</p>
<p>	/**<br />
	 * Moves the shark element to the XY coordinates<br />
	 */<br />
	var plot = function() {<br />
		that.el.style.left = that.x + &#8216;px&#8217;;<br />
		that.el.style.top = that.y + &#8216;px&#8217;;<br />
	};</p>
<p>	/**<br />
	 * Returns a random number<br />
	 * @param {Number} from The starting point<br />
	 * @param {Number} to The stopping point<br />
	 * @return {Number} A random number within the range<br />
	 */<br />
	var rand = function(from, to) {<br />
		return (from + Math.floor((to &#8211; from + 1) * Math.random()));<br />
	}; </p>
<p>	/**<br />
	 * Sets the background based on direction and randomizes the Y position<br />
	 */<br />
	var reset = function() {</p>
<p>		that.el.style.background = &#8216;url(&#8217; + ops.url + &#8217;shark_&#8217; + that.dir + &#8216;.gif)&#8217;;<br />
		that.y = rand(0, (SCREEN_HEIGHT &#8211; SHARK_HEIGHT &#8211; 1));</p>
<p>		plot();</p>
<p>	};</p>
<p>	/**<br />
	 * Starts the move interval<br />
	 */<br />
	that.start = function() {</p>
<p>		if (interval) {<br />
			return;<br />
		}</p>
<p>		interval = setInterval(move, DELAY);</p>
<p>	};</p>
<p>	/**<br />
	 * Stops the move interval<br />
	 */<br />
	that.stop = function() {<br />
		if (interval) {<br />
			clearInterval(interval);<br />
			interval = false;<br />
		}<br />
	};</p>
<p>	init();</p>
<p>};<br />
</textarea></p>
<p>
You can create new sharks like this:
</p>
<p><textarea class="javascript" cols="50" name="code" rows="10"><br />
var myShark = new Shark();</p>
<p>// Or setup some basics, like having him swim to the left<br />
var anotherShark = new Shark({dir : &#8216;l&#8217;});<br />
anotherShark.onclick = function() {<br />
alert(&#8221;I&#8217;m a shaaaaaaark!&#8221;);<br />
myShark.kill(true); // Kill it with a fade<br />
};</p>
<p>// You may also want to host the shark images yourself:<br />
var hostedShark = new Shark({url : &#8216;http://mydomain.com/shark/&#8217;});<br />
</textarea></p>
<p>
The last example above shows setting a custom <acronym title="Uniform Resource Locator">URL</acronym> for the shark images. You can download those here:
</p>
<p>
<a href="/lab/shark/shark_l.gif"><img alt="Shark!" src="/lab/shark/shark_l.gif"/></a><br />
<a href="/lab/shark/shark_r.gif"><img alt="Shark!" src="/lab/shark/shark_r.gif"/></a>
</p>
<p>
<a class="after" href="/lab/shark/"><img alt="Shark!" src="/assets/img/shark_thumbnail.jpg"/></a><br />
There are a few different events you can subscribe to including <code>onclick</code>, <code>onOutLeft</code> and <code>onOutRight</code>, which I think are pretty self-explanatory.<br />
Using these events, I put together <a href="http://scriptnode.com/lab/shark/">a very simple game</a> involving sharks, clicking and hopelessly trying to save a doomed diver. The ocean graphics came from one of my <a href="http://csszengarden.com/?cssfile=http://www.css-praxis.de/cssocean/zenocean.css">favorite CSS Zen Garden examples</a>.
</p>
<p>
The shark script requires <a href="http://developer.yahoo.com/yui/">YUI</a> (Animation, DOM and Event). I&#8217;ve included it in the third download option below:
</p>
<ul>
<li><a href="/assets/js/shark/shark.js">[Download shark.js]</a> (3,747 bytes)</li>
<li><a href="/assets/js/shark/shark-min.js">[Download shark-min.js]</a> (1,550 bytes; compressed)</li>
<li><a href="/assets/js/shark/shark-min-yui.js">[Download shark-min-yui.js]</a> (46k; compressed with YUI)</li>
</ul>
<p>
I&#8217;d love to see if anybody else has cool ideas of what to do with the shark script!
</p>
<p><script src="/assets/js/shark/shark-min-yui.js" type="text/javascript"></script><br />
<script type="text/javascript">
(function() {
new Shark({url : '/lab/shark/'});
new Shark({url : '/lab/shark/'});
})();
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://scriptnode.com/article/shark/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
