4
$\begingroup$

the title is pretty straightforward. In my application I have one-time-run animation that I'd like without the gray frame around the image that comes with the Animate[] primitive. Is there an elegant way to do it, or is it possible at all?

I found out I can use an animator with AppearanceElements->None to manipulate a Dynamic variable to move the animation, but:

  1. The "hidden" Animator leaves a blank output line
  2. I need to show different animations depending on user input (buttons), and I'm not able to restart the Animator when a different animation is selected

Currently this is the best result I have achieved (simplistic example):

step = 0;
cAnim[x_] := Graphics3D[Rotate[Cuboid[], x Degree, {0, 0, 1}]]
sqAnim[x_] := Graphics[Rotate[Rectangle[], x Degree]];
anim = Dynamic[cAnim[step]];
Animator[Dynamic[step], {0, 90}, AppearanceElements -> None, 
    AnimationRepetitions -> 1]
{ Button["CUBE", anim = Dynamic[cAnim[step]]], 
    Button["SQUARE", anim = Dynamic[sqAnim[step]]]}
Dynamic[anim]

This way I can change between the two animations, but the Animator just keeps running and doesn't restart on change (and of course I have no idea how to do it).

It's highly probable I'm just overlooking a simple solution (maybe not even involving Animate or Animator), but to the best of my efforts I couldn't find it.

Thanks in advance for any help.

$\endgroup$
  • $\begingroup$ Animator has this advantage that it can run completely FronEnd side (when used with DynamicModule variables), the problem is that there is no api for it except of built-in animator buttons. Less efficient but a more flexible way would be to use ScheduledTasks. $\endgroup$ – Kuba May 8 '17 at 20:48
2
$\begingroup$

Maybe this will get you started. The idea is borrowed from my answer to How to add the pause button to that dynamic stopwatch? It uses Clock instead of Animator.

cAnim[x_] := Graphics3D[Rotate[Cuboid[], x Degree, {0, 0, 1}]]
sqAnim[x_] := Graphics[Rotate[Rectangle[], x Degree]];
anim = cAnim;
offset = Clock[{0, Infinity}];
{Button["CUBE", anim = cAnim; offset = Clock[{0, Infinity}]],
 Button["SQUARE", anim = sqAnim; offset = Clock[{0, Infinity}]]}
DynamicModule[{time = Clock[{0, Infinity}], stop, dt = 3},
 Dynamic[
  If[(time - offset) 90/dt < 90,
   Refresh[
    time = Clock[{0, Infinity}];
    anim[Clip[(time - offset) 90/dt, {0, 90}]]],
   anim[90]
   ]
  ]]
$\endgroup$
1
$\begingroup$

Modifying my answer about the timer, you can create your own animator.

Dynamic[step = Min[step + 1, 90], TrackedSymbols -> {}, UpdateInterval -> .001]

where UpdateInterval controls the speed.

Then using your code as is:

step = 0;
cAnim[x_] := Graphics3D[Rotate[Cuboid[], x Degree, {0, 0, 1}]]
sqAnim[x_] := Graphics[Rotate[Rectangle[], x Degree]];
anim = Dynamic[cAnim[step]];
Style[Dynamic[step = Min[step + 1, 90], TrackedSymbols -> {}, UpdateInterval -> .001], 
      0, Transparent]
{ Button["CUBE", step = 0; anim = Dynamic[cAnim[step]]], 
    Button["SQUARE", step = 0; anim = Dynamic[sqAnim[step]]]}
Dynamic[anim]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.