Introduction To Javascript Animations

Plasma builds on top of the QtKinetic animation framework to provide a set of ready-made animations. Starting with KDE Development Platform v4.5, it is also possible to write new animations or mash up existing ones using Javascript. This tutorial describes how Javascript animations are defined, where they are stored and how you can access them from your code.

Writing An Animation

The key components of a Javascript animation are the constructor function, the updateCurrentTime function and the registerAnimation call.

The constructor function is called whenever a new instance of the animation is requested. The name of the function is not critical.

The updateCurrentTime function drives the actual animation: it is called whenever an animation frame is to be processed. The current time of the animation is passed in to it as the first parameter.

To let Plasma know that the animation exists, the registerAnimation function must be called from the script's global scope. registerAnimation takes two parameters: the name by which the animation will be identified and the constructor function.

API

Accessing Other Animations

An Animation object can be created by calling the Animationanimation(string type) function. The string corresponds to the type of animation, which are listed below.

By setting the widgetToAnimate property it can be assigned to animate any QGraphicsWidget desired.

Enumerations

All Animation objects have the following enumerations:

MovementDirection

MoveUp

MoveUpRight

MoveRight

MoveDownRight

MoveDown

MoveDownLeft

MoveLeft

MoveUpLeft

Reference

The reference point, relative to the target widget, for the animation.

Center

Up

Down

Left

Right

State

Paused

Running

Stopped

Common API

With the exception of Pause and Property animations, all animations support the following read/write properties:

AnimationDirectiondirection: the direction the animation should play: AnimationForward or AnimationBackward

intduration: length of the animation in ms.

EasingCurveTypeeasingCurveType: The easing curve to use in the animation

QGraphicsWidgettargetWidget: the QGraphicsWidget (e.g. a Plasma::Widget) that this animation should operate on

Animation Groups

Animations may be put into groups for convenient sequential or parallel running. Sequential groups, where the animations run one after the other, are handled by the AnimationGroup class. Parallel aniations, where the animations run simultaneously, are handled the ParallelAnimationGroup class.

Animations are added to a group by calling add(Animation) on the group object. Groups may also be added to other groups.

Animation Types

Below is a list of all current animation types and their particular read/write properties:

Fade

numberstartOpacity: the opacity, between 0 and 1, that the target widget starts at when the animation begins

numbertargetOpacity: the opacity, between 0 and 1, that the target widget will be at the end of the animation

Geometry

QRectFstartGeometry: the geometry that the target widget should start with

QRectFtargetGeometry: the geometry the target widget will have at the end of the animation

Grow

numberfactor: the factor by which the target widget will grow to by the end of the animation

Pause

numberduration: the number of milliseconds to pause for

Property

Animates an object (must be a QObject internally, which includes all Plasma Widgets) by manipulating one of its properties. Property animations have the following read/write properties:

anystartValue

anyendValue

ByteArraypropertyName

QObjecttargetObject

numberduration

EasingCurveeasingCurve

Pulser

numbertargetScale: the maximum scale of the pulse-shadow item, relative to the target widget

Rotate

QtAxisaxis: the axis along which to rotate the item

Referencereference: the reference point around which to rotate the target widget

numberangle: the number of degrees to rotate the item

RotateStacked

MovementDirectionmovementDirection: the direction to rotate the widgets in the stack around

QGraphicsLayoutItemlayout

Referencereference: the reference point around which to rotate the target widget

QGraphicsWidgetbackingWidget: the widget in the "back" to rotate to the front of the target widget

Slide

MovementDirectionmovementDirection: the direction to slide the widget

numberdistance: the distance to slide the widget

Zoom

numberzoom: the factor by which to zoom the target widget

QEasingCurve

Used to set the progress shape of Animation objects, this class has the following read-only properties:

stringtoString

numbervalueForProgress(number progress): returns effective progress for the easing curve at progress. While progress must be between 0 and 1, the returned effective progress can be outside those bounds.

as well as the following read/write property:

EasingCurveTypetype: the shape of this easing curve

and the following enumeration:

EasingCurveType

Linear

InQuad

OutQuad

InOutQuad

OutInQuad

InCubic

OutCubic

InOutCubic

OutInCubic

InQuart

OutQuart

InOutQuart

OutInQuart

InQuint

OutQuint

InOutQuint

OutInQuint

InSize

OutSine

InOutSine

OutInSine

InExpo

OutExpo

InOutExpo

OutInExpo

InCirc

OutCirc

InOutCirc

InOutCirc

OutInCirc

InElastic

OutElastic

InOutElastic

OutInElastic

InBack

OutBack

InOutBack

OutInBack

InBounc

OutBounce

InOutBounce

OutInBounce

InCurve

OutCurve

SineCurve

CosineCurve

Script Files

In Desktop Themes

A Plasma desktop theme can include one or more JS scripts in the animations/ subdirectory, each of which can contain one or more animations.

To make Plasma aware of the animations, create an [Animations] section in the theme's metadata.desktop file. This group must contain mappings between the script name and the animations the script contains. For example:

Theme inheritance is respected and supported as well, so animations defined in inherited themes as well as the default fallback theme will be available if they are not overridden by an animation of the same name in the theme.

Animations with the same name as a value in the Plasma::Animator::Animation enumeration will be automatically mapped as a replacement for the native C++ animation that is usually returned by Plasma::Animator::create().

In Plasmoids

In Plasma Applications

Accessing A Javascript Animation

C++

In the Plasma::Animator class there is a create(const QString &animationName, QObject *parent = 0) method which returns a QAbstractAnimation that can be used like any other. The animationName parameter is the name of the Javascript animation as defined at animation registration. If no such animation is available, a null pointer will be returned.

Javascript

Javascript animations can be retrieved like any other animation: by name using the animation(String) method.