As of October 2014 Adobe released an update to InDesign that gave new meaning to animation for fixed layout EPub ebooks. In addition to adding the ability to use audio, video, and placed HTML, the update includes the ability to create animations for fixed layout EPUB directly in InDesign. These animations previously were only available for projects exported as Interactive PDF.

To create a simple animation, all one needs to do is select the page item and establish its settings in the animation panel (Window > Interactive > Animation).

The Preset option in this panel provides access to 45 (or more) animation presets. When a preset is chosen, a proxy of the preset shows the animation in the window.

After choosing a preset, a few attributes such as duration, times to play, ease, scale, and opacity can be set. The animation can be set to trigger when the page is opened, when the page is clicked, or when the page item itself or a button is clicked.

You can view the changes made in the animation panel by clicking the small button at bottom left.

Automating Animation

With setting up animation being as easy as this, one might question why anyone would consider automating the process. For a simple animation, there most likely is little reason. But when it comes to more complex animations often involving multiple objects, or timing sequences, using a script is the only way to go. Since you already are familiar with writing scripts, you may wish to take advantage of the total control that scripts give to animation. There are some aspects of animating InDesign for fixed layout EPUB you just cannot do manually. The only way to do it is with a script (or muck around with the document’s .indl file).

A Simple Animation

To show how easy–and straightforward–using a script can be, the following demonstrates setting up a simple animation using AppleScript. (The same process can be used with ExtendScript; just change the syntax accordingly.)

To test, create a document for EPUB intent (1024 by 768).

Place a page item (rectangle, ellipse, or polygon) on the page. Give the item a colorful fill and place it about two-thirds toward the right side of the page. With the page item selected, run the following script. It sets the animation properties for the item to fly into the page when the page is opened.

Fly In

   tell application "Adobe InDesign CC 2014"
	set selList to selection
	set objRef to item 1 of selList
	set name of objRef to "Star1"
	--item ref, dTravel list, number times
	set dTravel to {800, 0.0} --relative path for travel
        set dTime to 1 --duration
        set numTimes to 1 --number of times to repeat
	my toCurrentLocation(objRef, dTime, dTravel, numTimes)
   end tell
   (*toCurrentLocation handler*)
   on toCurrentLocation(objRef, dTime, dTravel, numTimes)
      --default List for straight line path
      set mPathPoints to {{{{0.0, 0.0}, {0.0, 0.0}, {0.0, 0.0}}, {{0.0, 0.0}, {0.0, 0.0}, {0, 0.0}}}, true}
      repeat with i from 1 to 3
	 set item i of item 2 of item 1 of mPathPoints to dTravel
      end repeat
      tell application "Adobe InDesign CC 2014"
	 tell animation settings of objRef
	    set design option to to current location
	    set motion path points to mPathPoints
	    set plays to numTimes
	 end tell
      end tell
   end toCurrentLocation

When you run the script a green line is added to the page item to indicate its path of animation.

View the result in InDesign’s EPUB Interactive Preview panel (control + shift + return). Depending on the size of the page item and where it was placed, the script may have been fairly successful.

The top part of the script identifies the object to be animated, and establishes the animation values. The distance for travel list (dTravel) defines the relative distance the object is to travel, with horizontal being the first value, and vertical being the second. The values given to the dTravel list is just an educated guess based on the fact that two-thirds of 1024 (page width) is 682. Allowing 100 for the size of the page item, a relative distance of 800 should place the item off the page at the beginning of the animation. Notice the object is given a name to make it easier to access it, if needed. later in the script.

The toCurrentLocation handler is where the animation is added to the object. The design option is set to to current location. The path for the default travel list (mPathPoints) is modified using the value for dTravel. How this is accomplished and why will be discussed in a later blog when we start working with paths.

At first, this may look like a lot of work just for a simple animation. But as you add more to the script, you will start to see some definite advantages.

More than One Object

If instead of animating one object, assume that the page has three objects that need to be animated. If all of the items will fly in from the same direction, all that the script above requires is a simple repeat loop. Remember that the list that controls the direction of the animation (mPathPoints) uses relative values so all objects can use the same path. The script assumes that the objects will begin approximately 800 points horizontally from their current location. Add two more items to your test page.

Modify the top portion of the script above to read as follows:

   tell application "Adobe InDesign CC 2014"
	set selList to selection
	set dTravel to {800, 0.0}
        set dTime to 1 --duration
        set numTimes to 1 --number of times to repeat
	repeat with i from 1 to length of selList
	    set objRef to item i of selList
            set name of objRef to ("Star" & i)
	    my toCurrentLocation(objRef, dTime, dTravel, numTimes)
	end repeat
   end tell

Select all three of the page items and run the modified script. View the animation in the EPUB Interactivity Preview panel (option + shift + return). Be sure to click the Clear Preview button before clicking the Play button.

More than One Direction

If instead of all objects flying in from one direction, you want one to fly in from left, one from the top, and then one from the right. For this, a list of lists can be used for the value of the dTravelList variable. Each item in the list will identify the relative horizontal and vertical move for a selected page item.

Note: To move an item from right to left or bottom to top, a negative value is used for the appropriate horizontal or vertical value.

Modify the top portion of the script to read as follows:

   tell application "Adobe InDesign CC 2014"
	set selList to selection
        --list identifies relative horizontal and vertical moves for each item
	set dTravelList to {{800, 0.0}, {0.0, 600}, {-700, 0.0}}
        set dTime to 1 --duration
        set numTimes to 1 --number of times to repeat
	repeat with i from 1 to length of selList--repeat for each selected item
	    set objRef to item i of selList
            set name of objRef to ("Star" & i)
	    set dTravel to item i of dTravelList
            my toCurrentLocation(objRef, dTime, dTravel, numTimes)
	end repeat
   end tell
   --the toCurrentLocation handler is the same as above

Select the three items on the page and run the script. View the animation in the EPUB Interactivity Preview panel (option + shift + return).

Calculating Travel Amounts

The values set for the dTravel amounts above were best guesses. To be more precise, a handler can be added to the script to calculate the amount of move required. The calculation will be based on the bounds of the page item and the side of the page the item will move in from. This will be returned from the handler as a list (dTravelList). The result of this calculation is then passed to the toCurrentLocation handler. Modify the top portion of your script and add the calculateRelativeMove handler to read as listed below. Again, to test, select all three page items and run the script. The script should read as follows:

   tell application "Adobe InDesign CC 2014"
	set selList to selection
	set dSideList to {"left", "top", "right"}--side each item will fly in from
        set dTime to 1 --duration
        set numTimes to 1 --number of times to repeat
	repeat with i from 1 to length of selList
	    set objRef to item i of selList
	    set name of objRef to ("Star" & i)
	    set fromSide to item i of dSideList
	    set dTravel to my calculateRelativeMove (objRef, fromSide)
	    my toCurrentLocation(objRef, dTime, dTravel, numTimes)
	end repeat
   end tell
   (*Calculates relative distance for move and returns list*)
   on calculateRelativeMove(objRef, fromSide)
	tell application "Adobe InDesign CC 2014"
	    set pageRef to parent page of objRef
	    set pageBounds to bounds of pageRef
	    set gBounds to geometric bounds of objRef
	    if fromSide is "top" then
		set vMove to round (item 3 of gBounds) 
		return {0.0, vMove}
	    else if fromSide is "left" then
		set hMove to round (item 4 of gBounds)
		return {hMove, 0.0}
	    else if fromSide is "bottom" then
		set vMove to round ((item 1 of gBounds) - (item 3 of pageBounds))
		return {0.0, vMove}
	    else if fromSide is "right" then
		set hMove to round ((item 2 of gBounds) - (item 4 of pageBounds))
		return {hMove, 0.0}
	    end if
	end tell
    end calculateRelativeMove
    --add the toCurrentLocation handler from the script above 

As you can see, with just a few lines of code a script can set the animation settings of multiple page items to fly in from any side of the page. Do some experimenting using different values for the dSideList list (defining “top”, “left”, “bottom”, or “right”). Be sure to save your Fly In script as we will be using it and its handlers in future blogs.

Challenge

Modify your script to change the duration for each page item. (Duration is the amount of time, in seconds, allocated for an animation.) This value is defined in the script using the variable dTime.