Class: AnimationWidget

PR. AnimationWidget

Applies an animation to a DOM element.

There is a long list of parameters that can be applied to animations which make them a highly versatile widget capable of a vast range of different animations. The animation parameters change values assigned to the widget via CSS - for example, setting data-end-opacity to 100 and specifying that the widget begins with opacity = 0 in the CSS will create an animation that begins transparent and transitions to fully opaque. See the opacity examples.

The animation parameters can be categorised into 3 separate sections:

Transformation
Rotation, scaling, positioning and opacity.
Transition
Delay, duration, timing function, looping, loop count and hiding before and after.
Interaction
User interaction, on screen.

The transformation parameters are fairly straight forward and affect the movement (and opacity) of the animation. It should be noted that the values for rotation and positioning are absolute to the viewport whereas opacity and scaling are specified as a percentage of the original opacity and size i.e. a final x position of 100px will move the animation to x=100 on the viewport irrespective of where it begins on the page but a final x and y scaling of 50 will halve the size of the animation. Although this may sounds initially complex it is an intuitive way of working with the definition of the animation you are creating. See the movement examples.

The transitional parameters specify how the animation will occur. The delay and duration are fairly straight forward but the 'timing function' parameter is more interesting. The animations use CSS3 transitions under-the-hood so any standard preset CSS3 timing function can be used (such as linear, ease-in or ease-out). Behind the scenes these presets reference a cubic bezier curve but you can specify a custom curve yourself using 4 parameter notation, e.g. cubic-bezier(1.000, -0.530, 0.405, 1.425). A full discussion of bezier curves is beyond the scope of this documentation but more information can be found here. See the timing function examples.

The transitional loop and loop count parameters work together. data-loop is a boolean that works in conjunction with data-count to specify how many times the animation loops. If data-count=0 or data-count is omitted from the parameter list then the animation will loop indefinitely. It is worth noting that specifying data-count and omitting data-loop (or setting it to false) will have no effect on the animation—although you have specified a number of loops the animation will never begin looping so remember to set data-loop to true if you want a looping animation. See the loop examples.

The transitional hiding parameters hide an animation before or after it has fired. An animation that begins hidden will 'pop' into existence (i.e. it will not transition from transparent), similarly, an animation that hides on end will simply disappear. A hidden animation is still actionable, either by user interaction or responding to an event (such as a button tap/click).

The interactive parameters specify whether the animation refers to user interaction (i.e. the animation effectively becomes a button that actions itself when tapped/clicked) or responds to moving on screen. If both of these are set to false (onscreen is false by default) the animation can still respond to an event (such as a button tap/click).

HTML Parameters

The following parameters are required

data-widget="BUTTON" Required
Button identifier.

The following parameters are optional (defaults are listed in braces)

id="{{id}}" Optional
It is usually preferable to include a unique ID but it is not strictly necessary.
data-delay="{{0}}" Optional
(in ms) The delay before the animation begins
data-duration="{{2000}}" Optional
(in ms) The duration of the animation
data-timing-function="{{linear}}" Optional
(String) The timing function refers to the cubic graph used to determine the rate at which the transition is executed ( some example values include linear, ease-in, ease-out, ease-in-out although you could specify the bezier, e.g data-timing-function="cubic-bezier(1.000, -0.530, 0.405, 1.425)" )
data-loop="{{false}}" Optional
(Boolean) specifies whether the animation should loop. Used in conjunction with data-count, if data-count is 0 or is not specified then the animation will loop indefinitely.
data-count="{{0}}" Additionally Required
Specifies the number of times the animation will loop. Requires that data-loop is set to true.
data-hide-before="{{false}}" Optional
Boolean - Specifies whether the animation begins hidden. A hidden animation can be actioned (either by user interaction or via an event) whereby it will become visible and then fire it’s animation.
data-hide-after="{{false}}" Optional
Boolean - Specifies whether an animation ends hidden. If the animation can be actioned more than once then even though it is hidden it will still be actionable.
data-end-rotation="{{0}}" Optional
(in degrees) Specifies the final angle of rotation for the animation
data-end-opacity="{{100}}" Optional
(%) The final opacity of the animation. 100 denotes fully opaque, 0 denotes fully transparent.
data-end-scale-x="{{100}}" Optional
(%) The final scale of the animation for the x axis. Specified as a percentage of the starting scale, so 50 denotes halving the x size, 100 denotes no change and 200 denotes doubling the x size.
data-end-scale-y="{{100}}" Optional
(%) The final scale of the animation for the y axis. Specified as a percentage of the starting scale, so 50 denotes halving the y size, 100 denotes no change and 200 denotes doubling the y size.
data-end-x="0" Optional
(in px) The final x position of the animation. This is an absolute position relative to the viewport i.e. if the animation starts at x = 100px then to move the the animation 50px to the right data-end-x should equal '150px'.
data-end-y="0" Optional
(in px) The final y position of the animation. This is an absolute position relative to the viewport i.e. if the animation starts at y = 100px then to move the the animation 20px upwards data-end-y should equal '80px'.
data-user-interaction="{{true}}" Optional
Boolean - Specifies whether the animation responds to user interaction (i.e. a click or tap).
data-onscreen="{{false}}" Optional
Boolean - Specifes whether an animation fires when brought on screen.

Examples

Movement Examples

Simple Movement Animation

The following example shows a simple movement animation that begins at the top left of the scene and animates into the center when the page becomes on screen. In-line styles are used for clarity, stylesheets are recommended.

id Optional
A unique id for the HTML element.
data-widget="ANIMATION" Required
Animation widget identifier.
data-end-x="312px" Required
Final x position
data-end-y="174px" Required
Final y position
data-onscreen="true" Required
Fires when brought on screen
<!-- Moves to the center of the screen -->
<div id="example_id"
style="left:0px; top:0px; width:400px; height:400px;"
data-widget="ANIMATION"
data-end-x="312px"
data-end-y="174px"
data-onscreen="true" >
<img src="animation_image" />
</div>
Rolling Ball Animation

The following example shows slightly more complex movement, incorporating movement with rotation. Note that this animation just moves sideways across the page so data-end-y is omitted because it is not required. Also of note is that the rotation is set to 720 meaning that this ball animation will rotate the ball twice over the course of it’s movement.

In-line styles are used for clarity, stylesheets are recommended.

data-end-x="600px" Required
Final x position
data-end-rotation="720" Required
Rotates the animation
<!-- Rotation and horizontal movement -->
<div id="example_id" style="left:100px; top:0px; width:400px; height:400px;"
data-widget="ANIMATION" data-end-x="600px" data-end-rotation="720" >
<img src="ball_img" />
</div>

Opacity Examples

Fade In Animation

The following example shows an animation that fades in to 90% opacity (0.9) on response to a button. User-interaction is set to false so that the animatio is only triggered via the button. Note that using the data-hide-before parameter to achieve this will result in different behaviour, likely it will be behaviour you don’t want. See the next example for a hidden widget example.

In-line styles are used for clarity, stylesheets are recommended.

data-end-opacity="100" Required
Final opacity
data-user-interaction="false" Required
Remove user interaction
<!-- Fade in via a button -->
<div id="example_id" style="left:100px; top:0px; width:400px; height:400px; opacity: 0;"
data-widget="ANIMATION" data-end-opacity="90" data-user-interaction="false" >
<img src="animation_img" />
</div>
<div id="button_id" data-widget="BUTTON" data-target="example_id" data-action="PLAY_ANIMATION">
<img src="button_img" /><span>Click Me</span>
</div>
Crouching Tiger Hidden Animation

The following example shows an animation that is hidden before it starts. It also has a delay before it starts. The use-case here is that the background of the page contains an image which then starts to animate after 2 seconds, during those 2 seconds the animation should not be actionable but after the animation has begun a user may interact with it. By hiding the widget before it starts animating the animation is kept hidden until it is ready for action.

In-line styles are used for clarity, stylesheets are recommended.

data-end-scale-x="200" Required
Double horizontal size
data-end-scale-y="200" Required
Double vertical size
data-delay="2000" Required
Delay start by 2 seconds
data-hide-before="true" Required
Hide the animation before it begins
<!-- Hidden Animation Example -->
<div id="example_id" style="left:100px; top:0px; width:400px; height:400px;"
data-widget="ANIMATION"
data-end-scale-x="200"
data-end-scale-y="200"
data-delay="2000"
data-hide-before="true" >
<img src="animation_img" />
</div>

Timing Function Examples

Ease-out Animation

The following example shows an animation that uses the CSS3 preset transition timing function of ease-out. An ease-out animation comes to rest more slowly to it’s final state than the default linear transition. The best way to get a grip on timing functions is to simply try out a few different options and see which fits the effect you’re after.

data-timing-function="ease-out" Required
The timing function of the animation
<!-- Ease-out Example -->
<div id="example_id" style="left:100px; top:0px; width:400px; height:400px;"
data-widget="ANIMATION" data-timing-function="ease-out" data-end-x="600px" data-end-y="400px" >
<img src="animation_img" />
</div>
Cubic-bezier Timing Curve

If you’re not happy with the standard timing curve presets such as linear or ease-in-out then you can specify your own cubic bezier curve to time your animation. The full specification of defining a bezier curve is beyond the scope here but the cubic bezier function takes 4 parameters between 0 and 1 which define a pair of 2D vectors that define curved line between 2 points.

The curve descibed here is similar to a more extreme version of ease-out.

data-timing-function="cubic-bezier(1.000,0.423,0.250,1.000)" Required
The timing function of the animation
<!-- Defined Cubic-bezier Example -->
<div id="example_id" style="left:100px; top:0px; width:400px; height:400px;"
data-widget="ANIMATION" data-timing-function="cubic-bezier(1.000,0.423,0.250,1.000)" data-end-x="600px" data-end-y="400px" >
<img src="animation_img" />
</div>

Loop Examples

Loops Forever

The following example shows an animation that loops. As data-count is not specified it defaults to a value of 0 and is interpreted by making the animation loop indefinitely.

data-loop="true" Required
Start looping
<!-- Ease-out Example -->
<div id="example_id" style="left:100px; top:100px; width:400px; height:400px;"
data-widget="ANIMATION" data-loop="true" data-end-rotation="360" >
<img src="animation_img" />
</div>
Warning - Anti-Widget Ahead!

! This widget will not work as you may expect !

Although the widget below looks like it should loop twice and then stop, the data-loop parameter is not specified and defaults to false meaning that the animation will not start looping in the first place. To fix this add data-loop='true' to the example HTML below.

data-count="2" Required
Does not oop twice (see description)
<!-- Warning - This widget does not work as expected -->
<div id="example_id" style="left:100px; top:0px; width:400px; height:400px; opacity: 0;"
data-widget="ANIMATION" data-count="2" data-end-x="600px" data-end-y="400px" >
<img src="animation_img" />
</div>
Author:
  • Appstudio Widgets
Source:
  • widgets/AnimationWidget.js, line 2

Extends

  • PR.BaseWidget

Members

elHTMLDivElement

The element the widget is attached to

Source:
  • widgets/AnimationWidget.js, line 265

Methods

initialize( element )

Acts like a constructor, this function triggered by default when you create an object.

Parameters:
Name Type Description
element HTMLElement

The HTML DOM Element which references the animation widget

Throws:
Error Error HTML Element not attached.
Source:
  • widgets/AnimationWidget.js, line 300

initLooping_( el_ )

Initialises the loop variables based on HTML markup

Parameters:
Name Type Description
el_ HTMLElement

The HTML Dom Element that 'is' the Animation widget.

Source:
  • widgets/AnimationWidget.js, line 363

initVars_( el_ )

Initialize the variables, fetch the attribute values from the mark up and assign them to instance variables.

Parameters:
Name Type Description
el_ HTMLElement

The HTML Dom Element that 'is' the Animation widget.

Source:
  • widgets/AnimationWidget.js, line 333

onTap_( event )

Event handler when a button is pressed.
Simply prevents default action and passes the event to radio for any subcribers to hear the event.

Parameters:
Name Type Description
event event

The event which fired the handler

Source:
  • widgets/AnimationWidget.js, line 390
Top