Manipulate -> Pause Play at specific conditions
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]}
]
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]}
]
]
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]
}]
]