6
$\begingroup$

How can I pause play in Manipulate when a condition is met ? Here is an example:

Manipulate[
 Plot[i*Sin[x], {x, 0, 10}, PlotRange -> 10], {i, 1, 10, 
  AnimationRate -> 3, RefreshRate -> 60, Appearance -> "Open"}]

When I press the "Play" Buttom, i increases. Now, I want the animation to stop automatically, when i reaches a certain value, say i=5.

How can I do that ?

EDIT: Thanks a lot for the great answers. Would it be possible to implement the following:

  1. One can define points for which play will stop, for instance, at i=2, i=4, and i=8

  2. When i== One of the points -> Then play will stop, if I press the play buttom again, then it will continue to run until the next break point:

Example:

i==2 -> Stop

Manually: Press Play --> Continues

i==4 -> Stop

Manually: Press Play --> Continues

etc.

$\endgroup$
4
$\begingroup$

Manipulate by default uses Manipulator which doesn't seem to support dynamic values for the AnimationRunning option. However you could use ControlType -> Animator.

Here's a possible approach where by adding two extra control variables I can stop the animation after i reaches 5 on the first run.

Manipulate[
  If[initialRun && i >= 5, running = False; initialRun = False];
  Plot[i*Sin[x], {x, 0, 10}, PlotRange -> 10]
  ,
  {initialRun, {True, False}, ControlType -> None},
  {running, {True, False}, ControlType -> None},
  {i, 1, 10, ControlType -> Animator, AnimationRate -> 3, 
     RefreshRate -> 60, AnimationRunning -> Dynamic[running]}
]

Manipulate with one animation breakpoint

Note that if the user interacts with the animator before it gets paused by the Manipulate, the pausing will still happen whenever the value of i is 5 or greater first.

For a version with more than one breakpoint, replace the initialRun boolean with an integer counting how many runs have happened:

With[{breakPoints = {2, 4, 8}},
  Manipulate[
    If[runNumber <= Length[breakPoints] && i >= breakPoints[[runNumber]],
      running = False; runNumber++
    ];
    Plot[i*Sin[x], {x, 0, 10}, PlotRange -> 10]
    ,
    {runNumber, 1, ControlType -> None},
    {running, {True, False}, ControlType -> None},
    {i, 1, 10, ControlType -> Animator, AnimationRate -> 3, RefreshRate -> 60, AnimationRunning -> Dynamic[running]}
  ]
]

Manipulate with several animation breakpoints

$\endgroup$
  • $\begingroup$ +1 Good idea, it seems I am not able to think in this heat. :) $\endgroup$ – Kuba Aug 8 '18 at 15:42
  • $\begingroup$ +1, I arrived too late. $\endgroup$ – rhermans Aug 8 '18 at 15:44
  • $\begingroup$ Thank you very much! Great idea, but I would need it to stop when i=5 and then when I push the play button it should continue. Is this possible ? $\endgroup$ – james Aug 9 '18 at 5:39
  • $\begingroup$ Please see the recent edit for more information $\endgroup$ – james Aug 9 '18 at 6:12
  • $\begingroup$ Wow ! This is perfect ! Very well done !! Thank you very much. :)) $\endgroup$ – james Aug 9 '18 at 8:35
2
$\begingroup$

The credit goes to @Gerli, who arrived first. Difference is that mine uses DynamicModule

DynamicModule[
 {s = True},
 Manipulate[
  If[i > 5, s = False, s = True];
  Plot[i*Sin[x], {x, 0, 10}, PlotRange -> 10],
  {
   i, 1, 10
   , ControlType -> Animator
   , AnimationRunning -> Dynamic[s]
   }]
 ]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you very much! I would need it to stop when i=5 and then when I push the play button it should continue. Is this possible ? $\endgroup$ – james Aug 9 '18 at 5:39
  • $\begingroup$ Please have a look at the recent edit. Thanks :) $\endgroup$ – james Aug 9 '18 at 6:12

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.