Animate.hpp

Manage animations on a web site.

To build an animation, you must provide a function to be run repeatedly. When Start() is triggered, the function will be called 60 time per second (or as close as possible), until Stop() is caled.

class Animate
#include <Animate.hpp>

An object that, when active, repeatedly calls a function as fast as possible, to a maximum of 60 frames per second.

Parameters to the animation function can be:

  • double (representing time since the last frame)

  • a const reference to the animation object itself

  • nothing

Control methods: void Start() void Stop() void Step() void ToggleActive()

Access methods: bool GetActive() const bool GetDoStep() const double GetStartTime() const double GetPrevTime() const double GetCurTime() const double GetStepTime() const double GetRunTime() const int GetFrameCount() const

Config methods: void SetCallback(const std::function<void(const Animate &)> & fun) void SetCallback(const std::function<void(double)> & fun) void SetCallback(const std::function<void()> & fun)

Public Functions

inline Animate()

Setup an Animate object to call an anim_fun as fast as possible, at most 60 times a second. Call virtual function DoFrame() if no other functon is provided (which can be overridden if you derive a new class from Animate)

template<typename ...W_TYPES>
inline Animate(const anim_fun_t &fun, W_TYPES&... targets)

Construct an Animate object with the function to run each animation step and zero or more UI elements that should be updated after each frame.

template<typename ...W_TYPES>
inline Animate(const std::function<void(double)> &fun, W_TYPES&... targets)
template<typename ...W_TYPES>
inline Animate(const std::function<void()> &fun, W_TYPES&... targets)
inline virtual ~Animate()
Animate(const Animate&) = delete

Do not copy animations directly.

Animate &operator=(const Animate&) = delete
inline void Start()

Start this animation running.

inline void Stop()

Halt this animation for now.

inline void Step()

Take a single step in this animation.

inline void ToggleActive()

Toggle whether this animation is running or paused.

inline bool GetActive() const

Determine if this animation is currently running.

inline bool GetDoStep() const

Determine if this animation is currently in the process of running a single step.

inline double GetStartTime() const

Return the time point that this animation started MOST RECENTLY.

inline double GetPrevTime() const

Determine the time point when this animation last updated a frame.

inline double GetCurTime() const

Get the current time of the animation.

inline double GetStepTime() const

Determine how long the last step between frames took.

inline double GetRunTime() const

Determine the total amount of time that this animation has run.

inline int GetFrameCount() const

Determine how many total frames have existed thus far in this animation.

inline void SetCallback(const anim_fun_t &fun)

Set a new function for this animation to call when running that takes a const reference to the Animation object as an argument.

inline void SetCallback(const std::function<void(double)> &fun)

Set a new function for this animation to call when running that takes the amount of time since the last frame (a double) as an argument.

inline void SetCallback(const std::function<void()> &fun)

Set a new function for this animation to call when running that takes no arguments.

inline Button GetToggleButton(const std::string &but_name, const std::string &start_label = "Start", const std::string &stop_label = "Stop")

Get a toggle button that will start/stop this animation.

Parameters:
  • but_name – The HTML identifier used for this button.

  • start_label – The name on the button when it will start the animation (default=”Start”)

  • stop_label – The name on the button when it will halt the animation (default=”Stop”)

inline Button GetStepButton(const std::string &but_name, const std::string &label = "Step")

Get a step button that will advance this animation one frame.

Parameters:
  • but_name – The HTML identifier used for this button.

  • label – The name shown on the button.

Protected Types

using anim_fun_t = std::function<void(const Animate&)>

The full version of the animate function takes a const reference to the animate object.

Protected Functions

inline virtual void DoFrame()

DoFrame() is called by default if no animation function is provided. As such, an animation can be built by deriving a class from Animate and overriding this function.

Protected Attributes

anim_fun_t anim_fun

Function to repeatedly run for animation.

vector<Widget> targets

What widgets should be refreshed after each frame?

bool active

Is this animation currently running?

bool do_step

Should this animation take just a single step?

size_t callback_id

Intenral ID for javascript to call back AdvanceFrame()

double start_time

At what time did this animation most recently start?

double prev_time

What was the time point of the previous frame?

double cur_time

What time did the current frame start?

double run_time

How much run time has accumulated?

int frame_count

How many animation frames have gone by?

Button toggle_but

A button to start/stop this animation.

Button step_but

A button to advance this animation one step.