<?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>Brit Gardner &#187; html/css</title>
	<atom:link href="http://britg.com/topics/htmlcss/feed/" rel="self" type="application/rss+xml" />
	<link>http://britg.com</link>
	<description>The big yellow one&#039;s the sun.</description>
	<lastBuildDate>Fri, 03 Feb 2012 05:08:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Forging Forgecraft: Integrating CSS3 Transitions with Javascript</title>
		<link>http://britg.com/2012/01/15/forging-forgecraft-integrating-css3-transitions-with-javascript/</link>
		<comments>http://britg.com/2012/01/15/forging-forgecraft-integrating-css3-transitions-with-javascript/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 16:01:01 +0000</pubDate>
		<dc:creator>britg</dc:creator>
				<category><![CDATA[gaming]]></category>
		<category><![CDATA[html/css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[forgecraft]]></category>

		<guid isPermaLink="false">http://britg.com/?p=26688</guid>
		<description><![CDATA[Forgecraft is a game currently in development using Ruby on Rails, Backbone.js, and all sorts of HTML5 buzzwords. Read an introduction here, and play the demo here. With Forgecraft, and any game really, there are usually quite a few animations running concurrently. I found these moments to be choppy, unresponsive and frustrating when they were [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://playforgecraft.com"><img src="http://britg.com.s3.amazonaws.com/forgecraft/logo-shadow.jpg" style="float: right; margin-left: 10px; margin-bottom: 10px; height: 45px;" /></a><em><a href="http://playforgecraft.com">Forgecraft</a> is a game currently in development using Ruby on Rails, Backbone.js, and all sorts of HTML5 buzzwords. Read an introduction <a href="http://britg.com/2011/12/29/forging-forgecraft-part-1-introduction/">here</a>, and play the demo <a href="http://playforgecraft.com">here</a>.</em></p>
<p>With Forgecraft, and any game really, there are usually quite a few animations running concurrently. I found these moments to be choppy, unresponsive and frustrating when they were implemented in javascript (especially on lower CPU environments like the iPad) and needed another solution. <a href="http://www.alistapart.com/articles/understanding-css3-transitions/">CSS3 transitions</a> were an obvious choice to check out, as they&#8217;ve been shown to be a great way to add a little pizzazz to a modern web app and can be executed natively (and even <a href="http://www.html5rocks.com/en/tutorials/speed/html5/#transanim">GPU accelerated</a>).</p>
<p><strong>But, can they provide the technical underpinnings for an interactive gamely element?</strong> To be effective in this context, they&#8217;d have to be:</p>
<ul>
<li>responsive, consistent and reliable</li>
<li>noticeably smoother than their javascript counterparts</li>
<li>simple to integrate with scripting</li>
</ul>
<p>Regarding the first 2 points, CSS3 transitions&#8217; effectiveness will really depend on your particular use-case. With Forgecraft they were definitely response, reliable, and smoother than javascript and I decided early on to use them in lieu of javascript wherever I could. But, I won&#8217;t go into the benchmarks and A/B comparisons in this article… perhaps later. Let&#8217;s just skip straight to the useful part:</p>
<h3>Product Intergortion <span style="font-size:12px"><em><a href="http://www.youtube.com/watch?v=CurP0BRHT9Y&#038;feature=endscreen&#038;NR=1" target="_blank">obscure 30 Rock reference &rarr;</a></em></span></h3>
<p>Integrating CSS3 transitions with the game&#8217;s scripting is pretty straight forward:</p>
<ul>
<li>Define transitions in CSS on your master element</li>
<li>Define classes with resulting properties changed</li>
<li>Trigger a class change using javascript</li>
<li>Listen for the transition-end event with javascript</li>
</ul>
<p>Of course, there are a few pitfalls with each of these steps that I&#8217;ll go into here. Let&#8217;s take an example from Forgecraft: the Bonus Strike event. Randomly while forging the player will see a bar pop-up like this:</p>
<p><img src="http://britg.com.s3.amazonaws.com/forgecraft/active-forge.jpg" style="width: 500px;" /></p>
<p>The bar moves from left-to-right and the player&#8217;s goal is to click the icon when the moving bar is directly under the target (large white) bar. The moving bar is animated with CSS3 transitions.</p>
<h4>Defining the CSS:</h4>
<p>We have a bar that needs an initial negative offset (the start position) that transitions to its final position. Simple enough: we define the base CSS on the <code>#bar</code> element and give it two classes (.new and .activated) defining each of its states. We also define the transition between the two states:</p>
<p><script src="https://gist.github.com/1616386.js?file=bar.css"></script><br />
<noscript><br />
#bar {<br />
  width: 600px;<br />
  height: 60px;</p>
<p>  position: relative;</p>
<p>  background-color: rgba(153, 153, 153, 0.3);<br />
  border-right: solid 5px white;</p>
<p>  -moz-transition-property: left;<br />
  -webkit-transition-property: left;<br />
  -o-transition-property: left;<br />
  transition-property: left;<br />
  -moz-transition-duration: 1.5s;<br />
  -webkit-transition-duration: 1.5s;<br />
  -o-transition-duration: 1.5s;<br />
  transition-duration: 1.5s;</p>
<p>}</p>
<p>#bar.new {<br />
  left: 0px;<br />
}</p>
<p>#bar.activated {<br />
  left: 600px;<br />
}<br />
</noscript></p>
<p><strong>One pitfall you may run into:</strong> for the animations to work in Firefox, you have to explicitly define the initial state for whatever property you are transitioning. In this example we are transitioning the <code>left</code> property of the <code>#bar</code> element. You normally wouldn&#8217;t define <code>left: 0;</code> &#8212; that&#8217;s the default! But Firefox requires this to trigger the transition when that property changes.</p>
<h4>Triggering the Animation in Javascript</h4>
<p>Using jQuery, we simply apply the classes that we defined in the CSS when we want to trigger the transition.</p>
<p><script src="https://gist.github.com/1616386.js?file=activate.js"></script><br />
<noscript><br />
$(&#8216;#bar&#8217;).removeClass(&#8216;new&#8217;).addClass(&#8216;activated&#8217;);<br />
</noscript></p>
<h4>Listening for the transition-end event</h4>
<p>CSS3 Transitions would be useless if we couldn&#8217;t get their context from within our scripting. Luckily, a series of events are fired in javascript while the transitions are running, just like you&#8217;d expect in a javascript-based animation. The primary event we care about is when the transition ends as you&#8217;ll most likely want to trigger callbacks.</p>
<p>Unfortunately, each browser vendor has decided to name these events differently… typical huh. <a href="http://www.modernizr.com/">Modernizr</a> to the rescue! (If you&#8217;re not using Modernizr, you should be. But that&#8217;s another blog post altogether).</p>
<p>There&#8217;s a hidden gem in the comments in the source code of Modernizr that explains how to use its <code><a href="http://www.modernizr.com/docs/#prefixed">.prefixed()</a></code> API to make a simple wrapper around the browser-specific transition event names. Here&#8217;s how I implemented it for the transition-end event. Feel free to use this wherever you need it:</p>
<p><script src="https://gist.github.com/1616386.js?file=transitionend.js"></script><br />
<noscript><br />
// Adapted from the Modernizer source comments</p>
<p>var transEndEventNames = {<br />
  &#8216;WebkitTransition&#8217; : &#8216;webkitTransitionEnd&#8217;,<br />
  &#8216;MozTransition&#8217;    : &#8216;transitionend&#8217;,<br />
  &#8216;OTransition&#8217;      : &#8216;oTransitionEnd&#8217;,<br />
  &#8216;msTransition&#8217;     : &#8216;msTransitionEnd&#8217;, // maybe?<br />
  &#8216;transition&#8217;       : &#8216;transitionEnd&#8217;<br />
}, </p>
<p>CSS3_TRANSITION_END = transEndEventNames[ Modernizr.prefixed('transition') ];<br />
</noscript></p>
<p>Bam, now we can use one simple event binding to handle all browsers:</p>
<p><script src="https://gist.github.com/1616386.js?file=listen.js"></script><br />
<noscript><br />
$(&#8216;#bar&#8217;).bind(CSS3_TRANSITION_END, function() {<br />
  //&#8230; do stuff<br />
});<br />
</noscript></p>
<h4>Stopping a Transition Early</h4>
<p>When the player hits the hammer-and-anvil icon during the Bonus Strike event, I needed to stop the animation early. The easiest way I found to do this, was to just set the animating property to its current value. The transition delta becomes 0, so the animation stops:</p>
<p><script src="https://gist.github.com/1616386.js?file=stop.js"></script><br />
<noscript><br />
$(&#8216;#bar&#8217;)css({ left: $(&#8216;#bar&#8217;).css(&#8216;left&#8217;) });<br />
</noscript></p>
<h3>Transition: End</h3>
<p>You may find that integrating CSS3 Transitions into your game makes your interactions smoother and more responsive, and with a simple API for scripting against transition events, developing against them should look and feel a lot like working in pure javascript.</p>
<hr />
<p><em>More Forging Forgecraft:</em></p>
<ul>
<li> <a href="http://britg.com/2011/12/29/forging-forgecraft-part-1-introduction/">Introducing Forgecraft</a></li>
<li> <a href="http://britg.com/2012/01/07/forging-forgecraft-a-hybrid-sql-mongodb-data-solution/">A Hybrid SQL MongoDB Data Solution</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://britg.com/2012/01/15/forging-forgecraft-integrating-css3-transitions-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Apply Full CSS Reset on an Arbitrary Selector with Compass</title>
		<link>http://britg.com/2009/05/30/how-to-do-a-full-css-reset-on-an-arbitrary-selector-with-compass/</link>
		<comments>http://britg.com/2009/05/30/how-to-do-a-full-css-reset-on-an-arbitrary-selector-with-compass/#comments</comments>
		<pubDate>Sat, 30 May 2009 23:42:10 +0000</pubDate>
		<dc:creator>britg</dc:creator>
				<category><![CDATA[html/css]]></category>
		<category><![CDATA[compass]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[sass]]></category>

		<guid isPermaLink="false">http://britg.com/?p=914</guid>
		<description><![CDATA[Compass comes with a very handy utility for completely resetting the styling accross all browsers. In the author&#8217;s own words on the github wiki: The Reset module provides mixins that allow you to reset your html so that every element has browser-supplied no styling The default compass stylesheet comes with this module included, i.e. the [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-923" href="http://britg.com/2009/05/30/how-to-do-a-full-css-reset-on-an-arbitrary-selector-with-compass/compass/"><img class="alignright size-medium wp-image-923" title="compass" src="http://britg.com/blog/wp-content/uploads/2009/05/compass-300x80.png" alt="compass" width="300" height="80" /></a><a href="http://compass-style.org/">Compass</a> comes with a very handy utility for completely resetting the styling accross all browsers.  In the author&#8217;s own words on the <a href="http://wiki.github.com/chriseppstein/compass/compass-core-reset-module">github wiki</a>:</p>
<blockquote><p>The Reset module provides mixins that allow you to reset your html so that every element has browser-supplied no styling</p></blockquote>
<p>The default compass stylesheet comes with this module included, i.e. the <code>@import compass/reset.sass</code> you see at the top, and spits out the reset styles into the global namespace.</p>
<p>But, lets say you have a separate stylesheet that is to be included on a third party domain and that is namespaced under a selector.  You can still take advantage of the Compass reset styling through the following method:</p>
<pre class="brush: css; title: ;">@import compass/utilities/general/reset.sass

#hh
  +global-reset</pre>
<p>The result is the complete resetting of every child element of your namespace, i.e:</p>
<pre class="brush: css; title: ;">...
  #hh div, #hh span, #hh object, #hh iframe, #hh h1, #hh h2, #hh h3, #hh h4, #hh h5, #hh h6, #hh p,
  #hh pre, #hh a, #hh abbr, #hh acronym, #hh address, #hh code, #hh del, #hh dfn, #hh em, #hh img,
  #hh dl, #hh dt, #hh dd, #hh ol, #hh ul, #hh li, #hh fieldset, #hh form, #hh label, #hh legend, #hh caption, #hh tbody, #hh tfoot, #hh thead, #hh tr {
    margin: 0;
    padding: 0;
    border: 0;
    font-weight: inherit;
    font-style: inherit;
    font-size: 100%;
    font-family: inherit;
    vertical-align: baseline; }

...</pre>]]></content:encoded>
			<wfw:commentRss>http://britg.com/2009/05/30/how-to-do-a-full-css-reset-on-an-arbitrary-selector-with-compass/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

