Path Animation [The Path Object]

Path Animation Concepts:

From a programming point of view, doing animation using a path or timeline is sort of like cheating. All you do is define a path for the layer to follow, and it just loops through each point by just moving it there rather than calculating where to move it. This has some big advantages because it takes little CPU time (so they go really fast) and you can move the layer in any way you can think of. The disadvantage though, is that you cannot easily change the path like you can a calculative animation. If you need to change it, you have to go back and change all the points in your path. However, with that said, using a path is still a decent way of making animation.

The basic technique for path animation is easy to understand. Once you have all the co-ordinates of where to move the layer, you just need a function to loop through them and move the layer to those points. For this purpose I've written a Path Object which will take care of this for you. You just have to add the object to your DynLayers and use the appropriate methods.

The only problem then is to get your coordinates for your path. There are several commercial products that will do this for you such as Macromedia Dreamweaver or mBed Interactor. These pieces of software will auto-generate all code for you and require their own gigantic library files. And if you want to edit the JavaScript code by yourself you'll have to invest quite a bit of time to fiqure out exactly what it going on. If all you want to do is make a simple path animation in a small demo, then paying a couple of hundred dollars for it seems a little extreme. This is why I've designed my own little tool, called DuoPath, which a) doesn't cost anything, b) makes it easy to get all the co-ordinates, and c) lets you do all the coding so you understand what's going on and can easily change it to your needs. The next lesson will show how to use DuoPath, so I'll just continue on assuming that we've already created our path and have all the co-ordinates ready to go.

Here's all the coordinates for a path that I made with DuoPath:

x-coords: 101,105,113,122,131,140,149,156,157,156,155,157,163,172,181,188,196,203,208,215,221,227,234,243,252,

y-coords: 285,271,255,242,230,225,225,233,242,253,265,275,281,284,280,269,256,239,224,208,194,181,168,154,143,

As you see there's about a million of them. But it doesn't really matter how many points there are because remember path animation takes no CPU time, so making a lot of points is no problem. Each set of co-ordinates represent one point in the path. The first x value and the first y value make up the first point and so on. By assinging these numbers to an array we will be able to access any single set by using their indexes:

path1x = new Array(101,105,113,122,131,140,149,156,157,156,155,157,163,172,181,188,196,203,208,215,221,227,234,243,

path1y = new Array(285,271,255,242,230,225,225,233,242,253,265,275,281,284,280,269,256,239,224,208,194,181,168,154,

Now if we wanted to know the 10th x value and the 10th y value, you check value of path1x[9] and path1y[9]. The index number is always 1 minus the point we check because arrays start at zero. So path1x[9]=156 and path1y[9]=253.

With this knowledge in mind, you can apply these coordinates to the Path Object which will allow you to turn your coordinates into a path animation.

The Path Object


The Path Object is an addon object to the DynLayer. It works similarly to the way my Geometric Objects work. It's probably best to include the code for each of the DynLayer and the Path Object as js files:

<SCRIPT LANGUAGE="JavaScript" SRC="dynlayer.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="path.js"></SCRIPT>

Once you've created a DynLayer, you create the Path Object on top of it. You have to pass the Path Object the name of the DynLayer, and the name of the Path Object along with the path information. This is necessary so that the Path Object can manipulate the DynLayer.

objectName.pathName = new Path(dynlayer,name,arrayX,arrayY)


mylayer = new DynLayer("mylayerDiv")
mylayer.path1 = new Path(mylayer,'path1',
new Array(0,10,20,30),
new Array(0,10,20,30))

Optionally you could predifine the arrays and just send their names for the arrayX and arrayY values. Once the Path Object is initialized, the parameters become properties of the DynLayer/Path Object and can be changed at any time if need be (ie. mylayer.path1.arrayX = new Array(5,10,15,20)).

The play() Method:

Begins the animation.,speed,fn)

All the parameters of the play() method are optional, if you do not specify them it will default to play(false,30,null)


The pause() and stop() Methods:



Source Code

Download: path.js
View Source: path.js

View path1.html for a path animation example.

Home Next Lesson: Using DuoPath
copyright 1998 Dan Steinman

Casa de Bender