<?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>Kilian Valkhof &#187; Javascript</title>
	<atom:link href="http://kilianvalkhof.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://kilianvalkhof.com</link>
	<description></description>
	<lastBuildDate>Thu, 10 Nov 2011 12:42:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Annoying.js: How to be an asshole</title>
		<link>http://kilianvalkhof.com/2011/javascript/annoying-js-how-to-be-an-asshole/</link>
		<comments>http://kilianvalkhof.com/2011/javascript/annoying-js-how-to-be-an-asshole/#comments</comments>
		<pubDate>Mon, 23 May 2011 09:00:28 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=925</guid>
		<description><![CDATA[<blockquote><p>As you can see, you can do some pretty bad things with JavaScript that will certainly drive your visitors away (or insane!)</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2011/javascript/annoying-js-how-to-be-an-asshole/">Annoying.js: How to be an asshole</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In January I came across a website that had a whole slew of JavaScript that attempted to prevent you from selecting text, right clicking or dragging any content onto your desktop. I decided to copy the JavaScript and create a library with examples of JavaScript techniques you can use if you want to scare your visitors away, or want to piss them off. Annoying.js is the result.<span id="more-925"></span></p>
<h2>Annoying.js</h2>
<p><script src="https://gist.github.com/767982.js?file=asshole.js"></script></p>
<p>The file itself is commented with &#8220;functionality&#8221;, so I won&#8217;t bother repeating it here. You can invoke different functions via the <code>Annoying</code> object: <code>Annoying.noSelect();</code> will disable text selection, <code>Annoying.noRightClick();</code> will disable the context menu, and <code>Annoying.kitchensink();</code> Will turn all the functionality on. But you&#8217;re not going to use it anyway, right? ;)</p>
<h3>JavaScript is evil!</h3>
<p>As you can see, you can do some pretty bad things with JavaScript that will certainly drive your visitors away (or insane!). So why is this kind of functionality available? Some functions is actually very useful if you&#8217;re writing complex &#8220;html5&#8243; web applications. By disabling text-selection on interface elements you can make the application easier to use, and you can re-implement the right click menu using your own options. </p>
<p>Breaking out of iframes is something I think more websites should implement, actually, if only to prevent the &#8220;OMG!!!! Look What this Kid did to his School after being Expelled! &#8221; scams that all of my friends on Facebook keep falling for.</p>
<p>Using JavaScript to resize someone&#8217;s browser without consent should be reason enough to revoke someone&#8217;s license to making websites, though.</p>
<p>Let me know if you have any horrible additions!</p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2011/javascript/annoying-js-how-to-be-an-asshole/">Annoying.js: How to be an asshole</a></p>
 <p><a href="http://kilianvalkhof.com/?flattrss_redirect&amp;id=925&amp;md5=e466742a2fcd268f7e5d1740110f44fe" title="Flattr" target="_blank"><img src="http://kilianvalkhof.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2011/javascript/annoying-js-how-to-be-an-asshole/feed/</wfw:commentRss>
		<slash:comments>71</slash:comments>
		</item>
		<item>
		<title>Full size profile picture via the Facebook JavaScript SDK</title>
		<link>http://kilianvalkhof.com/2011/javascript/full-size-profile-picture-via-the-facebook-javascript-sdk/</link>
		<comments>http://kilianvalkhof.com/2011/javascript/full-size-profile-picture-via-the-facebook-javascript-sdk/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 08:00:12 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=878</guid>
		<description><![CDATA[<blockquote><p>All questions I found regarding getting a full size profile picture ended in "it can't be done". But I'm a stubborn guy, so I went to work with the Facebook Javascript SDK.</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2011/javascript/full-size-profile-picture-via-the-facebook-javascript-sdk/">Full size profile picture via the Facebook JavaScript SDK</a></p>
]]></description>
			<content:encoded><![CDATA[<p>While working on a soon-to-be-released website, I needed the full-size profile picture of a Facebook user. Facebook has a shortcut for getting someone&#8217;s profile picture, but the largest image you can get is just 200 pixels wide which is not enough for my application. This is a quick article explaining how to get the full size image, as I wasn&#8217;t able to find it elsewhere.<span id="more-878"></span></p>
<h3>Getting the profile picture the normal way</h3>
<p>Getting via the normal way is quite easy, it&#8217;s this link: </p>
<p><strong>http://graph.facebook.com/&lt;user.id&gt;/picture?type=large</strong></p>
<p>If you&#8217;re happy with a 200px wide image, you&#8217;re best off using this link.</p>
<h2>Impossible?</h2>
<p>All questions I found regarding getting a full size profile picture ended in &#8220;it can&#8217;t be done&#8221;. But I&#8217;m a stubborn guy, so I <a href="http://forum.developers.facebook.net/viewtopic.php?pid=303869">asked about it again</a> on the Facebook developer forums and then went to work with the (horribly underdocumented) Facebook JavaScript SDK. I got it working and a couple of days later someone posted an FQL method as a reply to my forum thread. Both methods take two API calls, so it&#8217;s a matter of preference which one you want to use. I&#8217;m posting them both.</p>
<h3>Assumptions</h3>
<p>The following scripts assume a couple of things:</p>
<ul>
<li>You&#8217;re obviously using <a href="http://developers.facebook.com/docs/reference/javascript/">the Facebook javascript SDK</a></li>
<li>You have an <a href="http://developers.facebook.com/docs/reference/javascript/FB.login">authenticated Facebook user</a></li>
<li>You have requested <a href="http://developers.facebook.com/docs/authentication/permissions">permission to access</a> <strong>user_photos</strong></li>
<li>All you care about is the URL of the full size profile image</li>
</ul>
<h3>Graph API method</h3>
<p>The following Graph API method works by doing two API calls: The first requests all the user albums and gets the ID of the album with the name &#8220;Profile pictures&#8221;, the second gets the first image from that album. Conveniently, the current profile picture is always the first in the returned list.</p>
<pre><code>//Get a list of all the albums
FB.api('/me/albums', function (response) {
  for (album in response.data) {

    // Find the Profile Picture album
    if (response.data[album].name == "Profile Pictures") {

      // Get a list of all photos in that album.
      FB.api(response.data[album].id + "/photos", function(response) {

        //The image link
        image = response.data[0].images[0].source;

      });
    }
  }
});</code></pre>
<h3>FQL method</h3>
<p>The FQL method also requires two api calls: one to get the user id, and a second FQL query for that user&#8217;s profile picture. You could lower this to one API call if you store the user id sometime earlier.</p>
<pre><code>//get the current user id
FB.api('/me', function (response) {

  // the FQL query: Get the link of the image, that is the first in the album "Profile pictures" of this user.
  var query = FB.Data.query('select src_big from photo where pid in (select cover_pid from album where owner={0} and name="Profile Pictures")', response.id);
  query.wait(function (rows) {

    //the image link
    image = rows[0].src_big;
  });
});</code></pre>
<h3>There you go!</h3>
<p>A full size Facebook profile picture, free for you to do all sorts of cool stuff with.  Let me know if you build a cool Facebook app and have been able to use this!</p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2011/javascript/full-size-profile-picture-via-the-facebook-javascript-sdk/">Full size profile picture via the Facebook JavaScript SDK</a></p>
 <p><a href="http://kilianvalkhof.com/?flattrss_redirect&amp;id=878&amp;md5=a3220aa6c0424f684144062a476e672e" title="Flattr" target="_blank"><img src="http://kilianvalkhof.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2011/javascript/full-size-profile-picture-via-the-facebook-javascript-sdk/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Open source updates</title>
		<link>http://kilianvalkhof.com/2010/web/open-source-updates/</link>
		<comments>http://kilianvalkhof.com/2010/web/open-source-updates/#comments</comments>
		<pubDate>Mon, 27 Sep 2010 08:00:30 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[SenCSs]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=796</guid>
		<description><![CDATA[<blockquote></p>Announced at the beginning of this year, Grafico has seen a bunch of cool improvements</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2010/web/open-source-updates/">Open source updates</a></p>
]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since I&#8217;ve written about a bunch of my open source projects, but that doesn&#8217;t mean they&#8217;re not still being maintained. Trimage, Grafico, F.lux and SenCSs are all still actively worked on. Read on to find out what&#8217;s been happening with them.<span id="more-796"></span></p>
<h2>SenCSs</h2>
<p>When last <a href="http://kilianvalkhof.com/2010/sencss/sencss-0-7-is-out/">written about</a>, SenCSs was at version 0.7. We&#8217;re currently at version 0.8.1, with a bunch of vertical rhythm fixes, mostly for form elements, and added support for HTML5 elements. You can now add it to the list of &#8220;HTML5 CSS frameworks&#8221;. ;) Though, &#8220;framework&#8221; it is not, so I decided to just rename it to what it is: a Baseline. </p>
<p>Get more info at <a href="http://sencss.kilianvalkhof.com">SenCSs.kilianvalkhof.com</a></p>
<h2>F.lux for Ubuntu</h2>
<p>After releasing <a href="http://kilianvalkhof.com/2010/linux/flux-for-ubuntu/">F.lux for Ubuntu</a> this summer, It went from 1.1.1 to 1.1.8 mostly improving stability and making different parts of the application (such as the preview button) work more like users expect. I&#8217;ve also been working on KDE/XFCE support, which was kind of finnicky due to the way I select the indicator applet icon. Version 1.1.9, which will be released soon, will hopefully bring this support. After that, it&#8217;s onward to 1.2, which will include the &#8216;turn off for an hour&#8217; functionality.</p>
<p>Get more info at <a href="http://stereopsis.com/flux/linux.html">stereopsis.com/flux/linux.html</a></p>
<h2>Trimage</h2>
<p>After announcing Trimage in <a href="http://kilianvalkhof.com/2010/web/trimage-image-compressor/">March of this year</a>, I&#8217;ve gone from 1.0.0-beta to 1.0.5, with a slew of added functionality in between. I&#8217;ve gotten a lot of help from people such as <a href="http://github.com/kalmi">Tarnay Kálmán</a> and <a href="http://github.com/bistory">Thomas Lété</a> in adding cool new functions. Here&#8217;s what has changed since 1.0.0:</p>
<ul>
<li>Correct unicode support for all filenames</li>
<li>Adding a notification area icon</li>
<li>Using a threadpool for processing (much faster)</li>
<li>Showing more statistics when compressing</li>
<li>Window geometry and last opened directory are now remembered</li>
<li>Ability to drag in entire folders</li>
<li>Prevent Trimage from overwriting already smaller images</li>
<li>Add PNGcrush to get even better compression</a>
<li>Miscelanious speed improvements</li>
</ul>
<p>A while ago I tried getting Trimage into Debian via <a href="http://mentors.debian.net/cgi-bin/welcome">debian-mentors</a>. This was unfortunately a very opaque process and in the end no-one wanted to sponsor Trimage despite numerous changes to the build system. I&#8217;d still like to get Trimage into the main repositories, so if anyone wants to help me out, that would be greatly appreciated.</p>
<p>For 1.1, <a href="http://paulchaplin.com">Paul</a> is going to update the command line options and generally use his python-fu to improve stuff.</p>
<p>Get more info at <a href="http://trimage.org">trimage.org</a></p>
<h2>Grafico</h2>
<p>Announced at the beginning of this year, Grafico has seen a bunch of cool improvements. Within a week, Jan Paul Posma implemented stream graph functionality (after I mentioned the math was much too hard for me, ahem&hellip;). Since then, <a href="http://github.com/suranyami">David Parry</a> added <a href="http://grafico.kilianvalkhof.com/documentation/index.html#stackbar">stacked bar charts</a>, <a href="http://github.com/rymai">Rémy Coutable</a> greatly improved my hover code while <a href="http://github.com/japetheape">Jaap van der Meer</a> improved the control of text in the hovers, <a href="http://github.com/menno">Menno van der Sman</a> implemented an <em>awesome</em> axis-value algorithm and a lot of options have become much more customizable. Most charts now support negative values as well as positive values, and I&#8217;ve done a whole slew of bug fixes and speed improvements.</p>
<p>Get more info at <a href="http://grafico.kilianvalkhof.com">Grafico.kilianvalkhof.com</a></p>
<p>All in all, a whole bunch of work by a whole bunch of awesome people!</p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2010/web/open-source-updates/">Open source updates</a></p>
 <p><a href="http://kilianvalkhof.com/?flattrss_redirect&amp;id=796&amp;md5=5f7c88276be52ba4db9b12318d0362b2" title="Flattr" target="_blank"><img src="http://kilianvalkhof.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2010/web/open-source-updates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The case of the disappearing favicon</title>
		<link>http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/</link>
		<comments>http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 08:00:37 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=772</guid>
		<description><![CDATA[<blockquote><p>Luckily, there is an easy fix: Just reset the favicon</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/">The case of the disappearing favicon</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Firefox has an annoying bug: If you set the <code>window.location.hash</code>, your favicon disappears. <em>Very</em> annoying. <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=519028">The bug</a> has been open and confirmed for a long time, and Firefox 3.0 all the way to 3.6 are affected. But it hasn&#8217;t been fixed yet, so here&#8217;s how to fix it yourself.<span id="more-772"></span></p>
<p>I encountered this bug while building <a href="http://lystener.com">Lystener</a>. I change the location hash there quite often, and each time I did, the favicon would disappear. Finding the root cause took me quite a while, and apart from the bug report I could not find any page describing why this happens and what to do about it. So I figured that I&#8217;d write a quick post about it to make it Google-able at least.</p>
<p>Luckily, there is an easy fix: Just reset the favicon after changing the <code>window.location.hash</code>. Here&#8217;s a jQuery function that does just that:</p>
<pre></code>function setFavicon() {
  var link = $('link[type=image/x-icon]').remove().attr("href");
  $('&lt;link href="'+ link +'" rel="shortcut icon" type="image/x-icon" /&gt;').appendTo('head');
}</code></pre>
<p>It&#8217;s fairly straightforward: We find the current <code>&lt;link&gt;</code> referencing the favicon, remove it, and then, making use of jQuery&#8217;s standard of functions returning themselves, get the href attribute of the just-deleted link. Then we simply set that as the <code>href</code> in a newly created <code>link</code> element, and add that to the head. </p>
<p>Just execute that function each time you change the <code>window.location.hash</code>, and you&#8217;re all set!</p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/">The case of the disappearing favicon</a></p>
 <p><a href="http://kilianvalkhof.com/?flattrss_redirect&amp;id=772&amp;md5=52e7ea753189ed3c0c9dca057736f7ce" title="Flattr" target="_blank"><img src="http://kilianvalkhof.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2010/javascript/the-case-of-the-disappearing-favicon/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>How to build a fast, simple list filter with jQuery</title>
		<link>http://kilianvalkhof.com/2010/javascript/how-to-build-a-fast-simple-list-filter-with-jquery/</link>
		<comments>http://kilianvalkhof.com/2010/javascript/how-to-build-a-fast-simple-list-filter-with-jquery/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 08:00:03 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=613</guid>
		<description><![CDATA[<blockquote><p>jQuery has a way to check for matched elements easily: the <code>:contains()</code> selector.</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2010/javascript/how-to-build-a-fast-simple-list-filter-with-jquery/">How to build a fast, simple list filter with jQuery</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Sometimes you want users of your web-app to quickly filter a list down. For example, a web-app I&#8217;m currently working on features a page listing two dozen countries, and I want users to find the country they are looking for as fast as possible. This article shows you how little code is needed to add this in a fast, progressively-enhanced way using jQuery.<span id="more-613"></span></p>
<h3>Demo</h3>
<p>If you&#8217;re the impatient type, I&#8217;ve set up a simple demo: <a href="http://kilianvalkhof.com/uploads/listfilter/">A simple list filter with jQuery</a>.</p>
<h3>The HTML</h3>
<p>The basic HTML you need for this is simple: just a header and a simple unordered list. They both have id&#8217;s so we can easily and uniquely find them:</p>
<pre><code>&lt;h1 id="header"&gt;List of countries&lt;/h1&gt;

&lt;ul id="list"&gt;
  &lt;li&gt;&lt;a href="/australia"&gt;Australia&lt;/a&gt;&#038;lt/li&gt;
  &lt;li&gt;&lt;a href="/austria"&gt;Austria&lt;/a&gt;&#038;lt/li&gt;
  &lt;li&gt;&lt;a href="/belgium"&gt;Belgium&lt;/a&gt;&#038;lt/li&gt;
  &hellip;
&lt;ul&gt;</code></pre>
<p>With the HTML ready, here is what the filter function needs to do:</p>
<ol>
<li>Add an input to the header</li>
<li>Set up events to check when someone is typing in the input</li>
<li>Check the value of the input, and compare it to the countries in the list</li>
<li>Hide the non-matching ones, while showing the matching ones</li>
</ol>
<h3>Step 1: add an input to the header</h3>
<p>We&#8217;ll start with a simple function, we&#8217;ll call it listFilter:</p>
<pre><code>function listFilter(header, list) {
  &hellip;
}</code></pre>
<p>As you see, the function takes two arguments. We&#8217;ll use jQuery to select the aforementioned header and list and give them as arguments to our function. Next, we need to create a form with an input in it, and append it to the header:</p>
<pre><code>function listFilter(header, list) {
  // create and add the filter form to the header
  var form = $("&lt;form&gt;").attr({"class":"filterform","action":"#"}),
      input = $("&lt;input&gt;").attr({"class":"filterinput","type":"text"});

  $(form).append(input).appendTo(header);
  &hellip;
}</code></pre>
<p>Using $(&#8220;&lt;form&gt;&#8221;) (with brackets) we can create new elements, and by inserting the input into the form using <code>append()</code>, and then using the <code>appendTo()</code> function to add the form (with the input in it) to the header, we easily add it into the page. For easy of use I&#8217;ve given both the form and the input classes, so you can style them easily.</p>
<p>That&#8217;s step one done, but it doesn&#8217;t do anything yet, for that we need to&hellip;</p>
<h3>Step 2: set up events</h2>
<p>To check for text in the input, we can use the <code>change</code> event. This fires every time the value of the input has changed, but only when you exit the input. However, this doesn&#8217;t work with a letter-for-letter filter. For that, we need to watch the <code>keyup</code> event a well. We use <code>keyup</code> instead of <code>keydown</code> because it fires after you pressed the key, and so the value of the input then includes the just-typed letter. With <code>keydown</code>, the event gets fired before the letter is added, so JavaScript can&#8217;t pick it up in the value of the input.</p>
<p>While using two events, there is no need to do double work. When we receive a <code>keyup</code> event, we just fire a <code>change</code> event:</p>
<pre><code>function listFilter(header, list) {
  &hellip;
  $(input).change( function () {
    &hellip;
  }).keyup( function () {
    // fire the above change event after every letter
    $(this).change();
  });
  &hellip;
}</code></pre>
<p>We&#8217;re now all set to start with the actual checking!</p>
<h3>Step 3: compare the values</h3>
<p>jQuery has a way to check for matched elements easily: the <code><a href="http://api.jquery.com/contains-selector/">:contains()</a></code> selector. Using this, you can very easily select all element containing the text between the parenthesis:</p>
<pre><code>function listFilter(header, list) {
  &hellip;
  $(input).change( function () {
    var filter = $(this).val(); // get the value of the input, which we filter on
    $(list).find("a:contains(" + filter + ")").parent().slideDown();
  });
  &hellip;
}</code></pre>
<p>The code above works like this: in the list, which is the <code>&lt;ul&gt;</code> element. it finds all the shows all the <code>&lt;a&gt;</code> elements that contain the value in the input. It then selects the parents of those <code>&lt;a&gt;</code> elements, the <code>&lt;li&gt;</code>&#8216;s. Those li&#8217;s then get shown. However, since we filter the list, we need to also hide all the li&#8217;s that <em>do not contain</em> that value. For this, jQuery offers the <code>:not()</code> selector, and we can just wrap that around the <code>:contains()</code> selector, like so:</p>
<pre><code>function listFilter(header, list) {
  &hellip;
  $(input).change( function () {
    var filter = $(this).val(); // get the value of the input, which we filter on
    $(list).find("a:not(:contains(" + filter + "))").parent().slideUp();
    $(list).find("a:contains(" + filter + ")").parent().slideDown();
  });
  &hellip;
}</code></pre>
<p>There, now with each keystroke, we check the list for matching elements, and hide the ones that don&#8217;t match. Easy-peasy.</p>
<p>But wait. What happens when you clear the input? Then $(this).val(); will be empty, and nothing matches with empty. So all list items will be hidden, the exact opposite of what we want. This can be remedied by just checking if $(this).val(); has any value, and if not, just showing everything:</p>
<pre><code>function listFilter(header, list) {
  &hellip;
  $(input).change( function () {
    var filter = $(this).val();
    if (filter) {
      $(list).find("a:not(:contains(" + filter + "))").parent().slideUp();
      $(list).find("a:contains(" + filter + ")").parent().slideDown();
    } else {
      $(list).find("li").slideDown();
    }
  });
  &hellip;
}</code></pre>
<p>Typing in letters now filters down the list, and removing them all shows the entire, now unfiltered, list again. Is that all we need? Well, It would be, except that <code>:contains()</code> is <em>case-sensitive</em>. so if you filter with &#8220;austria&#8221;, you won&#8217;t find Austria!</p>
<h3>Building a case-insensitive :contains()</h3>
<p>jQuery allows you to add your own selector expressions, and we can use that to build our own contains filter that&#8217;s a bit more sensible:</p>
<pre></code>jQuery.expr[':'].Contains = function(a,i,m){
    return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
};</code></pre>
<p>This adds a <code>:Contains()</code> (<em>note the uppercase</em>) option to your selectors that does the same as <code>:contains()</code>, but it first converts all text to uppercase, and then compares them. So once we add this to your JavaScript, all we have to do in our listFilter function is replace <code>:contains()</code> with <code>Contains()</code> and we have a fast, simple, progressively-enhanced way to filter down a large list!</p>
<h3>Demo</h3>
<p>If you want to browse through the entire, documented code, and see it in action, check out the example page: <a href="http://kilianvalkhof.com/uploads/listfilter/">A simple list filter with jQuery</a>.</p>
<p>Please let me know in the comments what you would use this for, and please let me know your remarks and questions!</p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2010/javascript/how-to-build-a-fast-simple-list-filter-with-jquery/">How to build a fast, simple list filter with jQuery</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2010/javascript/how-to-build-a-fast-simple-list-filter-with-jquery/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
		<item>
		<title>Grafico: Javascript charting library</title>
		<link>http://kilianvalkhof.com/2010/design/grafico-javascript-charting-library/</link>
		<comments>http://kilianvalkhof.com/2010/design/grafico-javascript-charting-library/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 04:00:30 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=541</guid>
		<description><![CDATA[<blockquote><p>There are a lot of bad charts out there, and Grafico will do it's best to prevent you from adding charts onto that pile. ;)</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2010/design/grafico-javascript-charting-library/">Grafico: Javascript charting library</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Grafico is a javascript charting library built with Raphaël and Prototype.js. The library provides a wide array of graphs and stays with the guidelines laid out by Stephen Few and Edward Tufte. <strong>Grafico provides pretty charts that effectively communicate their information.</strong><span id="more-541"></span></p>
<h2>Graphs</h2>
<p>This means you get awesome graphs such as <em>stacked area charts</em> and <em>sparklines</em>, but no pie charts or bar charts with every bar a different color. There are a lot of bad charts out there, and Grafico will do it&#8217;s best to prevent you from adding charts onto that pile. ;)</p>
<p><img src="http://grafico.kilianvalkhof.com/grafico-example.png" alt="" /><br />
<strong><a href="http://grafico.kilianvalkhof.com">Website</a></strong>, <strong><a href="http://grafico.kilianvalkhof.com/documentation/">Documentation</a></strong> (might be slow on some browsers due to all the examples)</p>
<div style="float:right;margin-left:2em;">
Available graphs:</p>
<ul>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#line">Line graphs</a></li>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#area">Area graphs</a></li>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#stack">Stacked area graphs</a></li>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#stream">Stream graphs</a></li>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#bar">Bar graphs</a></li>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#horizontalbar">Horizontal bar graphs</a></li>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#sparkline">Sparklines</a></li>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#sparkbar">Sparkbars</a></li>
<li><a href="http://grafico.kilianvalkhof.com/documentation/index.html#sparkarea">Sparkareas</a></li>
</ul>
</div>
<p>Grafico requires <a href="http://raphaeljs.com">Raphaël</a> 1.0+ and <a href="http://prototypejs.org">Prototype.js</a> 1.6+</p>
<p>Each graph type has numerous API options to customize it&#8217;s look and behavior. Many examples of this can be found in the <a href="http://grafico.kilianvalkhof.com/documentation/">documentation</a>. Some highlights: pop-up hovers, meanlines and watermarks. Minified, Grafico is about 30kb and available under the MIT license.</p>
<p>Grafico is also available on <a href="http://github.com/kilian/grafico">Github</a> and includes a minification script made by <a href="http://wakoopa.com/menno">Menno</a> using the Google Closure Compiler.</p>
<h3 style="clear:both">History</h3>
<p>In april 2009 I started working on new visualizations for <a href="http://wakoopa.com">Wakoopa</a>. I had set it my target to provide unique and interesting graphs. One thing that particularly inspired me was the <a href="http://www.neoformix.com/2008/StreamGraph.html">stream graph</a>, but the <a href="http://www.neoformix.com/2008/StreamGraph.html">math behind</a> it scared the crap out of me. I decided to focus on stacked area graphs instead.</p>
<p>After doing extensive research on the available JavaScript charting libraries, and being slightly disappointed by most, <a href="http://alexyoung.org/2009/01/21/ico-svg-graphs-with-prototype-and-raphael/">Ico by Alex Young</a> was the most promising. In the (almost ) year I worked on it, the graphs, code and API changed considerably. To avoid confusion I decided to rename my version to Grafico. </p>
<h3>Todo</h3>
<p>As always, there are points of improvement. The major one is speed. Grafico is very speedy on modern browsers, but of course IE lags behind. When building graphs, drawing pop-up hovers for all data points takes too much time and I have yet to find a way to improve this. The project lives on <a href="http://github.com/kilian/grafico">Github</a>, so if you have improvements, fork away and I&#8217;ll gladly pull them!</p>
<h2>Update! (16 Feb 2010)</h2>
<p><a href="http://janpaulposma.nl">Jan Paul Posma</a> wrote the code to implement the aforementioned stream graphs into Grafico. Which is <em>awesome</em>. I have updated the links in this article accordingly.</p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2010/design/grafico-javascript-charting-library/">Grafico: Javascript charting library</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2010/design/grafico-javascript-charting-library/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>The state of selectable text-replacement</title>
		<link>http://kilianvalkhof.com/2009/javascript/the-state-of-selectable-text-replacement/</link>
		<comments>http://kilianvalkhof.com/2009/javascript/the-state-of-selectable-text-replacement/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 04:00:53 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=468</guid>
		<description><![CDATA[<blockquote><p>You shouldn’t have to compromise the design or the user's experience to have selectable text.</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2009/javascript/the-state-of-selectable-text-replacement/">The state of selectable text-replacement</a></p>
]]></description>
			<content:encoded><![CDATA[<p>In &#8220;<a href="http://kilianvalkhof.com/2009/javascript/Cuf&oacute;n-vs-typefacejs-which-one-is-better/">Cufón vs. Typeface.js, which one is better?</a>&#8220;, I declared that the first one to support selectable text wins. We&#8217;re a couple of months further now and both <em>Typeselect</em> and <em>Typeface.js 0.13</em> appeared, adding support for selectable text. What is the state of selectable text-replacement?<span id="more-468"></span></p>
<h3>Typeselect</h3>
<p>Let&#8217;s take a look at the new kid on the block. <a href="http://typeselect.org">Typeselect</a> is based on version 0.12 of Typeface.js, and defines it&#8217;s goal boldly in a custom, text-selectable font on it&#8217;s homepage:</p>
<p><strong>You shouldn’t have to compromise the design or the user&#8217;s experience to have selectable text.</strong></p>
<p>Amen!</p>
<p>Typeselect acts as a layer above Typeface.js. You should take this as literal as possible, since the way Typeselect works is by adding transparent text on top of your replaced text. Script-wise, added to Typeface.js are <a href="http://jquery.com">jQuery 1.3.2</a> and their own js file, typeselect.js that takes care of the actual selection of text.</p>
<h3>Including Typeselect in your website</h3>
<pre><code>&lt;head&gt;
&hellip;
  &lt;script type="text/javascript" src="js/typeface-custom.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="fonts/nilland_regular.typeface.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/typeselect.js"&gt;&lt;/script&gt;
&hellip;
&lt;/head&gt;</code></pre>
<p>While it takes two more javascript files when compared to Typeface.js and Cuf&oacute;n, chances are that you already use jQuery and you can always choose to combine multiple javascript files. When we move on though, actually changing the font is pretty easy. By adding the class <code>select</code> to the elements you want to replace, and adding your typeface.js converted font to the css, the font will be replaced!</p>
<pre><code>&lt;div class="myclass <strong>select</strong>"&gt;
&hellip;
h1,  h2 {
  font-family:nilland, Georgia, Serif;
  font-style:italic;
}</code></pre>
<h3>Future?</h3>
<p>It seems that Typeselect aims to be a layer above typeface.js and Cuf&oacute;n to provide selectable text for both. Apart from that, better rendering of the fonts is on the <a href="http://www.typeselect.org/usage">roadmap</a> as well as a host of other things. Only&hellip; Typeface.js currently has it&#8217;s own, very similar, implementation of selectable text built right in. Actually, as far as I can see, it&#8217;s identical. So without Cuf&oacute;n support, I&#8217;m not sure if Typeselect is the best choice you can make.</p>
<h3>Typeface.js</h3>
<p>Typeface.js added text selection in version 0.13 in the same way as Typeselect: selectable text is implemented by overlaying real, transparent text on top of the rendered text. Pretty smart! AS you can see on <a href="http://typeface.neocracy.org/examples.html">Typeface.js&#8217; download page</a>, it works <em>really</em> well.</p>
<p>However, by doing this, performance goes down a bit. Not enough to really notice it, but if you want to disable it, there&#8217;s an <del>app</del> option for that. By adding the following to your &lt;head&gt; in a script element, selecting text gets disabled:</p>
<pre><code>_typeface_js.configure({ disableSelection: true })</code></pre>
<h3>Cuf&oacute;n?</h3>
<p>So, where does that leave Cuf&oacute;n? Well, text selection is <a href="http://github.com/sorccu/cufon/issues#issue/7">planned</a> in version 1.2. Right now Cuf&oacute;n is at version 1.08, so we&#8217;ll have to wait a little longer.</p>
<h3>Conclusion</h3>
<p>For now, <a href="http://typeface.neocracy.org">Typeface.js</a> is pretty much the best way to use good looking fonts while also allowing selectable text, cross-browser. However, with some work, @font-face can be made working in Firefox 3.5, Safari 3.1+, Opera 9+, Internet explorer 4+ and Chrome 0.3+. So, basically every browser. However, it is <a href="http://snook.ca/archives/html_and_css/becoming-a-font-embedding-master">a lot</a> of <a href="http://paulirish.com/2009/bulletproof-font-face-implementation-syntax/">work</a> compared to using Typeface.js, Cuf&oacute;n or Typeselect. </p>
<p>Personally I think that in most browsers, using the javascript replacement results in better looking text, and it&#8217;s definitely easier to implement. In terms of forward compatibility, @font-face is a better choice. It&#8217;s up to you!</p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2009/javascript/the-state-of-selectable-text-replacement/">The state of selectable text-replacement</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2009/javascript/the-state-of-selectable-text-replacement/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>gridBuilder.js, background grid generator in canvas</title>
		<link>http://kilianvalkhof.com/2009/design/gridbuilder-js-background-grid-generator-in-canvas/</link>
		<comments>http://kilianvalkhof.com/2009/design/gridbuilder-js-background-grid-generator-in-canvas/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 04:00:15 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=451</guid>
		<description><![CDATA[<blockquote><p>In order to correctly draw a 1px wide line, <em>on</em> a single pixel, you actually need to position it 0.5 pixel further.</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2009/design/gridbuilder-js-background-grid-generator-in-canvas/">gridBuilder.js, background grid generator in canvas</a></p>
]]></description>
			<content:encoded><![CDATA[<p>GridBuilder.js is a jQuery plugin that uses canvas to generate a grid background for arbitrary elements. Because it generates the grid via javascript and canvas, it&#8217;s much more flexible than using an image background.<span id="more-451"></span></p>
<h3>Canvas</h3>
<p>&hellip;is pretty easy. There are good guides available, not the least the one from Dive into HTML5, <a href="http://diveintohtml5.org/canvas.html">Let’s Call It A Draw(ing Surface)</a>. You need them, because there are a couple of gotcha&#8217;s. For example, to get different colored lines, you need to explicitly start new paths (but you don&#8217;t have to explicitly end them, just draw them on the canvas before starting another line). Once you get the way this works, it&#8217;s pretty much straightforward from there.</p>
<p>Another example is that in canvas, pixels are seen as small squares (yes, really.) When you draw a line from a pixel, it actually draws the line centered in the top-left corner of the pixel. When it renders, that means that the line gets blurred because it&#8217;s actually drawn on two pixels. This means that in order to correctly draw a 1px wide line, <em>on</em> a single pixel, you actually need to position it 0.5 pixel further. (so it&#8217;s in the &#8216;middle&#8217; of the pixel). It turns out, that sort of fails in Firefox (but not in Safari) because of a rounding error. If I used +0.5, Some lines just wouldn&#8217;t appear! However, if you just <strong>subtract 0.5</strong>, then everything works as expected.</p>
<h2><a href="http://gridbuilder.kilianvalkhof.com/">Download and demo</a></h2>
<p>The whole project is hosted at github as well: <a href="http://github.com/Kilian/gridBuilder.js">gridBuilder.js on Github</a>. gridBuilder.js is 4kb unpacked, uses jQuery and works in <strong>real browsers</strong>*. It&#8217;s dual licensed under the GPL and MIT licenses.</p>
<p>*A real browser here is defined as one that supports both <em>canvas</em> and <em>data-url&#8217;s</em>. The latter is being used to set the canvas as a background image for your element.</p>
<h3>Code</h3>
<p>Since it&#8217;s a <a href="http://jquery.com">jQuery</a> plugin, gridBuilder.js works for any jquery element. gridBuilder.js offers 5 customizable settings, a primary and secondary color for gridlines and secondary gridlines (positioned in the middle between two gridlines), a height for the vertical rhythm, a width for the horizontal strokes and a gutter (spacing) for between the strokes. </p>
<p>Entering <code>false</code> for any of the options will disable that particular option, so you can for example just have a vertical rhythm and no horizontal strokes, or disable the secondary gridlines. The default settings are shown below.</p>
<pre><code>$(document).ready(function(){
  $("body").gridBuilder({
    color:          '#eee',    // color of the primary gridlines
    secondaryColor: '#f9f9f9', // color of the secondary gridlines
    vertical:       18,        // height of the vertical rhythm
    horizontal:     140,       // width of horizontal strokes
    gutter:         40,        // width of the gutter between strokes
  });
});</code></pre>
<p>While this is a plugin you&#8217;d primarily use while building a website, should you want to remove it via javascript, you can call this function:</p>
<pre><code>$("body").destroyGrid();</code></pre>
<h3>Future improvements</h3>
<p>I would like to add a floating panel where you can enter all options and have the grid regenerate on the fly, as well as giving you the javascript code to invoke that particular grid. This would make it easier to tweak your grid, since you wouldn&#8217;t have to edit your file, save it and then refresh. </p>
<p>It&#8217;s hosted on <a href="http://github.com/Kilian/gridBuilder.js">Github</a>, so feel free to fork it and do with it whatever you like :)</p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2009/design/gridbuilder-js-background-grid-generator-in-canvas/">gridBuilder.js, background grid generator in canvas</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2009/design/gridbuilder-js-background-grid-generator-in-canvas/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cufón vs. Typeface.js, which one is better?</title>
		<link>http://kilianvalkhof.com/2009/javascript/cufon-vs-typefacejs-which-one-is-better/</link>
		<comments>http://kilianvalkhof.com/2009/javascript/cufon-vs-typefacejs-which-one-is-better/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 07:00:00 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=355</guid>
		<description><![CDATA[<blockquote><p>Cuf&#243;n and Typeface.js are very similar in use, with just a couple of key differences.</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2009/javascript/cufon-vs-typefacejs-which-one-is-better/">Cufón vs. Typeface.js, which one is better?</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Cuf&oacute;n and Typeface.js are both methods to show text in custom fonts (like sIFR), are both only a couple of months old and both use canvas or VML. So what&#8217;s the difference? And which one is better?<span id="more-355"></span></p>
<h3>How does it work?</h3>
<p><a href="http://cufon.shoqolate.com/generate/">Cuf&oacute;n</a> and <a href="http://typeface.neocracy.org">Typeface.js</a> both work by converting a regular font file into an internal format. On execution, they display the font using Canvas in modern browsers, and VML in Internet Explorer. The benefits of doing it this way is that you do not need to load the Flash plugin. This makes it faster compared to <a href="http://wiki.novemberborn.net/sifr/">sIFR</a>, and visible on more computers.</p>
<h3>Quick stats</h3>
<p>Cuf&oacute;n and Typeface are very similar, here&#8217;s a quick list of their properties:</p>
<table>
<tr>
<th></th>
<th>Cuf&oacute;n</th>
<th>Typeface.js</th>
</tr>
<tr>
<th>File size</th>
<td>14.0kb (compressed)</td>
<td>16.3kb (uncompressed)</td>
</tr>
<tr class="alt">
<th>Supported browsers</th>
<td>Firefox 1.5+, Safari 3+, Opera 9.5+, Google Chrome and Internet Explorer6+</td>
<td>Firefox 1.5+, Safari 2+, and Internet Explorer 6+, but not 8</td>
</tr>
<tr>
<th>Supported file types</th>
<td>.ttf, .otf, PFB, postscript</td>
<td>.ttf</td>
</tr>
<tr class="alt">
<th>Supported font styling</th>
<td>font-size, font-style, font-weight, line-height, text-shadow, color</td>
<td>font-size, font-style, font-weight, font-stretch, letter-spacing, line-height</td>
</tr>
<tr>
<th>Selectable text?</th>
<td>not yet</td>
<td>not yet</td>
</tr>
<tr>
<th></th>
<th>Cuf&oacute;n</th>
<th>Typeface.js</th>
</tr>
</table>
<h3>Font Conversion</h3>
<p>Both Typeface.js and Cuf&oacute;n have an online font converter:</p>
<ul>
<li><a href="http://Cufon.shoqolate.com/generate/">Font converter for Cuf&oacute;n</a></li>
<li><a href="http://typeface.neocracy.org/fonts.html">Font converter for Typeface.js</a></li>
</ul>
<p>You upload your font, select which glyph groups (which are sets like uppercase, lowercase, numbers, punctuation etc.) you want to include and it returns a .js file containing your font. Typeface.js only supports TrueType (.ttf) fonts. Cuf&oacute;n also supports OpenType (.otf), Printer Font Binary and postscript. The converter for Typeface.js has a build-in check for the font license, and will refuse to convert any font that doesn&#8217;t allow embedding.</p>
<p>On uploading, Typeface.js forwards to a page where you can select your glyphs and download your file. Cuf&oacute;n lets you select the glyphs on the first page, and also give you the option to include a bold and an italic variant in the same file. Besides that, it allows you to restrict usage to certain domains, and set the quality of the font (lower quality is faster performance).</p>
<p>I used both to convert Optimer with uppercase, lowercase, numerals, punctuation and extended latin. Cuf&oacute;n&#8217;s file is called <a href="/uploads/cufon-vs-typeface/Optimer_400.font.js">Optimer_400.font.js</a> and is <strong>27.3kb</strong> while Typeface.js&#8217; file is called <a href="/uploads/cufon-vs-typeface/optimer_regular.typeface.js">optimer_regular.typeface.js</a> and is <strong>68.7kb</strong>.</p>
<h3>Including Cuf&oacute;n or Typeface.js in your website</h3>
<p>Cuf&oacute;n and Typeface.js both work by linking to their javascript file and the font file in your <code>&lt;head&gt;</code>:<br />
<strong>Cuf&oacute;n</strong></p>
<pre><code>&lt;head&gt;
  &hellip;
  &lt;script src="cufon-yui.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="Optimer_400.font.js" type="text/javascript"&gt;&lt;/script&gt;
  &hellip;
&lt;/head&gt;</code></pre>
<p><strong>Typeface.js</strong></p>
<pre><code>&lt;head&gt;
  &hellip;
  &lt;script src="typeface-0.11.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="optimer_regular.typeface.js" type="text/javascript"&gt;&lt;/script&gt;
  &hellip;
&lt;/head&gt;</code></pre>
<p>To select which elements to convert, you have to add the following <code>&lt;head&gt;</code> for Cuf&oacute;n:</p>
<pre><code>&lt;script type="text/javascript"&gt;
  Cufon.replace('h1');
  //If you also include a library, you can add complex selectors like #content h1
&lt;/script&gt;</code></pre>
<p>Typeface.js does things a bit differently, and in my opinion, easier. Typeface.js automatically changes all heading elements that reference Typeface.js fonts (which is the first part of your font file name, in our case <em>optimer</em>, and any element that has the class <code>typeface-js</code>:</p>
<pre><code>&lt;div class="myclass <strong>typeface-js</strong>"&gt;
&hellip;
h1,  h2 {
  font-family:optimer;
}</code></pre>
<h3>Multiple fonts</h3>
<p>Both Typeface.js and Cuf&oacute;n allow you to load in multiple fonts. Typeface.js finds them automatically when you specify them in your CSS, and Cuf&oacute;n allows you to use multiple fonts by specifying the font-family in it&#8217;s initial function:</p>
<pre><code>Cuf&oacute;n.replace('h2', { fontFamily: 'Qlassik Medium' }); </code></pre>
<h3>Internet Explorer 7</h3>
<p>No party would be complete without someone trying to ruin it for everyone, so here&#8217;s internet explorer&#8217;s problem: for both Cuf&oacute;n and Typeface.js, the script can have a couple of milliseconds of delay, causing the text to &#8216;flicker&#8217;. To stop it from doing so, place the following code right before the closing <code>&lt;/body&gt;</code> body tag:</p>
<p><strong>Cuf&oacute;n:</strong></p>
<pre><code>&lt;script type="text/javascript"&gt; Cufon.now(); &lt;/script&gt;</code></pre>
<p><strong>Typeface.js</strong></p>
<pre><code>&lt;script type="text/javascript"&gt; _typeface_js.renderDocument(); &lt;/script&gt;</code></pre>
<h3>Side by Side</h3>
<p>Of course, all this is fun, but it would be nice to see them in action besides each other. No worries, I&#8217;ve made a <strong><a href="/uploads/cufon-vs-typeface/">Demo Comparison Page</a></strong>.</p>
<p>On rendering, you can see a number of spacing differences. There are especially noticeable when looking at the spacing in the typographic scale, where Cuf&oacute;n performs much better than Typeface.js</p>
<h3>Conclusion</h3>
<p>Which one is better? Cuf&oacute;n or Typeface.js? In terms of functionality, the difference is small. Cuf&oacute;n supports more filetypes, Typeface.js supports more CSS properties. Both however, support the stuff you&#8217;re most likely to need. Typeface.js is easier to use (just use the right font-family and, if it&#8217;s not a header, add a class), Cuf&oacute;n is about 40kb smaller in total and handles spacing better out of the box.</p>
<p>For now, Cuf&oacute;n performs slightly better due to handling spacing better, but I don&#8217;t think there is a clear winner. That just means that the race is on: <strong>the first one to add selectable text wins!</strong></p>
<h3>Next Week</h3>
<p>Come back next week for an article on how to <strong>combine these techniques with @font-face!</strong></p>
<h4>Translations</h4>
<p><a href="http://www.designcontest.com/show/cufon-vs-typefacejs-be">Belorussian translation</a></p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2009/javascript/cufon-vs-typefacejs-which-one-is-better/">Cufón vs. Typeface.js, which one is better?</a></p>
 <p><a href="http://kilianvalkhof.com/?flattrss_redirect&amp;id=355&amp;md5=46768805a77bb486c4cff4b0eea1269d" title="Flattr" target="_blank"><img src="http://kilianvalkhof.com/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2009/javascript/cufon-vs-typefacejs-which-one-is-better/feed/</wfw:commentRss>
		<slash:comments>86</slash:comments>
		</item>
		<item>
		<title>Fancyzoom image gallery functionality</title>
		<link>http://kilianvalkhof.com/2009/javascript/fancyzoom-1-1-1-image-gallery-functionality/</link>
		<comments>http://kilianvalkhof.com/2009/javascript/fancyzoom-1-1-1-image-gallery-functionality/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 01:00:11 +0000</pubDate>
		<dc:creator>Kilian Valkhof</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://kilianvalkhof.com/?p=338</guid>
		<description><![CDATA[<blockquote><p>Since I was adding next and previous links, I wanted the arrow keys to work as well.</p></blockquote><p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2009/javascript/fancyzoom-1-1-1-image-gallery-functionality/">Fancyzoom image gallery functionality</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Fancyzoom is pretty cool in a lot of ways, like it&#8217;s amazing zoom functionality modelled after OS X. The only thing missing was next and previous links for gallery functionality, so I implemented those.<span id="more-338"></span></p>
<h3>How it works</h3>
<p>This version is an edit of the original Fancyzoom, available at <a href="http://www.fancyzoom.com">http://www.fancyzoom.com</a>.</p>
<p>I&#8217;m used to writing JavaScript with a library like jQuery or Prototype, but since Fancyzoom was library-free, I decided not to add additional dependencies. <em>Turns out, regular JavaScript makes you type a whole lot more.</em> Here&#8217;s what I did:</p>
<p>For next and previous links to work we need to know which images are next and previous, and even <em>if</em> there is a next or previous image. So on initialisation I compose an array of &#8216;candidate images&#8217;. When you then open an image, it checks the position of the current image in the array, and adds or hides the previous/next links accordingly. The first image doesn&#8217;t get a previous, the last image doesn&#8217;t get a next, everything in between gets both links. </p>
<p>When displaying the links, I add the corresponding image number to the previous and next links (current image number minus one for previous, and plus one for next). When you click either of the links, it finds the image in the array, sets up an onload event, and displays the loading spinner. Once the image is loaded, I remove the spinner and update the previous and next links.</p>
<h3>Keystrokes</h3>
<p>Now, one of the cool things of Fancyzoom is it&#8217;s keystrokes. <code>esc</code> closes the image overlay, <code>ctrl</code> and <code>alt</code>  are click-throughs (no overlay, just open the image). Like in OSX you can press shift while clicking to make the zoom effect go really slow. Since I was adding next and previous links, I wanted the arrow keys to work as well.</p>
<p>I started with <a href="http://www.quirksmode.org/dom/events/keys.html">PPK&#8217;s key reference page</a> to see which charcode I had to use. PPK then gave me <a href="http://twitter.com/ppk/statuses/1230393621">a smart tip</a>: &#8220;Use keydown, not keypress. Saves you a lot of hassle.&#8221;. Fancyzoom used keypress, which indeed make things more annoying to do cross-browser. I changed it to keydown, and it worked without having to change any of the other code. In the end it worked so well that for kicks I also added the spacebar as a way to go to the next image.</p>
<p>The above is of course only a cursory view of the changes made, but the fancyzoom.js and fancyzoomhtml.js files themselves are well documented so with minimal JavaScript knowledge you should be able to follow what&#8217;s happening. (and if not, don&#8217;t hesitate to ask!)</p>
<h3>Download</h3>
<p>Fancyzoom exists out of two files, and I added a third CSS file to it (which you can just copy/paste into your own css file)</p>
<ul>
<li><a href="/uploads/fancyzoom/fancyzoom.js">Fancyzoom.js</a></li>
<li><a href="/uploads/fancyzoom/fancyzoomhtml.js">Fancyzoomhtml.js</a></li>
<li><a href="/uploads/fancyzoom/fancyzoom.css">Fancyzoom.css</a></li>
</ul>
<p><strong>License</strong>: this is <strong>free for personal use</strong>. For all other uses, refer to the full license in the fancyzoom.js file.</p>
<p>You can see it in action on the numerous software pages on Wakoopa, for example the <a href="http://wakoopa.com/software/safari">Safari page</a> or the <a href="http://wakoopa.com/software/openoffice">Openoffice page</a>.</p>
<p><strong>Let me know what you think, and if you have improvements I&#8217;d love to hear them!</strong></p>
<p>This article is written by Kilian Valkhof, Front-end developer & User experience designer. Please visit <a href="http://kilianvalkhof.com">KilianValkhof.com</a> for more articles!<br/><br/><a href="http://kilianvalkhof.com/2009/javascript/fancyzoom-1-1-1-image-gallery-functionality/">Fancyzoom image gallery functionality</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kilianvalkhof.com/2009/javascript/fancyzoom-1-1-1-image-gallery-functionality/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

