Thomas Fuchs
Hi, I'm Thomas Fuchs. I'm the author of Zepto.js, of script.aculo.us, and I'm a Ruby on Rails core alumnus. With Amy Hoy I'm building cheerful software, like Noko Time Tracking and Every Time Zone and write books like Retinafy.me.
   Want me to speak at your conference? Contact me!

Learn to create amazing mobile HTML5 sites and apps

June 15th, 2011

Today we launched HTML5 Mobile Pro Download Edition — a great way to learn how to code HTML5 sites and apps for mobile devices, especially those that sport WebKit-based browsers. Based off our successful workshops, you can now enjoy the videos and materials at any time you wish—and for just $99 while we’re in beta (it’s the full product, but we’ll ship a polished version in August, which you will get for free if you get the beta!). That’s just a third of the price of our regular workshop costs!

The 1.3GB download comes with 3+ hours of video-based teaching with Amy Hoy and yours truly, hundreds of slides (PDF, full-screen and print-ready versions), and tons and tons of demo code.

Best of all, and kind of sad that I need to stress this, the videos can be easily transferred to your iPad, phone or other device, as we haven’t crippled HTML5 Mobile Pro Download Edition with any kind of silly DRM. You can also print and copy+paste from the PDFs to your heart’s content, no silly passwords required.

Here’s a fancy video (browser that supports HTML5 video required to play):

Psst! As a reader of my blog I’ll give you extra $15 off, just use coupon code HTML5YAY. That’s $15 off the beta price, so you’ll save $45 off the final price when we ship the final version in August!

Learning by exploring

June 14th, 2011

To learn a language by heart, try to set some time aside to explore it! Set yourself a specific goal—for example, find three ways to convert a character code to a string in JavaScript.

Here’s the basic set up, with what probably makes the most sense in almost any situation.

var charCode = 65;
String.fromCharCode(charCode) // => "A"

Where to go from here? It seems, at first that there is no other way to achieve this, right? Wrong, of course.

As a scripting language, JavaScript has powerful ways to evaluate generated code. Let’s try using the eval function to do this. Remember that JavaScript strings can contain escape sequences, for example in the form of xAB, where AB is a hexadecimal character code:

eval("'\x"+charCode.toString(16)+"'") // => "A"

An here’s a third way to do it, using the unescape DOM method:

unescape('%'+charCode.toString(16)) // => "A"

There are even more possible ways to achieve this conversion—try to find one!

Zepto.js v0.6 – DOM goodies & cleanups, experimental Firefox mobile support

May 14th, 2011

Zepto has seen some great bug fixes, jQuery compatibility improvements and of course some new features lately, so it’s time for a new release, Zepto.js v0.6 is now out for your download pleasure.

We’re holding at 141 forks and 1,507 watchers on GitHub—keep the goodness coming in.

Here’s what new since v0.5:

  • appendTo() and prependTo()
  • replaceWith()
  • empty(), add(), submit()
  • function arguments to add/remove/toggleClass
  • lots of cleanups and improvements across the board; especially regarding Ajax and Events

More stuff is coming as we are nearing v1.0, and big names are starting to pick it up; for example the next iteration of Zappos’ mobile site will use Zepto (not online yet!). Learn a bit more by having a look at my recent presentation on Zepto at JSConf 2011.

Also of note, Zepto now has experimental compatibility with Firefox mobile (proceed with caution—largely untested!).

Big thanks to all the contributors—please keep it coming!

Zepto! slides from my JSConf 2011 presentation

May 6th, 2011

Here are my slides from JSConf 2011 in Portland, Oregon. I was talking about Zepto, my micro-framework, WebKit-only (works on Firefox too, though) clone of the jQuery API, and why I think that Micro-Frameworks and Micro-Libraries are better suited for the mobile Web than traditional, monolithic libraries, like script.aculo.us, Prototype or jQuery.

The talk also marks the announcement of microjs, a website for discovering small and to-the-point JavaScript libraries.

You can download the slides as PDF (24MB). (View the video on slide #7)

P.S. Of course, just the slides don’t give you the whole picture—there will be a video of the talk at some point, too!

Microjs, a directory of Micro-Frameworks and Micro-Libraries

May 5th, 2011

I’m thrilled to announce the launch of microjs, a directory of JavaScript Micro-Frameworks and Micro-Libraries, something that grew out of some reactions to my post on micro-frameworks in early April.

How much library code do you really need — 50K? 100K? 150K? More? How much of that do you really use?

Sure, we all love our favorite monolithic frameworks, and sometimes we even use them fully. But how often do we reach for the ride-on John Deere tractor with air conditioning and six-speaker sound system, when a judiciously applied pocketknife would do the trick better, faster, slicker?

Micro-frameworks are definitely the pocketknives of the JavaScript library world: short, sweet, to the point. And at 5k and under, micro-frameworks are very very portable. A micro-framework does one thing and one thing only — and does it well. No cruft, no featuritis, no feature creep, no excess anywhere.

Microjs.com helps you discover the most compact-but-powerful microframeworks, and makes it easy for you to pick one that’ll work for you.

Want to add your own? Fork it on GitHub, add your framework to data.js and submit a pull request.

Microjs was released as part of JSConf 2011 in Portland, Oregon, and I will release my talk slides and comments in an additional blog post later this week.

A big thank-you goes out to Dustin Diaz of Twitter who helped me sanity-check everything and is busy improving Microjs; and my awesome wife Amy Hoy for providing great copy and moral support!

Last but no least, thanks to all the contributors who already added their favorite libraries—keep it coming!

I for one welcome our new micro-framework overlords

April 11th, 2011

In the beginning there were the script kiddies and everyone saw that things were pretty bad. So people dismissed the ideas of mouse trails and wobbly status lines and, more-or-less quickly, moved on to actually useful libraries, the first of which being Prototype.js back in 2005, followed by many others. Over the years, one of these libraries, jQuery has become the de-facto standard for JavaScript frameworks on the web (but others continue to be used).

All of these monolithic libraries have one thing in common: they try to solve the same problem, and the scope of this problem is a big one. It’s not easy to provide a fully cross-browser environment and API for DOM manipulation, data crunching, class-based OO and more. And this shows in code bloat. These libraries are HUGE, being 100+ kilobytes in code size, and even minified and gzipped they are pretty big.

What’s wrong with that? A whopping 100% of sites or apps using these libraries don’t use all the features they provide. That wouldn’t be so bad if we didn’t have to transfer these libraries to the client every time. Some solutions exist for making it better (say, Google’s Libraries API), but it doesn’t really get down to the root of the problem.

I for one welcome our new micro-framework overlords—small frameworks and libraries that do one thing only, not trying to solve each and every problem, just using pure JavaScript to do it. Easy to mix and match, and to customize to your needs.

Micro-framework group hug!

(cc) jans canon — Micro-framework group hug!

Here is just a small selection of these micro-frameworks (this list is far from complete):

What you’ll find these libraries have in common is an emphasis on small download size.

Small code is good code: the fewer lines in your source, the fewer bugs you’ll have, plus it will download and execute faster.

And an other big advantage is that all the building blocks work stand-alone, which counteracts the this needs to be implemented as a jQuery plug-in for no reason mentality anti-pattern.

There’s even work being done on meta-micro-frameworks, like Ender.js that strive to provide a “homogenized” mix of useful micro-frameworks that gets you started quickly, but you can later cut off parts or replace modules.

Awesome.

How to play a sound in a web browser (it ain’t easy)

March 16th, 2011

So with all the super-modern new amazing, astonishing technology at our fingertips—how hard could it be to play a single sound effect in a web browser?
The answer is: it’s hard, painful, and you can’t get it to work in all cases anyway.

Of course there’s the venerable SoundManager 2 library. But that’s 3000 lines of JavaScript code, plus a Flash file and more code, and… I just don’t think that you need 3000 lines of code just to play a sound effect. I refuse.

Why play sounds? Some user interfaces really benefit from audio feedback. In my case, it’s a feature so that people that use the Freckle Timer will get an audible ping every 15 minutes so they don’t forget that it’s still running (it’s optional of course!).

Anyway, script.aculo.us (version 1) actually came with experimental sound support, but that code is from a time when there was no HTML5, yet alone HTML5 audio yet. Let’s go with that code but update it to use HTML5 when possible.

Here’s look at various browsers and what they support:

Internet Explorer

Surprisingly, it’s pretty easy to play a mp3 on IE, even on version 6. The little known bgsound element does it for you, no questions asked. On IE9, with all the amazing HTML5 support, you can also use an audio tag, but IE9 misreports that it can play mp4 files when it can’t.

Safari

Can play mp3’s and m4a’s just fine with the audio tag, but behold: On Windows, if QuickTime is not installed also, not only will it not play any codecs, but the audio tag simply doesn’t exist, and the Audio object in JavaScript doesn’t either.

Firefox

Has support since 3.5, but it will only play OGG and WAVE files. Go figure.

Chrome

Has broad support and will play most stuff thrown at it. Also, no cross-platform differences.

Mobile devices

You can’t auto-play sounds, so forget about playing sounds as part of the UI now. On some devices (namely recent iOS devices) you can play sounds as a result of a direct user interaction (e.g. tap). It’s out of the scope of this article really.

It all boils down to:

When HTML5 is there, do this:

First, you need mp3, mp4 and ogg versions of your sound.

if("Audio" in window){
  var a = new Audio();
  if(!!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, '')))
    a.src = "/sounds/ping.ogg";
  else if(!!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, '')))
    a.src = "/sounds/ping.mp3";
  else if(!!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, '')))
    a.src = "/sounds/ping.m4a";
  else
    a.src = "/sounds/ping.mp3";
  
  a.autoplay = true;
  return;
}

Basically, this will attempt to play a sound through an audio tag, and knows about IE9’s problem of misreporting that it plays mp4s. If all else fails, it will still attempt to play the mp3 version of the sound.

When there’s no HTML5 around, do this

1. In case you’re on IE, use a bgsound element:

<bgsound src="/sounds/ping.mp3" loop="1" autostart="autostart">

2. Else, test for the availability of various plugins and generate object/embed tags:

// this code uses Prototype.js
if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('QuickTime') != -1 }))
  Sound.template = new Template('<object id="sound_#{track}_#{id}" width="0" height="0" type="audio/mpeg" data="#{url}"/>');
else if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('Windows Media') != -1 }))
  Sound.template = new Template('<object id="sound_#{track}_#{id}" type="application/x-mplayer2" data="#{url}"></object>');
else if(navigator.plugins && $A(navigator.plugins).detect(function(p){ return p.name.indexOf('RealPlayer') != -1 }))
  Sound.template = new Template('<embed type="audio/x-pn-realaudio-plugin" style="height:0" id="sound_#{track}_#{id}" src="#{url}" loop="false" autostart="true" hidden="true"/>');

Basically, the code tries to use QuickTime, Windows Media and RealPlayer to play back the sound. You could add a Flash failover, but for that you need specially crafted Flash files, and then you get yourself into the whole Flash blocker madness. Avoid.

Little helpers: a tweet-sized JavaScript templating engine

March 9th, 2011

JavaScript libraries don’t have to big and clunky to be useful. Often, less is more. I’m doing a lot of work with single-page apps and short-lived event sites and visualizers—often coming up with some helper code while doing it, because it’s faster to implement small things yourself then to search for existing solutions, with none of them really doing what you want.

An example? Templating. While there’s a gadzillion more or less awesome templating engines for JavaScript out there, in one case I needed something übersimple—and tried to fit it into a tweet (for no particular reason).

function t(s,d){
 for(var p in d)
   s=s.replace(new RegExp('{'+p+'}','g'), d[p]);
 return s;
}

Call it like this:

t("Hello {who}!", { who: "JavaScript" }); 
// "Hello JavaScript!"

t("Hello {who}! It's {time} ms since epoch.", { who: "JavaScript", time: Date.now }); 
// "Hello JavaScript! It's 1299680443046 ms since epoch."

If I’d need larger templates, I’d probably go with one of the engines that allows you to store HTML templates in <script> tags, but for my use case (just some messages) this worked beautifully.

Update Thanks to Daniel Herzog to point out that replace is automatically executing functions, which basically cut my code in half. Awesome.

DOM Monster 1.3: save results for later with Jdrop

February 16th, 2011

DOM Monster is now at 1.3, and this version brings support for Jdrop, a “JSON in the cloud” service launched by fellow performance guru Steve Souders.

It’s easy— just go to Jdrop, connect with Twitter or Google, and then use the good old DOM Monster bookmarklet anywhere. You’ll notice that there’s a new “save to Jdrop” link. Just click it and boom! — it stores the results on Jdrop, so you can revisit the results later.

The most awesome thing about this is that you can use the DOM Monster on your mobile device, and then visit Jdrop on your desktop browser to easily analyze the results!

Here is Steve’s post introducing Jdrop.

Also new are some updates and bug fixes to better parse JavaScript library version numbers, and we have more cool stuff planned! Feel free to participate by submitting bug reports, patches and documentation on the DOM Monster’s GitHub site.

Developing web apps in 2011

February 12th, 2011

What tools are the best for web development these days? It depends, of course, on what you do and what your personal preferences are. But let me tell you what I use and what works best for me, and most importantly, why I think it works best for me.

Zenith Z-19 Terminal by ajmexico

These days are over! Or are they? — (cc) ajmexico

Software

IDEs are not my kind of idea of how software should be developed. If your programming language, toolkit or library is so extensive and complicated that you need an IDE to develop, something is wrong. Small is beautiful. A plain old text editor is enough to make the world’s most amazing web applications.

My development work basically falls into two distinct categories, bugs/features for existing products and open source software; and rapid prototyping (there’s also a huge chunk of administrative work of course).

Bugs and features for our existing products/OSS

  • MacVIM rules supreme as my editor of choice here, not only it has great customizability and comes loaded with all sorts of helpers, it also can run full-screen, which makes it easy to concentrate on a bit of code, figuring out bugs, and so on. Be sure to grab PeepOpen, the ideal companion app.
  • I develop exclusively in Safari. Safari’s Web Inspector is quite an amazing tool to help you get stuff done. I know that many developers prefer Google Chrome, but most just subscribe to Google’s Kool-Aid, while overlooking the issues it has. It’s not as stable as it could be (at least on OS X), doesn’t support hardware-acceleration and 3D CSS properly (I do a lot of work with advanced animations), has inferior typography and the JavaScript engine is actually slower than Safari’s in a lot of real-world situations. Firefox, sadly, is no longer an option as Firebug tends to be buggy and sluggish (Firefox 4 is coming along rather nicely, tho!)
  • In addition, I find Benchmark.js, UglifyJS and the DOM Monster incredibly helpful for JavaScript and web development.
  • For bug-tracking and support we use our own app, Charm, (it’s awesome, and it will be out soon!)
  • Our code is safe and sound with GitHub.
  • For cross-browser testing, I use VMware Fusion. I have 5 virtual machines installed, side-by-side, Windows XP with IE6, Windows XP with IE7, Windows 7 with IE8, Windows 7 with IE9 RC, and the latest Ubuntu. These machines are set up in a way to be able to connect to the host (OS X) operating system’s file and Apache servers.
  • Other tools and helpers I use include zsh and oh-my-zsh, Homebrew, Transmit, Propane and Skitch.
  • Adobe Photoshop and Illustrator are still pretty much a given for any serious web design; with some contenders lining up to (hopefully) change this in the not too distant future.

Rapid prototyping of new products

As above, but I prefer TextMate here. For rapid prototyping I have a much more visual approach on where to add code and files, and TextMate’s project window is pretty great for this.

Hardware

There are two options, either you need to be fully mobile or not. Right now, I’m travelling a lot and basically live in two places, so I need a complete development setup on me. There are some constraints here:

  • The hardware must be powerful enough not to make me wait in most if not all situations
  • I need to be able to run multiple Virtual Machines (see Software, above), without slowdown (also, these need quite a lot of hard drive space)
  • Needs to run cool and quiet (I hate fans), with good battery life
  • Need to support all of the previously mentioned software
  • Screen size should be big enough to comfortably work on for extended periods of time
  • For cross-browser testing, you need to be able to run certain browser/OS combinations: IE6, 7, 8, 9 on Windows; Firefox on Linux, Windows, Mac; Safari on Windows, Mac; Chrome on Linux, Windows, Mac; and perhaps Opera and other exotic browsers, too.
  • For mobile web development, simulators or emulators for iOS, Android and webOS should be available.

Basically, what I have is the latest model maxed-out MacBook Pro 15″, with the largest possible SSD drive. I update my computer about once a year. This may seem excessive, but it’s the single most expensive tool I use, and in the overall scheme of things, it’s what I’m sitting in front of every day, for hours. If you’re not getting the very best thing that’s out there, you’re just hurting yourself in the long run.

Your health, well-being, happiness and productivity will be worth the investment.

If you’re asking yourself whether or not to get an SSD, you’re asking the wrong question. Ask yourself whether or not you want to copy 20GB in 3 minutes, if you want to boot in 10 seconds, if you want applications to start instantly, if you want the laptop to be inaudible, if you want no head crashes and if you want longer battery life. The answer is, of course you do, so get the SSD. I just use the Apple-provided 512GB SSD, but if you feel more adventurous you can get even better 3rd-party SSD drives.

One reason to not get 3rd-party stuff is Apple’s incredible support. Anywhere I travel in the world where there’s either Apple Stores or approved resellers, I can get my laptop fixed if something breaks, usually in no time at all. No other manufacturer provides service like this, so conveniently. If all the stuff that’s in the laptop was part of my original order, it’s easier for them and for me to repair it.

In both of my offices, I have external screens, and I can highly recommend the 27″ Cinema Display, because the screen size and ratio is really comfortable to work with, the picture quality is amazing, and the built-in camera, speakers and power for the laptop make it super-convenient.

I use the Apple full-size keyboard (US layout) and a Magic Mouse. I also do use the Magic Trackpad some, as I find it works very well in tools like Photoshop when I need precise control. It just feels more direct than using the mouse there.

For backing up important files I use Dropbox, just don’t be their marketing droid and get the paid plan and be happy. Plus, I’ve time machine backups in place on both sides of the Atlantic, using this 2TB external hard drive.

If I was to live in just one place, I’d probably get an iMac or a Mac Pro; and a MacBook Air for mobile use. The next gen MacBook Airs that are rumored for June might just make me do the switch to a 13″ Air from the MacBook Pro, as these should be powerful enough for my multiple-VM setup.

Oh, and I have an old-school desktop calculator as I just like pressing real buttons from time to time. And headphones. And I’m a sucker for great desk lighting.

But wait, there’s one more thing…

Software development is so much more than just computers and software and hardware.

You want to be in a place that sparks your creativity, that gets you off your lazy ass, that inspires you and where you can relax, too.

More important than any computer you can ever buy is surrounding yourself with the right people. If you’re working from an office cubicle, with all your co-workers just waiting for 5pm so they can go home, you just can’t create great software. Same goes if you’re stting at home, all by yourself, brooding over the keyboard. Not possible. We found our development nirvana in Philadelphia, at IndyHall.