I'm Aaron and I make stuff that goes on the internets. We can be Twitter buds, and my feed is here.

Get Harvest: The best time tracking and invoicing app for web designers and developers.

Rounded Corners for Images with CSS 3 and jQuery

Jun 17

Rounded corners with border-radius is one of the most used CSS 3 enhancements, but it doesn’t reliably work on images. There’s a way around that, however. That’s what this tutorial is about. I didn’t invent this general technique, but the few examples I’ve found were either too messy or failed to work with linked images.

Ok then, let’s start with an image from a parking garage near me:

Let’s first try the easy way of adding border-radius styles to the image itself:


Those of you using Webkit browsers (excluding Mobile Safari) will see rounded corners. The rest of you will likely see no change at all. Even if we have it believe it is a block-level element, it still doesn’t work. What we’re going to do is create a new element, round its corners, and set its background to the original image.

First let’s define a basic CSS class for any image we want to apply rounded corners to.

.roundedimage {
     display: inline-block;
     -webkit-border-radius: 10px;
     -moz-border-radius: 10px;
     border-radius: 10px;
}

And the script to do the work of converting the image.

$(document).ready(function() {
	$(".roundme").each(function() {
		$(this).one("load",function() {
			var url = $(this).attr("src");
			var imageHeight = $(this).height();
			var imageWidth = $(this).width();
			$(this).css("visibility", "hidden");
			$(this).wrap('');
			$(this).parent()
				.css("width",imageWidth)
				.css("height",imageHeight)
				.css("background-image","url("+url+")");
		});
		if (this.complete) $(this).trigger('load');
	});
});

This script looks for anything with classname “roundme” and assigns a function to run when it is loaded. This function stores the source location and dimensions of the image, turns the visibility off, and wraps it with a span. This new wrapper element is then given the image’s dimensions and source URL for its background.

Some browsers do not fire the load() function if the image has been cached. This is a problem which would cause the whole technique to fail. The this.complete statement at the end addresses this issue. To prevent the load() function from potentially executing twice, all we need to do is make sure to attach the load() event handler using the one() method (it’s just like bind() only it automatically unbinds itself right after it runs the first time).

Now we just attach the “roundme” class to any image we want rounded for all browsers:


The jQuery above transforms this into:


Rad. But, what if the image is linked?


It’s cool. We wrapped it in a span so it’s still valid — hooray! I’ve already found a use for this for an upcoming mobile launch of Coolspotters.com. Hopefully others can benefit as well.

Push effect: Improving visual feedback for Digg style social voting tools with jQuery

Feb 3

digg.tools

Digging, voting, buzzing, retweeting, liking, bumping, shouting. These ubiquitous social tools have become the de facto mechanism to determine collective popularity. There’s little variation in terms of visual feedback when you engage these tools to increase an item’s popularity by one unit. Most just update the value instantly while others (Digg) perform a little fade effect.

Years ago I had done some push effects with live scoring applications for ESPN. I like this form of feedback when a numeric value increments. More recently I had created a UI for the CoolPapers “like” tool that uses a similar push effect. In this tutorial I’ll create a new social voting tool called Flippit to demonstrate this:

preview

See a demo here.

Markup

Like anything, this can be done a number of ways. There’s not much semantic relevance here, so any sensible structure will do. The only real requirement is to have enough elements for styling and animation.

293Flips
Flippit

Background & CSS

I’ll just use one sprite for this tool. The top will serve as the background for the numeric value, the bottom area gives us backgrounds for the unclicked, hover and clicked states of the action button.

spritepreview

There’s nothing too tricky or innovative in the CSS below. However, the key technique here is the overflow:hidden rule on the “votecard” class’s child div (line 10). This will effectively serve as a mask which the animating objects will move across. More on that later.

.votecard {
	background: url(images/sprite.png) no-repeat 0 0;
	padding: 4px;
	width: 63px;
	height: 43px;
	text-align: center;
}
.votecard div {
	position: relative;
	overflow: hidden;
	width: 63px;
	height: 43px;
}
.votecard em {
	display: block;
	position: relative;
	width: 63px;
	height: 33px;
	padding: 6px 0 6px 0;
	font: normal 24px/24px "Helvetica Neue","Helvetica","Arial",Sans-serif;
	color: #45453f;
}
.votecard strong {
	font-weight: bold;
}
.votecard span {
	font-size: 10px;
	line-height: 10px;
	display: block;
	color: #9a9a94;
}
a.voteaction {
	margin: 0 0 0 3px;
	display: block;
	text-indent: -9999px;
	width: 71px;
	height: 21px;
	background: url(images/sprite.png) no-repeat -3px -75px;
}
a.voteaction:hover {
	outline: none;
	background-position: -3px -54px;
}
a.voted,
a.voted:hover {
	outline: none;
	background-position: -3px -96px;
	cursor: default;
}

jQuery

Now we’ll have some real fun and wire this up.

$(document).ready(function() {
	/* create a node for the flip-to number */
	$(".votecard em").clone().appendTo(".votecard div");
	/* increment that by 1 */
	var node = $(".votecard em:last strong")
	node.text(parseInt(node.text())+1);

	function flip(obj) {
		obj.prev().find("em").animate({
			top: '-=45'
		}, 200);
		obj.toggleClass("voted",true);
	}

	$('.voteaction').bind({
	  click: function(event) {
	    event.preventDefault()
	  },
	  mouseup: function() {
	    flip($(this));
		$(this).unbind('mouseup');
	  }
	});

});

Content duplication

You might have wondered why the next number (294) was not included in the original markup shown earlier. The answer is found in line 3 through 6. This code duplicates the <em> node containing the current vote value (293) then increments that value by one. This new <em> node is forced to flow beneath the original one, but is hidden from view as specified in the CSS above. Here’s what your browser sees after this code runs:

duplication

Button events

Two events will be bound to the “Flippit” button, and you’ll see why these events are kept separate shortly. The default browser response is disabled in the click event. The mouseup event handler will call the flip() function which will perform the animation. It is important to allow this function to be called once only. After the user has initiated a “flip”, this action should be immediately disabled.

Had we grouped the event.preventDefault() together with the flip() call in a single button event (on click for example), we’d reinstate the default browser response when we unbound that event from the button. This would likely result in a page refresh if the user clicked the button twice. Keeping these events separate lets us unbind just the flipping action while preserving event.preventDefault().

Flip tha script *

The flip() function’s task is to select both <em>s and then move them up a distance of 45 pixels. Note also that this function takes as an argument a jQuery object reference to the button which called it. From this, we can traverse to select the <em>s we want to animate. The top <em> will slide out of view, while the jQuery generated one slides into view. The process is completed by toggling the button’s class name to “voted”.

What’s this good for?

This adds a little tactility to these contraptions. It reinforces the notion that you just impacted a piece of content on the web, if only by one solitary unit. If it’s good enough for sports apps, it’s good enough for anything else.

This technique could be used for anything where a numeric value increases or decreases whether it results from user interaction or not (as in live scoreboards).

View the demo (Works in IE6, IE7, IE8, Safari 4, Chrome, Firefox 3.6)

Download files

$237 million Avatar movie’s font is… Papyrus?

Dec 23

Is it really true that nobody came up with an original, illustrated title for James Cameron’s insanely expensive blockbuster movie? Let’s take a look at the title itself. This is from a screen capture taken from one of the HD trailers:

title

It’s got a nice glow effect with some color variations within the glyphs. I think it achieves that eco-spiritual, aboriginal feel well enough. The obvious font that looks like this is Papyrus, a font found on every Windows and Mac out there. Here’s a quick and dirty copy I made in Photoshop using that ubiquitous font (without going through the work of replicating the effects):

title.papyrus

Hmm. Not really a match. The details are clearly different. They must have set aside some of that massive budget to hire a talented typographer to give Cameron’s film an original word mark. I wonder, though, if it were just thickened up a bit with the old “faux bold” text modifier:

title.papyrus.bold

Closer, but still not quite. But wait… They do both have random nicks and notches in the letters to help give it them a weathered, pitted effect. A closer comparison:

notches

Everything lines up perfectly. Well that settles it. Avatar’s font is definitely Papyrus with some path distortions done in something like Illustrator. They did at least try to fool us by making the A’s unique from one another:

avatar.variation

Still, I can’t help but be a little disappointed at this. Oh well. Fantastic movie otherwise.

Twitter reflects our dreadful collective geopolitical IQ

Aug 9

“I hope not, but I wouldn’t put it past this stupid country.

That’s what liberal commentator & comedian Bill Maher recently said in response to CNN’s Wolf Blitzer when asked if he thought Sarah Palin had a chance of being elected President of the United States. This caused a negative response from viewers. Maher’s rebuttal to his hate mail is quite amusing in itself and in my opinion worth a read. He cites a few statistics to support his position:

  • Almost half of Americans don’t know each state has two senators
  • The average voter thinks foreign aid constitutes nearly a quarter of the federal budget (it’s less than 1%)
  • Most Americans cannot name any of the branches of the federal government
  • 24% could not name the country we fought for independence from in the Revolutionary War
  • Two-thirds don’t know what Roe vs. Wade established

Maybe he was just exaggerating? I doubt it. Here are a few more alarming figures that he didn’t mention:

  • Over 40% of Americans think Earth is less than 10,000 years old
  • 53% of adults know how long it takes for the Earth to revolve around the Sun
  • 75% of 18-24 year-olds are unable to locate Israel on a map of the Middle East

I don’t know if, collectively, the United States is a “stupid country.” But I wouldn’t be shocked if the number of Americans that could name the last three American Idol winners outnumbered those that could name one justice on the Supreme Court by 5 to 1. We know lots of things, we just don’t necessarily know things that matter.

Twitter as a window to our civic literacy

Could we find any anecdotal evidence of Bill Maher’s findings in our day to day culture? If we use Twitter as an imperfect gauge of the nature of public discourse, then I’ll answer this question with a question: What do the Prime Minister of Isreal, the Chairman of the Joint Chiefs of Staff, the Prime Minister of Canada, the US Senate Majority Leader, the United Nations Secretary General and the Deputy Prime Minister of Iraq have in common? They have less Twitter followers, combined, than Haylie Duff. No, not Hilary Duff. We’re talking about her less successful older sister here.

Here’s the follow count breakdown (as of today) of those I mentioned above — people that have a direct hand in shaping the world we live in:

PM of Israel Benjamin Netanyahu (@netanyahu): 2,026 *

Adm. Mike Mullen, Chairman of the Joint Chiefs of Staff, highest ranking officer in the US Military (@thejointstaff): 5,363

Steven Harper, PM of Canada (@pmharper): 17,189

Harry Reid, Senate Majority Leader (@SenatorReid): 1,591

Ban Ki-moon, Secretary General of the United Nations (@secgen): 4,131

Barham Sali, Deputy PM of Iraq (@BarhamSalih): 2,498

* Netanyahu seems to be on a hiatus from tweeting

I admit this is a small unscientific sample to illustrate a point. NPR Politics is now over 1 million and Barack Obama will eclipse 2 million followers soon (but he’s still tied at the moment with the likes of Ryan Seacrest). How influential is Ashton Kutcher on Twitter? He has more followers than the entire US Congress combined.

As for Bill Maher (@billmaher), he weighs in at nearly 28,000 followers. Not bad for sending only seven total tweets over the last three months.

Sarah Palin’s new Twitter username

Jul 29

Clearly @AKGovSarahPalin is no longer valid. So what will it be?

I hereby offer some initial suggestions. I’m not feeling particularly creative or mavericky at the moment, so surely there must be better ideas out there. What’s yours?

Tweet®

Jul 1

We sort of knew this was coming. You can use Tweet in your app’s name, as long as your app doesn’t suck.

I suppose that somewhat puts to rest my earlier post about Twitter nouns and verbs.

What I learned from Transformers 2

Jun 25

Overweight or unattractive college girls don’t exist anymore. Every one of ‘em is a dime. (Real factoid: both the University of Pennsylvania and Princeton did not want to be named in the movie because they disapproved of a scene in which Sam’s mom gets high.)

Davis Monthan AFB in Arizona is right outside the hangar doors at the Air and Space Museum in Washington, DC.

If you have a ton of slow motion footage of Megan Fox running towards the camera, just break it up with shots of dueling decepticons in the desert and you got yourself a movie.

They can create realistic looking transformers mixed in with live action, but the special effects technology required to lengthen Megan Fox’s short thumb remains elusive.

Even if only by chance, try not to wear this shirt the day you go see it, or you’ll feel like some sort of Shia Labeouf fanboy.

transformers_shiatransformers_me

Visualizing Persian public opinion: The Iranian blogosphere map

Jun 12

blogmap1

Really interesting visualization of the social/political landscape in Iran at the moment using computational social network mapping. It’s current enough to reflect public opinion on the day of the election in Iran. Here’s how the map is drawn, according to the creators:

“Our map of the Iranian blogosphere is a social network diagram that shows the relationships among the many kinds of bloggers active in Iran. It combines two analytic techniques to build a visualization that makes this very complex network more easily understood. The first technique is to draw the position of the blogs, each represented by a dot, using what is called a “spring embedder” or “physics model” algorithm. This map uses a particular algorithm known as Fruchterman-Rheingold, after its inventors. Imagine there is a wind trying to blow all the blogs to the edge of the circle, but any two blogs that link to each other are pulled together as though by an invisible spring. This causes groups of densly interconnected blogs to cluster together into “network neighborhoods.” The arrangement of dots on the map, in which the size of each dot reflects the number of other blogs linking to it, shows these network neighborhoods, or “proximity clusters.” The second technique involves statistically grouping blogs according to what they link to over a long period of time, i.e. what websites the bloggers prefer. These groups, which we call “attentive clusters,” are indicated by the colors of the dots. Blogs of the same color tend to link to similar websites.”

HOME: A beautifully filmed documentary

Jun 6

I can’t say enough about this film. It’s simply remarkable.

HOME is non-profit, 100% free documentary produced by Yann Arthus-Bertrand with a beautiful score by Armand Amar and narrated by Glenn Close. It was filmed using helicopter mounted Cineflex hi def cameras designed to nearly eliminate vibration (see a making-of video here). The results are arrestingly breathtaking. Below are some screen shots I snapped at various points throughout the film.

It doesn’t even matter if you disagree with the scientific community on the issue of climate change — but if that’s the case, then turn the audio off, listen to some Jon Hopkins and enjoy the stunning cinematography.

Watch HOME in hi def here. If you find it inspiring, I suggest reading Ishmael by Daniel Quinn.

youtube-home-english-with-subtitles-4-1

youtube-home-english-with-subtitles-5-1

youtube-home-english-with-subtitles-6-1

youtube-home-english-with-subtitles-7-1

youtube-home-english-with-subtitles-8-1

youtube-home-english-with-subtitles-9-1

youtube-home-english-with-subtitles-10

youtube-home-english-with-subtitles-11

youtube-home-english-with-subtitles-12-1

youtube-home-english-with-subtitles-13-1

youtube-home-english-with-subtitles-14-1

youtube-home-english-with-subtitles-15-1

youtube-home-english-with-subtitles-16-1

youtube-home-english-with-subtitles-17-11

youtube-home-english-with-subtitles-18-1

youtube-home-english-with-subtitles-20-1

youtube-home-english-with-subtitles-21-1

youtube-home-english-with-subtitles-22-1

youtube-home-english-with-subtitles-23-1

youtube-home-english-with-subtitles-24-1

youtube-home-english-with-subtitles-25

youtube-home-english-with-subtitles-1-1

youtube-home-english-with-subtitles-3-1

youtube-home-english-with-subtitles-2-1

Awkward ad placement

Jun 1

I was cruising Fox News (don’t ask) and laughed a little bit. Certainly not at the story’s headline, but the ads for Ally Bank – a rebranded effort from GMAC Financial Services.