summaryrefslogtreecommitdiffstats
path: root/doc/src/declarative/animation.qdoc
blob: 53a0c55ce9063ac2bb0eaa46f6fa8ca230a31950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in a
** written agreement between you and Nokia.
**
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of this
** file.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
\page qdeclarativeanimation.html
\title QML Animation


In QML, animations are created by applying animation objects to object property
values to gradually change them over time. Animation objects are created from
the built-in set of animation elements, which can be used to animate various
types of property values. In addition, animation objects can be applied in
different ways depending on the context in which they are required.

To create an animation, use an appropriate animation element for the type of
the property that is to be animated, and apply the animation depending on the
type of behavior that is required. This page describes the \l {Types of
Animations} that can be created and the \l {Animation Elements} that are used
to create these animations.


\section1 Types of Animations

An animation is created in different ways depending on the context in which it
is required. Suppose a \l Rectangle's movement - that is, changes in its \c x
or \c y property values - should be animated. The semantics of the animation
differ depending on whether you want to create:

\list
\o An animation that moves the \l Rectangle as soon as it is created, to a
known position
\o An animation that only triggers when the \l Rectangle is moved by external
sources - for example, when the mouse is clicked, animate the movement to the
mouse position
\o An animation that triggers when a particular signal is received
\o A standalone animation that is not bound to the \l Rectangle's movement, but
instead can be started and stopped from script as required
\o An animation that only triggers during \l{QML States}{state changes}
\endlist

To support these different types of animation methods, QML provides several
methods for defining an animation. These are:

\list
\o Creating an \l{Animations as Property Value Sources}{animation using
property value sources}, to immediately animate a specific property
\o Using \l{Behavioral Animations}{behavioral animations}, which are triggered
when a property changes value
\o \l{Animations in a Signal Handler}{Within a signal handler}, to be triggered
when a signal is received
\o As a \l{Standalone Animation}{standalone animation}, that can be
started/stopped from script and can be rebound to different objects
\o Using \l{Transitions}{transitions}, to provide animations between \l{QML
States}{state changes}
\endlist

These methods are demonstrated below. Notice these examples use
PropertyAnimation, which is one of several QML elements that can be used to
create an animation. See the \l {Animation Elements} section further below for 
details.



\section2 Animations as Property Value Sources

An animation is applied as a \l{QDeclarativePropertyValueSource}{property value
source} using the \e Animation \bold on \e Property syntax. Here is a \l
Rectangle whose movement is animated using this method:

\snippet doc/src/snippets/declarative/animation-propertyvaluesource.qml 0
	
This applies a PropertyAnimation to the \l Rectangle's \c x and \c y properties
to animate from their current values (i.e. zero) to 50, over 1000 milliseconds.
The animation starts as soon as the \l Rectangle is loaded. To animate from
specific values rather than the current \c x and \c y values, set the
PropertyAnimation's \l {PropertyAnimation::}{from} property.

Specifying an animation as a property value source is useful for animating a
property to a particular value as soon as the object is loaded.


\section2 Behavioral Animations

Often an animation should be applied whenever a particular property value
changes. In these cases, a \l Behavior can be used to specify a default
animation for a property change. Here is an example:

\snippet doc/src/snippets/declarative/animation-behavioral.qml 0

This \l Rectangle has \l Behavior objects applied to its \c x and \c y
properties. Whenever these properties change (in this case, when the mouse is
clicked within the parent \l Item), the PropertyAnimation objects defined
within the behaviors will be applied to these properties, thus animating the \l
Rectangle's movement to its new position. Unlike the method of \l {Animations
as Property Value Sources}{defining an animation as a property value source},
which creates a one-time animation that animates a property to a known value, a
behavioral animation is an animation that is triggered \e {in response to} a
value change.

Any changes to these properties will trigger their animations. If \c x or \c y
were bound to other properties, and those properties changed, the animation
would be triggered. The \l{Behavior::}{enabled} property can be used to force a
\l Behavior to only apply under certain circumstances.

Notice that unlike for property value source animations, the
PropertyAnimation's \l {PropertyAnimation::}{from} and \l
{PropertyAnimation::}{to} properties do not need to be defined because these
values are already provided, respectively, by the \l Rectangle's current values
and the new values set in the \c onClicked handler. If these properties were
defined anyway, they would override the default values.

See the \l {declarative/animation/behaviors}{Behaviors example} for a
demonstration of behavioral animations.


\section2 Animations in a Signal Handler

An animation can be created within a signal handler to be triggered when the
signal is received. For example:
    
\snippet doc/src/snippets/declarative/animation-signalhandler.qml 0

The PropertyAnimation is triggered when the MouseArea is clicked, animating the
\c x and \c y properties to a value of 50 over 1000 milliseconds. Since the
animation is not bound to a particular object or property, it must define the
\l {PropertyAnimation::}{target} and \l {PropertyAnimation::}{property} (or \l
{PropertyAnimation::}{targets} and \l{PropertyAnimation::}{properties}) values.
The \l {PropertyAnimation::}{to} property is also required to specify the new
\c x and \c y values.


\section2 Standalone Animations

Animations can also be created as ordinary QML objects that are not bound to
any particular objects and properties. Here is an example, using a 
PropertyAnimation object. The animation is explicitly started when the 
\l Rectangle is clicked:

\snippet doc/src/snippets/declarative/animation-standalone.qml 0

A standalone animation object is not running by default and must be started explicitly
using the \l {Animation::}{running} property or \l {Animation::}{start()} and
\l {Animation::}{stop()} methods. Since the animation is not bound to a
particular object or property, it must define the \l
{PropertyAnimation::}{target} and \l {PropertyAnimation::}{property} (or \l
{PropertyAnimation::}{targets} and \l{PropertyAnimation::}{properties}) values.
The \l {PropertyAnimation::}{to} property is also required to specify the new
\c x and \c y values. (The \l {PropertyAnimation::}{from} value can optionally
be provided.)

Standalone animations are useful when an animation is not targeted towards a
single object property and the animation should be explicitly started and
stopped.


\section2 Transitions

Transitions are used to describe the animations to be applied when a \l {QML
States}{state change} occurs. To create a transition, define a \l Transition
object and add it to an item's \l {Item::}{transitions} property. An example:

\snippet doc/src/snippets/declarative/animation-transitions.qml 0

The PropertyChanges object in the \e moved state defines that when the
\l Rectangle is in this state, its position should be changed
to (50, 50). When the \l Rectangle changes to the \e moved state, the 
\l Transition will be triggered, and the transition's \l PropertyAnimation will
animate the changes in the \c x and \c y properties to their new values.
The animation will not be applied at any time other than during the state 
change.

Notice the example does not set any \l {PropertyAnimation::}{from} and \l
{PropertyAnimation::}{to} values for the PropertyAnimation. As a convenience,
these properties are automatically set to the values of \c x and \c y before
and after the state change, respectively. However, they can be explicitly set
if these values should be overrided.

Also notice the PropertyAnimation does not need to specify a \l
{PropertyAnimation::}{target} object; any \c x or \c y value of any object that
has changed during the state change will be animated. However, the target can
be set if the animation should be restricted to certain objects.

The top-level animations in a \l Transition are run in parallel. To run them
one after the other, use a SequentialAnimation, as shown below in \l {Grouping
Animations}.

See the \l Transition documentation for more information.


\section1 Animation Elements

To create an animation, choose from one of the built-in QML animation elements.
While the above examples are demonstrated using PropertyAnimation, they could
have used other elements depending on the type of the property to be animated
and whether a single or multiple animations are required.

All animation elements inherit from the \l Animation element. It is not
possible to create \l Animation objects; instead, this element provides the
essential properties and methods for animation elements. For example, it allows
animations to be started and stopped through the \l {Animation::}{running}
property and the \l{Animation::}{start()} and \l{Animation::}{stop()} methods.
It can also define the number of \l {Animation::}{loops} for an animation.


\section2 Property Animation Elements

PropertyAnimation is the most basic animation element for animating a property.
It can be used to animate \c real, \c int, \c color, \c rect, \c point, \c size, and
\c vector3d properties. It is inherited by NumberAnimation, ColorAnimation,
RotationAnimation and Vector3dAnimation: NumberAnimation provides a more 
efficient implementation for animating \c real and \c int properties, and 
Vector3dAnimation does the same for \c vector3d properties.  ColorAnimation 
and RotationAnimation provide more specific attributes for animating color 
and rotation changes. 

A ColorAnimation allows color values for the \l {ColorAnimation::}{from} 
and \l {ColorAnimation::}{to} properties. The
following animates the rectangle's \l {Rectangle::}{color} property:

\snippet doc/src/snippets/declarative/animation-elements.qml color

RotationAnimation allows a rotation's direction to be specified. The following
animates the rectangle's \l {Item::rotation} property:

\snippet doc/src/snippets/declarative/animation-elements.qml rotation

In addition, the following specialized animation elements are available:

\list
\o SmoothedAnimation: a specialized NumberAnimation that provides smooth 
changes in animation when the target value changes
\o SpringAnimation: provides a spring-like animation with specialized 
attributes such as \l {SpringAnimation::}{mass}, 
\l{SpringAnimation::}{damping} and \l{SpringAnimation::}{epsilon}
\o ParentAnimation: used for animating a parent change (see ParentChange)
\o AnchorAnimation: used for animating an anchor change (see AnchorChanges)
\endlist

See their respective documentation pages for more details.


\section3 Easing

Any PropertyAnimation-based animations can specify \l
{PropertyAnimation::easing.type}{easing attributes} to control the
easing curve applied when a property value is animated. These control the
effect of the animation on the property value, to provide visual effects like
bounce, acceleration and deceleration.

For example, this modified version of an \l {Animations as Property Value
Sources}{earlier example} uses \c Easing.OutBounce to create a bouncing effect
when the animation reaches its target value:

\snippet doc/src/snippets/declarative/animation-easing.qml 0

The \l{declarative/animation/easing}{easing example} visually demonstrates each
of the different easing types.

\section2 Grouping Animations

Multiple animations can be combined into a single animation using one of the
animation group elements: ParallelAnimation or SequentialAnimation. As their
names suggest, animations in a ParallelAnimation are run at the same time,
while animations in a SequentialAnimation are run one after the other.

To run multiple animations, define the animations within an animation group.
The following example creates a SequentialAnimation that runs three animations
one after the other: a NumberAnimation, a PauseAnimation and another
NumberAnimation. The SequentialAnimation is applied as a \l{Animations as
Property Value Sources}{property value source animation} on the image's \c y
property, so that the animation starts as soon as the image is loaded, moving
the image up and down:

\snippet doc/src/snippets/declarative/animation-groups.qml 0
\image propanim.gif

Since the SequentialAnimation is applied to the \c y property, the individual
animations within the group are automatically applied to the \c y property as
well; it is not required to set their \l{PropertyAnimation::}{properties}
values to a particular property.

Animation groups can be nested. Here is a rather complex animation making use
of both sequential and parallel animations:

\snippet doc/src/snippets/declarative/animation-groups.qml 1

Once individual animations are placed into a SequentialAnimation or
ParallelAnimation, they can no longer be started and stopped independently. The
sequential or parallel animation must be started and stopped as a group.

See the \l {declarative/animation/basics}{Animation basics example} for a
demonstration of creating and combining multiple animations in QML.



\section2 Other Animation Elements

In addition, QML provides several other elements useful for animation:

\list
\o PauseAnimation: enables pauses during animations
\o ScriptAction: allows JavaScript to be executed during an animation, and can
be used together with StateChangeScript to reused existing scripts
\o PropertyAction: changes a property \e immediately during an animation,
without animating the property change
\endlist

See their respective documentation pages for more details.

*/