Using Javascript to Set a Slide Background

Posted by jonathan on February 28, 2006

One of the problems of making a good image display page is the problem that some images come in portrait format, and some come in landscape. There are several ways to set up appropriately formatted thumbnail backgrounds including:

  • pick each thumbnail background manually, or
  • use only one format so a consistent background can be applied, or
  • write some code! (This is what we’ll be doing!)

In this blog entry I’ll use the slide templates from project two into the book ‘More Eric Meyer on CSS’ (by Eric Meyer). You can get the files here. Also, the book is highly recommended.

As you can see, Eric has done a really nice job of creating a great set of formatting that really does look like slides on a lightbox. However, there is a big problem. We need to know beforehand about the shape, portrait or landscape of each slide.

Here’s the (unwrapped) css formatting magic that Eric uses:

body {background: #EED; margin: 1em;}
div#footer {clear: both; padding-top: 3em;
font: 85% Verdana, sans-serif;}
div.pic {float: left; height: 130px; width: 130px;
padding: 15px; margin: 5px 3px;
background: url(frame-ls.gif) center no-repeat;}
div.pt {background-image: url(frame-pt.gif);}
div.pic img {border: 1px solid; border-color: #444 #AAA #AAA #444;}
div.ls img {height: 96px; width: 128px; margin: 16px 0;}
div.pt img {height: 128px; width: 96px; margin: 0 16px;}
div.pic ul {display: none;}

As you can see, we have a div.ls and a div.ps to be used for landscape and portrait images respectively.

This code does great formatting, but has the big problem that you need to set up each thumbnail individually. This is a problem when you are dealing with large numbers of images, or when generating sets of a few random images from a large list of images.

I’m going to take the approach of shimming in some Javascript to looks at the images and pick the appropriate div formatting dynamically as the page loads.

Lets take a look at the required Javascript.

// ::slide_frame()
// Use the correct slide frame for loaded images
// This gets called when the body is loaded.

function slide_frame() {
// Get slide divs in document
slides = document.getElementsByName( “slide” );

// Iterate over all the slides
count = slides.length;
for( i=0 ; i

// Get child img, we need this to handle any whitespace
// in the div.
for( node=0 ; length > node ; ++node ) {
if( slides[i].childNodes[node].className == ‘flh’ ) {
pic = slides[i].childNodes[node];
break;
}
}

// Set offset of image within slide (In this case I hardcoded to the slide size)
w = (160 – pic.width) / 2;
h = (160 – pic.height)/ 2;
pic.style.margin= h + “px ” + w + “px ” + h + “px ” + w + “px “;
pic.style.border=”1px dotted black”;

// Check aspect ratio of thumbnail
if( pic.width > pic.height ) {

// Give it a landscape slide background
slides[i].style.background=”url(‘frame-ls.gif’)”;
}
}
}
The Javascript should be pretty clear as all it does is switch the background property of the slide div.

Now all we need to do is construct some slides to display, and attach the slide_frame() code to the onLoad event of the document body.

Click here for an example of this in action.

Meredith’s Engagement Ring 10

Posted by jonathan on February 25, 2006

I got to play with my camera again this morning to take a picture of my fiance’s new engagement ring.

Javascript-Powered Slide Display

Posted by jonathan on February 23, 2006

Click my Paris Vacation Pictures to see my new javascript-powered slides.

I will blog this as an article pretty soon, but for now, just click the link to enjoy a random set of slides each time you load the page.

Also, the backgrounds used for the slides are selected by the javascript code and poked into the DOM depending on the orientation, portrait or landscape, of the image.

I used quite a few different resources whilst implementing this, but the main inspiration was Eric Meyers Project 2 in the excellent ‘More on CSS’ book.

Last Run of XCode Before Bed

Posted by jonathan on February 22, 2006

Last run of XCode before bed. Managed to work out drawers in Cocoa by just connecting stuff randomly and debugging. Not the fastest way, but it makes you appreciate being able to send messages to nil without crashing and burning.

Welcome to My New Home

Posted by jonathan on February 20, 2006

Welcome to my new home at jonathanwatmough.com. I’ll be keeping my old blog up at blogger for a while, but eventually everything should migrate over here.

The main site will run in WordPress, but old content here will still be available from the links section (to be added).

Washinton Post Botnet Article – Dump Internet Explorer Now!

Posted by jonathan on February 18, 2006

You know when your computer suddenly slows down? You ever wonder what’s happening? You ever have to reinstall windows, or run a spyware remover?

Most people would answer ‘Yes’ to at least one of the above.

This article in the Washington Post tells you what’s happening at the other end. A hacker is being paid $7000 a *month* to install adware/spyware/crapware on *your* machine. These programs install themselves though Internet Explorer (the Internet) when you browse. They can do almost anything on your machine. However, like a parasite, they may not damage your data, though they have the potential to, they can and do spy on your surfing habits and online user accounts.

Chances are if you have strange windows popping up on your machine, it is infected. Your passwords, kids details, names, addresses could all have been sucked back to guys like this.

Dump Internet Explorer, maybe dump Windows entirely, and switch to a computer system or browsing software such as Mozilla Firefox, that makes this harder for the hackers.

3d Real Time FFT Analysis Software

Posted by Jonathan on February 16, 2006


I have a decent pre-Alpha version of my audio analysis software running. It displays a real-time FFT plot from any supported audio input source. Pre-Alpha in this case means non-feature complete, but fairly solid, crash and leak free. The pic shows data being displayed on a labelled plot with the settings window up and displaying the current settings. Through the magic of Cocoa, changes to the sliders adjust the plot in real-time allowing easy setup for different sound sources. I have kinda written this software before, but in assembler on a Commodore Amiga. Luckily the Mac OS comes with a built in vector processing lib and though my G4 is hardly a Cray, it does a pretty good job from Objective-C using threads to bury the sound capture, and regular old events to perform data analysis and screen plotting. Any enquiries about this software can be directed to Highland Innovation Centre Limited in Scotland.

Re-enacting The Dodge “I Smell a Hemi Ad”

Posted by Jonathan on February 16, 2006

Remember the ad … guy goes “I smell a Hemi!” then jumps out of his car to look under the dustsheet covering a dodge on a transporter …

Yeah, you guessed it, I managed to reenact the part of the ad where the dustsheet catches on the windshield of the moving car and the guy can’t see.

Yep, it was a big piece of plastic and I managed to drive right into it at 70 mph on the Houston Beltway driving to work. Sucks. It was really scary because there was a slap of plastic and the entire windshield just went opaque. Slap! Just like that! And it really covered the whole windshield. I swore, panicked for half a second, then wound the window down and drove for a mile with my head out the window. Managed to pull the mess off my windshield at the toll plaza at Westheimer.

Scary!

Paris Vacation Pictures – Reprocessed

Posted by Jonathan on February 12, 2006

I reprocessed my Paris vacation pictures to be a bit brighter. Fixed the ‘Made with a Mac’ logo also.

Click on Paris Vacation Pictures to go directly to the new pictures. You might want to hit ‘refresh’ to clear out your cache if you already looked at the previous upload.

113918017001248283

Posted by Jonathan on February 05, 2006

p a r i s v a c a t i o n 2 0 0 6

Travel day, and First Day in Paris

Notre Dame Cathedral and Eiffel Tower

Cemetery Pere Lachaise and Musee d’Orsay

Invalides and the Louvre

La Madeleine and St Chapel

Sacre Coeur and Montmartre

Catacombs, St Sulpice and St Germain

All these destinations a photographed at my paris vacation pages on my website.