Adding easing to animations

  • 26 June 2023
  • 0 replies
  • 123 views

Userlevel 7
Badge +2

Hello everyone,

 

I just wanted to share some work I’ve been doing today with adding easing to animations for OpticStudio.

In March 2021, Dan Hill published his fine article: How do I create presentation quality graphics and animations? At the end of the article, Dan demonstrates how to produce a 360 degree animated GIF using ZPL.

However, one may want to rotate an object over a smaller angular range, and go back and forth from the original position. If you were to do so naively using Dan’s approach, every frame would be at a fixed angular position from the previous one, and you would obtain a “linear” GIF. However, you can obtain a slightly different result if you apply an easing function. There’s an outstanding website, called https://easings.net/, that demonstrates how to make use of such easing functions and the website also includes the formulas necessary to calculate these easing functions.

Let me demonstrate the result with an example first, and then show you the workflow I used.

I wanted to illustrate astigmatism (although we can debate whether it is a good way to do so) in a presentation, and wanted to rotate my design over 90 degrees. The “linear” and “quad easing” results are shown below.

Animated GIF without easing (linear, every has the same angular step in it).
Animated GIF with Quad easing

Whether your prefer, with or without easing is a matter of personal preference. But if you want to achieve that effect, follow along!

I’ve modified Dan’s original code to look like so:

This is a screenshot from OpticStudio, use the code below to copy/paste.
# Number of frames in animation
frame_num = 160

# Create Quad easing function (formula from https://easings.net/#easeInOutQuad)
#
# I'm doing it in a separate loop so that one can easily switch for another
# easing function
#

# Array containing the Quad easing function (between 0.0 and 1.0)
DECLARE quad_ease, DOUBLE, 1, frame_num

# Loop over the frames and apply the Quad easing function formula
FOR ii, 1, frame_num, 1
abscissa = (ii-1) / (frame_num-1)
IF ( abscissa < 0.5 )
quad_ease(ii) = 2 * abscissa * abscissa
ELSE
quad_ease(ii) = 1 - POWR( -2 * abscissa + 2, 2) / 2
ENDIF
NEXT

# Apply the Quad easing function and save the frames
# Here, I'm rotating a sub-system about Z from 0.0 to 90.0 degree
# in non-sequential mode
#

# Rotation amplitutde [degree]
rotation_amp = 90

# Layout window number
layout = 2

# Loop over the frames
FOR ii, 1, frame_num, 1
# Set the Tilt About Z of my object as the product of the rotation
# amplitude with the Quad easing function (it works well because we
# rotate from 0 to rotation_amp, if we wanted to rotated from a
# negative Tilt About Z to a positive Tilt About Z, such as -30.0
# to +30.0 deg, we would have to change the Quad function such that
# it is in the interval -1.0 to 1.0)
SETNSCPOSITION 1, 1, 6, quad_ease(ii) * rotation_amp

# This is the same as Dan Hill's macro
UPDATE ALL
filename$ = "E:\Group Meeting Presentation\June2023\astigmatism\anim_" + $STR(ii)

# I prefer to use EXPORTBMP instead of EXPORTJPG, because EXPORTJPG tend to produce
# color glitches in my animations
EXPORTBMP 2, filename$

# Added a pause to make sure the frames where saved appropriately (as documented in
# the Help File for EXPORTBMP
PAUSE TIME, 1000
NEXT

This will generate BMP images for half of your GIF. I then used Fiji to:

  • File..Import..Image Sequence.. (select folder and use default settings)
  • Image..Stacks..Images to Stack
  • Optional: Image..Adjust..Size..
  • Image..Duplicate (tick Duplicate stack)
  • Image..Stacks..Tools..Reverse
  • Image..Stacks..Tools..Concatenate (the two stacks should be there by default as Image1 and Image2)
  • Optional: Image..Stacks..Animation..Animation Options.. (change the frame rate of the GIF)
  • Image..Type..8-bit Color
  • File..Save As..GIF..

Finally, I used https://compressor.io/ to compress the GIF. I’m uploading my files for you to test as well.

Take care,

 

David


0 replies

Be the first to reply!

Reply