7
$\begingroup$

I would like my Animator to pause when the mouse is not over it. For example, here it continues to update even when it's not moused-over:

Mouseover["not updating",
 {Animator[Dynamic[n], {0, 100, 1}], Dynamic[n]}]

enter image description here

Is there a simple trick to pause it (keeping n static) until it is moused-over again?

$\endgroup$
1
  • $\begingroup$ Can you give more specifics about what exactly you are trying to accomplish? There may be other solutions with manipulate or a user created animator which may or may not be acceptable depending on what you are trying to do. $\endgroup$
    – bRost03
    Apr 9 '20 at 18:37
5
+50
$\begingroup$

The Animator object has a lot of internal controls that are a bit difficult to access. Another solution, if you don't need an Animator object, is to define your own.

mouseSlider[max_,rate_] := DynamicModule[{running, i = 0}, Mouseover[
   running = False; "not running", running = True; 
   Dynamic[i += rate; {Slider[Mod[i, max], {0, max}], Mod[i, max]}]]]

mouse slider

$\endgroup$
6
  • $\begingroup$ It's a good approach, but there's no obvious way (at least that I can see) to enforce an exact animation rate :( $\endgroup$
    – M.R.
    Apr 9 '20 at 22:14
  • $\begingroup$ I included a rate in the definition of mouseSlider. You could also use a Pause command. If you give more details about what you're trying to accomplish, I may be able to tailor my answer to better suit your situation $\endgroup$
    – bRost03
    Apr 10 '20 at 3:26
  • $\begingroup$ Not my question, but just saying using an ++ increment has no connection to time $\endgroup$
    – M.R.
    Apr 10 '20 at 3:38
  • $\begingroup$ Are we looking at different things? You should see += rate (or += 0.01 in the animation) $\endgroup$
    – bRost03
    Apr 10 '20 at 3:57
  • $\begingroup$ Yes I’m talk about frame rates in real time $\endgroup$
    – M.R.
    Apr 10 '20 at 4:00
3
$\begingroup$

An extended comment. I tried with:

DynamicModule[{running = False, n = 0},
   EventHandler[
     Animator[Dynamic[n], {0, 100, 1},   AnimationRunning -> Dynamic[running]   ]
   , { "MouseEntered" :> FEPrivate`Set[running, True]
     , "MouseExited" :> FEPrivate`Set[running, False]   
     }
   ]
]

But at least on Win10 v12.1 the internal timer goes on and when on MouseExited and when it runs again it starts from the position it would have had without stopping.

EDIT: The solution

If we are less subtle it will work:

DynamicModule[{running = False, n = 0},
   EventHandler[
     Dynamic @ Animator[Dynamic[n], {0, 100, 1}, AnimationRunning -> running ]
   , { "MouseEntered" :> FEPrivate`Set[running, True]
     , "MouseExited" :> FEPrivate`Set[running, False]   
     }
   ]
]

It isn't completely FE-side anymore, evaluation of the whole Animator[...] goes back and forth, but at least it works.

$\endgroup$
4
  • 1
    $\begingroup$ I've been running in circles, can't figure out why it keeps going internally... any other approaches? $\endgroup$
    – user5601
    Apr 6 '20 at 18:03
  • $\begingroup$ The scheduled task approach might be a bit heavy, especially if you try to make a grid of these things... are there no other dynamic magic tricks you know of to try here? $\endgroup$
    – user5601
    Apr 7 '20 at 4:29
  • $\begingroup$ @user5601 See the edit, I am not happy about it but it more acceptable than tasks. $\endgroup$
    – Kuba
    Apr 8 '20 at 7:18
  • $\begingroup$ This is nice, but if I make 50 of them the FE does the spinny wheel... also TableView, how might you get this to work in a TableView? $\endgroup$
    – user5601
    Apr 8 '20 at 17:00

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.