Text with Moving Backgrounds with jQuery

Personally, I am a huge fan of negative space in design. This got me thinking while I was trying to accomplish something different. Normally a textual caption would be positioned above a background, but I wanted to do it the other way around: place the background in the letters.
I also wanted to add some nice dynamic effects to enhance the effect. This was amazingly easy to accomplish in jQuery with the use of a little creativity.

This article will explain what you need to do create an effect like this yourself.

Text with Moving Backgrounds with jQuery

Example How does this look?

What are we going to do?

We are going to create a container which has a moving background, but only a set of letters will be visible of the background. It will be as if there are holes in your container.

To do this we need just a few things:

  • A nice background pattern or image
  • Letters punched out of an image
  • Just a little jQuery code

Normally you would create a PNG file containing anti-aliased letters and place it inside some container on top of a background. What we are going to do is place a full image over a background, covering parts that shouldn’t be seen. Just like a mask!

Then we’re going to make the background of the container move around to create a nice looking effect.

Step 1: Creating the “mask”

To create the overlaying mask I am going to use Photoshop. You can do this with any other image manipulation application, but I am going to explain what I did using screenshots of Photoshop.

  1. First create a new image and fill the background with the foreground of the mask. This is the part that will be visible for the users. I used a black solid fill for this, so it stays clean.
  2. Create a new text layer on the fill you just made.
    Mask in Photoshop
    This gives us the right impression, and we just have to imagine that the white letters will be punched out later.
  3. Hold CMD / CTRL and click on the text layer icon
    Select the text
    Doing this the text will get a selection around it which should look like this:
    Selected text in Photoshop
  4. Click: Select > Inverse
    Inverse selection
    Now the area that will be visible on the mask image is selected. It will look like this:
    Inversed text selection in Photoshop
  5. Select the fill layer and click on “Add mask”
    Add layer mask
    This will create a mask for the fill and the letters have been punched out.
  6. Hide the text layer
    Hide text layer
    You have now punched out the letters of the fill!
  7. Save as .png!
    PNG handles transparency very well and looks right in almost all current browsers (nope, not IE6). It also support opacity per pixel, so it’s not like the the GIF transparency.

Step 2: A little documenting and styling

Now that you’ve got your own mask image, it’s time to create the HTML page for it to end up in.

Just for basics, use something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>Some title</title>
    <link rel="stylesheet" type="text/css" href="style.css">

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>

<body>
<div class='scrollBg'>
    <img src='overlay.png' alt='' />
</div>
</body>
</html>

We created a simple HTML page containing just two elements; the container "scrollBg" and the image that serves as the mask.

Make sure to fix the src of the image and the href to your stylesheet.

In the stylesheet we just need a little bit of styling:

body {
    background-color: #000000;
}

.scrollBg {
    background-image: url(background.jpg);
    background-color: #000000;
    width: 487px;
    height: 83px;
}

.scrollBg img {
    display: block;
}

You'll have to fix the width and height of the scrollBg container. Adjust it to the width and height of the mask you created. This will prevent the background image from showing outside of the mask.

Also change the URL of the background image.

Step 3: Go jQuery crazy!

In the Javascript part of this tutorial we are going to make the background image shift in position at random.

To make jQuery able to move the background image, we need a plugin, since it's not in the default behavior.

Go download this plugin: http://plugins.jquery.com/project/backgroundPosition-Effect

Include this script in on your page using:

<script type="text/javascript" src="url_to_moving_background.js"></script>

Now we can use backgroundPosition as a parameter in the jQuery animate effect! Pretty neat!

The following jQuery code will move the background around at random (put it in the <head> part of the HTML page):

<script type="text/javascript">
$(document).ready(function() {

    moveBgAround();

});

function moveBgAround() {
    //give a random value from 0 to 400 for the X axis and the Y axis
    var x = Math.floor(Math.random()*401);
    var y = Math.floor(Math.random()*401);

    //random generated time it takes to move
    var time = Math.floor(Math.random()*1001) + 2000;

    //make the background image move!
    $('.scrollBg').animate({
        backgroundPosition: '(' + (x * -1) + 'px ' + (y * -1) + 'px)'
    }, time, "swing", function() {
        moveBgAround();
    });
}
</script>

It's that simple! You are now ready to experiment with the code I just created.

The X and Y values are the amount of pixels the background image will shift. He will not move that amount in pixel, but he will move to the generated coordinates.

If you would like to make it move more you can increase the number stated here:

var x = Math.floor(Math.random()*401);

If you want to create a background image that is not a pattern but rather just a large texture, you have to an image with the following width and height: Take the number that you entered in the script (amount after random()) and add the width of the mask to it: that is your width. Same goes with the height.
This will allow the background to move around without being repeated.

Done!

If you have any questions or was inspirited to create different crazy effects: let me know in the comments below.

Happy developing!

Liked this article? Sharing is caring!

89 Comments on this subject

  1. Marco said:

    Pretty cool! Now to make a version where you can select the text etc., and you would be all ready to implement it :) . But I’m not really sure how userfriendly it would be :P .

    Anyway, nice POC and keep up the great work!

  2. Gaya said:

    @Marco: Thanks buddy! It’s more an effect for headers and small animated elements, but selecting the text would be cool!

    @Devolved: Exactly ;) I should try that out haha.

    @Andy: I don’t know if rotating would be posible, could be with CSS3, but that’s not a standard yet :(
    Cool example buddy! I really like it.

    @Moabi, JayQuery and Chuck: My pleasure :)

  3. Mia said:

    You are def a JQuery-Zen-Master, all of your scripts are awesome ! I so enjoyed this one though- I couldnt stop staring at it :D this is great for a hypnotic effect ! :D This site will def be my new playground :)

  4. Kai said:

    Hi, nice tutorial, I will definitaley use this, but would it be possible, that the background only appears if you mouseover the object? Would be amazing for navigations…

  5. nic said:

    hi mr kessler
    my name is nicolas koller (switzerland) and i’m a typographic artist. i like the style of your website especially your arts programming. I’m just wondering what kind of CMS do you use?

  6. Ben said:

    Sorry, I didn’t like this. I came here super-impressed to hear of “text” with a moving background. I was expecting some CSS3 stuff I’d never heard of… instead I saw an image with a moving background, not text.

    It’s a cool effect, but nothing mind blowing.

  7. Jack Rugile said:

    Mmm, this is pretty tasty!

    I think a cool effect could be achieved with a texture that is continuously rushing by either completely horizontal or vertical. Like if there was a floor mask that had square windows cut out of the bottom and water was constantly rushing by.

    I definitely love your application of it though. It definitely opened my mind a little bit as to what jQuery can do. Thanks :)

  8. Pepe said:

    Hi Gaya,

    I have read your last comment on setting a different BG, i ve been trying to add a masked image of 543 width and 400 height but it only moves once and then it stops.

    Could you tell me the measures the BG needs to have ?

    Thx

  9. Michael said:

    I really liked your feature of jquery, well done. I am a beginner with wordpress, how can you insert the code in the wordpress; i.e in header.php and style.css. I am trying to do a moving background but is not working. Guess i am stuck in what to put in header and style. Feedback would be much appreciated

  10. Song said:

    I love to try your awesome work( moving backgrounds), which was posted by posted on: January 8, 2010.
    However, I couldn’t figure it out how I could plug in the number of the width and height.
    I have a background image as a texture (width 384px and height 272px).

    Could you help me please!

Leave your reply

Your email address will not be published. Avatars through Gravatar.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>