r/reddithax • • May 03 '14

Frame by frame animation implementation

Here's a fairly simple way to implement a frame by frame animation. I'm pretty sure this is fairly well known, but I thought to share this for people who don't know about it yet. You can find an example here (hover over the submit feedback button). Edit: The animation isn't up anymore. You can find a similar animation with the Clippy at /r/tumblrinaction.

Although it may seem like using CSS transforms for the page flip is better (and indeed, for this case it probably is), as animations get more complicated it becomes much harder to implement them using pure CSS transformations. I'd much rather create an animation in Flash, and then somehow port it over into the CSS, so I used this method to do it.

It basically consists of a long png file with each frame, similar to a film reel. At that point I just used the CSS animations to change the background-position of the png file every 1/24 of a second to the next frame of the png file.

Here's some sample HTML code showing a basic example:

<html>
    <body>
        <div class = "box"> </div>
    </body>
<html>

And the CSS:

.box {
    background-image: url(path)
    background-position: 0px 0px;
    transition: all 0s;
}

.box:hover {
    background-position: 0px -736px;
    transition: background-position 1s steps(23, end);
}

Where path is the path to a PNG file containing 24 frames that are each 32 pixels in height.

6 Upvotes

3 comments sorted by

1

u/Walter_Bishop_PhD May 04 '14

I implemented a frame animation as the hover animation for the logo in /r/Cosmos!

I tried having a transition for when a user hovers off of the logo, but if you hover in and out of the logo before any of the animations finish, the background-position glitches hard. Is there a way to fix that?

2

u/202halffound May 04 '14

Yeah, that was a problem I ran into also. This is why frame-by-frame is still only really suitable for certain situations. For your case, perhaps you could try using text and animating the letter-spacing attribute?

1

u/Walter_Bishop_PhD May 04 '14

I tried that way prior to this sprite animation way, and a problem I ran in to was not all browsers have the font Arial (android for example) and the alignment of the logo would get seriously messed up on those platforms, and I'm not sure if there's a way to make it use a different letter-spacing and text-indent for different fonts