Skip to main content

Animating objects in JavaScript

I needed a way to animate any property of an object in JavaScript (not necessarily even an element on the page). I did not find anything satisfactory (to me) online, so I decided to write my own. This is the result.

It's a simple animation class, loosely modeled on the Android animations API (in fact, the built-in animation interpolators are simple translations from the Android source code). It is very flexible, and supports animating any numeric property of any object. Animations run at 60 Hz to be fluid if UI elements are being animated.

  • Animate any numeric value
  • Start/stop, pause/resume/restart
  • Repeat - for a specified count or infinitely
  • Reverse animation on repeat.
  • Callbacks for start, stop, resume, end, repeat
  • Custom interpolators
  • NO dependencies
Download the code here (includes documentation for all methods).

A demo of the code is available here. It simply animates a box moving to the right, but demonstrates how the class is used. Prototype is used in the demo, but not required for the animation class itself.

This class will even work with discrete properties, as long as the animated property callback is written for that (example below).

Example Usage:

//make the y property of the coords object fall and bounce from 100 to 0 in 5 seconds
var coords = {x: 100, y: 100};
new Animation(coords, function(obj, val) { obj.y = val; }, 100, 0, 5000, Animation.Interpolators.Bounce()).start();

//animated a box moving 500px to the right in 1 second
//when the animation is over, set the animation reference to null
var anim = new Animation($("box"), function(obj, val) { = val + "px"; }, 0, 500, 1000, Animation.Interpolators.Accelerate(), {end: function() { anim = null; }});

//animate a continuously-blinking box
var anim = new Animation($("box"), function(obj, val) { obj.setOpacity(val); }, 0, 1, 500, Animation.Interpolators.AccelerateDecelerate());
//can do anim.stop() and anim.resume() to pause/resume the blinking

//animate through a set of discrete values
var colors = ["red", "yellow", "green", "blue", "black"];
var anim = new Animation($("box"), function(obj, val) { = colors[Math.floor(val) % colors.length];
}, 0, colors.length, 500 * colors.length, Animation.Interpolators.Linear()).setRepeat(-1).start();

I hope that this will become useful to people working with animations in JavaScript. Please leave feedback and suggestions on how to improve this, or any features that you would like to see.


Popular posts from this blog

Linux on XPS 15 9550/9560 with TB16 Dock [Update:3/29]

Finally got a laptop to replace my fat tower at work - Dell XPS 15 9560. I was allowed to choose which one I wanted and chose the XPS for its Linux support since Dell ships developer edition XPS's running Ubuntu so I figured Linux support would be better than other manufacturers. At first they got me the model with the 4K screen but my monitors are 2K and multi-dpi support in Linux is virtually non-existent and even hi-dpi support on its own is pretty terrible. So I got it exchanged for the model with the regular 1080p screen (which happened to also be the updated 9560 model), which works much better. I'm very glad to report that pretty much everything works, including the TB16 desktop dock, with just a bit of settings tweaking. This post is to help anybody considering getting this setup or looking for help getting things working. For now, I am running Kubuntu 16.04 with KDE Neon installed. List of things I explicitly tested and work: WiFi, Bluetooth Thunderbolt charging

My Views on Code Indentation

I have read many, many articles about the whole tab vs. space indentation thing. Personally, I don't necessarily agree with most of them. They will require the coder to use a specific indentation size and stick with it, even forcing that on other coders. First off, let me outline my method for indenting code. Then I will explain the reasons and advantages/disadvantages. When I indent code, I will use tabs, but only at the beginning of a line. To align something in the rest of the line, I will use spaces. If a line spills to the next line(s), I will indent that line two tabs further. Rationale: Tabs Why tabs? First off, they're compact in the file (1 byte each). This is really insignificant with current disk sizes, but still. If you indent in spaces, then your file will be larger (unless you indent with one space). Another advantage of tabs is that a tab is a tab. It doesn't specify by how many spaces the code is indented, but rather by how many tabs it is indente

Generating PDFs in Rails with Prawn and Prawnto

There are several different PDF generation libraries available for Rails apps. Some of these are PDF::Writer, Prawn, Ruport, RGhost, and FlyingSaucer (for JRuby). First off, Ruport is basically a higher-level API for PDF::Writer. FlyingSaucer is only for JRuby (because JRuby allows for loading of Java classes). Not having any experience with RGhost, I can't exactly write a guide for using it. So now it's basically between Prawn and PDF::Writer, unless there's another library I haven't hear of. However, to my dismay, there is not UTF-8 support in PDF::Writer. So Prawn ends up being the PDF generation library of choice. One thing to keep in mind is that Prawn and Prawnto are beta-stage (sometimes even alpha) libraries, so some code hacking is going to be necessary. Also note the Prawnto is not maintained or tested by the Prawn developers, so there's no guarantee that it'll work with future versions (especially since Prawnto's developer has been "below t