Animation Implementations

Introduction

Purpose

The previous article in this series discussed iteration techniques and animation algorithms. This article fully implements those functions.

These implementations use the closure-based iteration technique. In the previous article I pointed out how the data is actually specific to each call of the function - not just specific to the element object. For example, this means that the tmr variable is not as accessible - to be able to stop the animation before it completes. To get around this I passed an object as an argument to each animation method. The object had the tmr variable. This eventually lead to the object holding all the context of an animation.

This article has more code than text. After you have read over this page and tried the demos I encourage you to view the source of this page and browse through the code. The source for xAnimation is now included in the X Library. There are also more demos available.

Last update: 27Apr07.

Stand-Alone Implementations

The stand-alone implementations that follow have also been included in the X Library. They are alternatives to the existing X animation functions (xEllipse, xParaEq, xSequence, xSlideCornerTo, xSlideTo and xWinScrollTo). X will retain the existing animation functions for backwards compatibility.

xAniXY('div0', x, y, t)

A minimal, time-based implementation. This only supports constant velocity and does not support an onEnd handler. View source & docs.

x

y

t total time in milliseconds.

 

div0

xAniLine('div1', x, y, t, a, oe)

A time-based linear position animator. Supports constant velocity or two types of acceleration, and supports an onEnd handler. View source & docs.

x

y

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine.

oe onEnd can be a function or string.

 

div1

xAniOpacity('div2', o, t, a, oe)

A time-based opacity animator. Supports constant velocity or two types of acceleration, and supports an onEnd handler. View source & docs.

o target opacity

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine.

oe onEnd can be a function or string.

 

div2

xAniRgb('div3', p, c, t, a, oe)

A time-based color animator. Supports constant velocity or two types of acceleration, and supports an onEnd handler. View source & docs.

p CSS property name.

c target color

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine.

oe onEnd can be a function or string.

 

div3

Abstracting The Engine, Part 2

At the end of the previous article I hinted at the possibilities in abstracting the animation engine out of the functions that had been discussed. xAnimation is an application of that idea.

xAnimation provides an animation engine which supports up to three axes of animation, multiple acceleration types and an onEnd handler. The onEnd handler can return true to cause the animation to repeat. It supports a bounce-back feature. The acceleration types are implemented as an array of velocity functions, so it is very easy to add a custom acceleration type.

The following demonstrate the animation methods I have implemented so far. For more info see xAnimation.

xa.line('div4', x, y, t, a, b, oe)

Animate an element's position over a line segment. View source & docs.

x

y

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

   

div4

xa.arc('div7', xr, yr, a1, a2, t, a, b, oe)

Animate an element's position over an elliptical arc. View source & docs.

xr x radius

yr y radius

a1 start angle

a2 stop angle

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

   

div7

xa2.para('div11', xe, ye, inc, t, oe)

Animate an element's position over parametric equations. This is still experimental. View source & docs.

xe x expression.

ye y expression.

inc parameter increment.

t total time in milliseconds. 0=no time limit.

oe onEnd can be a function or string.

   

xa.size('div10', w, h, t, a, b, oe)

Animate an element's size. View source & docs.

w target width.

h target height.

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

   

div10

xa.corner('div12', c, x, y, t, a, b, oe)

Animate an element's corner over a line segment. View source & docs.

c corner string: 'ne', 'se', 'sw' or 'nw'

x

y

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

   

div12

xa.rgb('div6', p, c, t, a, b, oe)

Animate an element's CSS property that accepts color values. For example, color, background-color and the border colors. View source & docs.

p CSS property name.

c target color

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

   

div6

xa.css('div13', p, v, t, a, b, oe)

Animate an element's CSS property that accepts integer pixel values. For example, the size, position, margin, padding and border properties, as well as font-size, line-height, letter-spacing, word-spacing, etc. View source & docs.

p CSS property name.

v target value

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

  

div13
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.

xa.opacity('div5', o, t, a, b, oe)

Animate an element's opacity. View source & docs.

o target opacity

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

   

div5

xa.scroll(window, x, y, t, a, b, oe)

Animate the scroll position of a window. View source & docs.

x

y

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

xa.scroll('div9', x, y, t, a, b, oe)

Animate the scroll position of an element.

x

y

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

  

div9

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?

Specific Animation Needs

The above animation methods should cover most needs, but sometimes you have a situation where you need a specific type of animation. The following is an example of a custom xAnimation method.

Changing a style property of a container element will also change the same property on any descendants that have that property set to 'inherit'. However, IE has problems with inherit. With the animation engine now abstracted it is easy to write animation methods for custom needs. For example the following animates a CSS property (integer pixel values only) of some container element, the same as css does, but this method also animates all the container's descendant elements.

xa.cssAll('div14', p, v, t, a, b, oe)

p CSS property name.

v target value

t total time in milliseconds.

a acceleration type: 0=none, 1=sine, 2=cosine, 3=combined.

b number of bounces.

oe onEnd can be a function or string.

  

div14
sitvoluptatemaccusantium
doloremquelaudantiumtotam

Sed ut perspiciatis unde omnis iste natus error

Graph of Velocity Functions

Here is a graph of the current velocity functions.

Related

The first article in this series.

The X Animation Index page.

Latest Demos

Pick A Card, Demo 2 - an improved version of this demo.

xFenster rev 21 - added support for a "control menu".

xCalendar - a new X UI object.

Tech Support

Forum support is available at the X Library Support Forums.

License

By your use of X and/or CBE and/or any Javascript from this site you consent to the GNU LGPL - please read it. If you have any questions about the license, read the FAQ and/or come to the forums.

div11