<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: timeline code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 05:39:22 GMT</pubDate>
    <description>DZone Snippets: timeline code</description>
    <item>
      <title>TimeLine //JavaScript Class</title>
      <link>http://snippets.dzone.com/posts/show/5293</link>
      <description>&lt;br /&gt;&lt;br /&gt;&lt;a href="http://jsfromhell.com/classes/timeline"&gt;&lt;br /&gt;Simulates the Adobe Flash timeline. You define the amount of frames, the speed in fps (frames per second) and, at each frame passage an event is called, useful for animations.&lt;br /&gt;&lt;br /&gt;[UPDATED CODE AND HELP CAN BE FOUND HERE]&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//+ Jonas Raoni Soares Silva&lt;br /&gt;//@ http://jsfromhell.com/classes/timeline [v1.0]&lt;br /&gt;&lt;br /&gt;TimeLine = function(fps, f){&lt;br /&gt;	this.fps = fps, this.frames = f;&lt;br /&gt;};&lt;br /&gt;with({o: TimeLine, $: TimeLine.prototype}){&lt;br /&gt;	o.timers = [];&lt;br /&gt;	$.running = !!($.current = +(o.timer = $.time = null));&lt;br /&gt;	o.run = function(){&lt;br /&gt;		var o = this;&lt;br /&gt;		o.timer || (o.timer = setInterval(function(){&lt;br /&gt;			for(var h, d = +(new Date), t = o.timers, i = t.length; i--;){&lt;br /&gt;				(!t[i].running || ((d - t[i].time) / (1e3 / t[i].fps) &gt; t[i].current + 1 &amp;&amp;&lt;br /&gt;				t[i].onframe(++t[i].current), t[i].current &gt;= t[i].frames)) &amp;&amp;&lt;br /&gt;				(h = t.splice(i, 1)[0], h.stop(1));&lt;br /&gt;			}&lt;br /&gt;		}, 1));&lt;br /&gt;	};&lt;br /&gt;	$.start = function(c){&lt;br /&gt;		var o = this, t = TimeLine;&lt;br /&gt;		if(o.running) return;&lt;br /&gt;		o.running = true, o.current = c || 0;&lt;br /&gt;		o.time = new Date, o.onstart &amp;&amp; o.onstart();&lt;br /&gt;		if(!o.onframe || o.frames &lt;= 0 || o.fps &lt;= 0)&lt;br /&gt;			return o.stop(1);&lt;br /&gt;		t.timers.push(this), t.run();&lt;br /&gt;	};&lt;br /&gt;	$.stop = function(){&lt;br /&gt;		var o = this;&lt;br /&gt;		o.running = false;&lt;br /&gt;		if(!TimeLine.timers.length)&lt;br /&gt;			TimeLine.timer = clearInterval(TimeLine.timer), null;&lt;br /&gt;		arguments.length &amp;&amp; o.onstop &amp;&amp; o.onstop();&lt;br /&gt;	};&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;&lt;div id="box" style="position: absolute; top: 100px; background: #efe; width: 100px; height: 100px"&gt;25 fps&lt;/div&gt;&lt;br /&gt;&lt;div id="box2" style="position: absolute; top: 300px; background: #ff9; width: 100px; height: 100px"&gt;12 fps&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;TimeLine working together with the ease in quad function.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;Math.ease = function (t, b, c, d) {&lt;br /&gt;	if ((t /= d / 2) &lt; 1)&lt;br /&gt;		return c / 2 * t * t + b;&lt;br /&gt;	return -c / 2 * (--t * (t - 2) - 1) + b;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;var o = new TimeLine(25, 50), d = document, b = d.getElementById("box");&lt;br /&gt;o.onframe = function(){&lt;br /&gt;	b.style.left = Math.ease(this.current, 0, 400, 30) + "px";&lt;br /&gt;};&lt;br /&gt;o.onstart = function(){&lt;br /&gt;	d.body.appendChild(d.createTextNode("Started"));&lt;br /&gt;};&lt;br /&gt;o.onstop = function(){&lt;br /&gt;	d.body.appendChild(d.createTextNode(" - Finished (" + (((new Date) - this.time)) + " msec)"))&lt;br /&gt;	d.body.appendChild(d.createElement("br"));&lt;br /&gt;	this.start();&lt;br /&gt;};&lt;br /&gt;o.start();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;var o2 = new TimeLine(12, 50), b2 = d.getElementById("box2");&lt;br /&gt;o2.onframe = function(){&lt;br /&gt;	b2.style.left = Math.ease(this.current, 0, 400, 30) + "px";&lt;br /&gt;};&lt;br /&gt;o2.onstop = function(){&lt;br /&gt;	this.start();&lt;br /&gt;};&lt;br /&gt;o2.start();&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 30 Mar 2008 16:49:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5293</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
  </channel>
</rss>
