As I said in a previous Angular 2 tutorial.
Animations can make or break your UX.
Too much of them and your application looks like a Vegas Casino (although this can be an interesting idea for an Augmented Reality application).
None and your app will be missing the little spark to make it stand out.
In this tutorial we have a look at how to implement them, starting from the previous Ionic AR tutorial.We are going to resize our Drawables when switching between the idle and the selected states.
Let’s start by adding three properties to our Marker Class:
The RESIZE_DURATION represents the duration of the animation.
Animations in Wikitude are very simple.
Their base object is an AnimationGroup. The animations will be stocked into an array-like object then triggered.
Two types of AnimationGroups:
- Parallel: Where every animations are run at the same time
- Sequential: Where the animations are triggered one after the other
In this tutorial we use a Parallel Group because it’s the most suitable for our transition. Each group has it’s own use.
We have two AnimationGroups, one that will be used when the Marker is selected and another one when it’s deselected.
Back to the code now!
Our animations last two seconds so we need to prevent any action on a Marker if an animation is running.
We need the following method to check this condition:
Three cases here:
- When both AnimationGroups are not yet initialized: we are good to go
- When one group is running an animation: we lock any action
- When both groups aren’t running any animation: we are good to go
This method will be used in the getOnClickTrigger method in order to see if we allow the click action or not:
When no animations are running the setSelected and setDeselected methods can be used.
Starting with the setSelected method:
We already had this method (previous tutorial).
The first part is to create a new AnimationGroup and stock it in the animationGroupSelected property if it’s the first time.
We create four PropertyAnimations. A PropertyAnimation will modify a target’s property over time.
The first PropertyAnimation selectedDrawableResizeAnimationX will increase the scale of the marker when it’s selected, making it bigger on the horizontal axe.
It will do it during a two seconds time period using the Ease Out Elastic easing curve.
An Easing Curve represents how the animation is done, for example, it can be linear and constantly move to the finished state or start slowly and finish faster.
More information on this subject in the documentation here.
The second part is about the markerDrawableIdle, these PropertyAnimations just make it smaller.
Those PropertyAnimations are stocked in the animationGroupSelected property.
Finally after our if condition, we start the animations in this group:
The deselection process is the reverse of what we have done before:
Here we increase the markerDrawableIdle‘s size and decrease the markerDrawableSelected‘s size.
We use another group located in the animationGroupIdle property.
And we trigger the animations:
Animations in Wikitude are very easy.
The workflow is very simple to master: creating some AnimationProperties, stocking them in an AnimationGroup that can run them Sequentially or Parallelly and trigger the group when we need the animations to occur.
All of that is encapsulated in the Marker so the World doesn’t care how things are running, once again, this Marker can be replaced by any other Marker.