7
$\begingroup$

I do small animations with Manipulate, but sometimes I don't like that it runs over and over again from start. I would like to have the possibility to stop the animation after one run at the end of the parameter interval. Is there a way to get it this way?

$\endgroup$
6
  • 1
    $\begingroup$ Use Animate with AnimationRepetitions -> 1. $\endgroup$
    – Szabolcs
    Commented Jan 14, 2015 at 23:50
  • 2
    $\begingroup$ I believe this can be closed as a duplicate of: (17571) $\endgroup$
    – Mr.Wizard
    Commented Jan 15, 2015 at 0:04
  • 1
    $\begingroup$ @Mr.Wizard I think the question is valid. The OP asked about Manipulate not Animate. $\endgroup$ Commented Jan 15, 2015 at 0:28
  • $\begingroup$ @Algohi Fair enough. However if a good solution is to use Animate one must ask why that option is rejected? $\endgroup$
    – Mr.Wizard
    Commented Jan 15, 2015 at 0:29
  • 1
    $\begingroup$ @Mr.Wizard,I agree that the good solution is to use Animate. It will be good if someone finds a solution for Manipulate too. $\endgroup$ Commented Jan 15, 2015 at 0:35

3 Answers 3

6
$\begingroup$

The default Manipulator, FEPrivate`FrontEndResource["FEExpressions", "Manipulator04"], does not allow the option AnimationRepetitions to be passed to the Animator used by the Manipulator. However, there is another FEPrivate`FrontEndResource["FEExpressions", "Manipulator05"], that does allows further options to be passed. See

FileNameJoin[{$InstallationDirectory,
  "SystemFiles", "FrontEnd", "TextResources", "MiscExpressions.tr"}]

Also see my answer here: Logarithmic slider

However, the method used in my answer to that question does not work if I use FEPrivate`FrontEndResource["FEExpressions", "Manipulator05"]. Instead we can adapt the code for it to this situation. (This cumbersome workaround may convince one that Animator is a better choice for a simple application as described by the OP. In a more complicated Manipulate, this solution may still have some value.)

Manipulate[
 Plot[myvariable x, {x, 0, 100}],
 {myvariable, 0.1, 1, 
   myManipulator[##, AnimationRunning -> True, AnimationRepetitions -> 1] &}]

The code for myManipulator and the "Manipulator05" workaround man05 are given below.

Let me first point out the following way to combine the Animate behavior with a more general Manipulate:

Manipulate[
 Plot[myvariable x, {x, 0, 100}], {myvariable, 0.1, 1, Animator, 
  AnimationRunning -> True, AnimationRepetitions -> 1}]

This is basically all Animate does anyway.

Code dump

ClearAll[myManipulator];
Options[myManipulator] = 
  DeleteDuplicatesBy[
   Join[Options[Manipulator], Options[Slider], Options[Animator], 
    Options[InputField]], First];
With[{smallerRule = {Large -> Medium, Medium -> Small, 
    Small -> Tiny}}, 
 myManipulator[Dynamic[x_], range_: {0, 1}, opts : OptionsPattern[]] :=
   With[{imagesize = OptionValue[ImageSize] /. Automatic -> Medium, 
    inputfieldsize = 
     OptionValue[ImageSize] /. Automatic -> Medium /. smallerRule, 
    enabled = OptionValue[Enabled], 
    continuousaction = OptionValue[ContinuousAction], 
    appearance = 
     First[Cases[OptionValue[Appearance], 
        Tiny | Small | Medium | Large] /. {} -> {Automatic}], 
    labeled = ! FreeQ[OptionValue[Appearance], "Labeled"] || ! 
       FreeQ[OptionValue[AppearanceElements], "InlineInputField"], 
    opener = 
     OptionValue[AppearanceElements] /. {Automatic -> True, 
       All -> True, None -> False, 
       l_List :> (Cases[l, Except["InlineInputField"]] =!= {})}, 
    inputfield = 
     MatchQ[OptionValue[AppearanceElements], Automatic | All] || ! 
       FreeQ[OptionValue[AppearanceElements], "InputField"], 
    appearanceelements = 
     OptionValue[AppearanceElements] /. {Automatic -> All, None -> {},
        l_List :> 
        Cases[l, Except["InlineInputField" | "InputField"]]}, 
    autoaction = OptionValue[AutoAction], 
    exclusions = OptionValue[Exclusions]}, 
   ReleaseHold@
    MakeExpression[
     PaneBox[DynamicModuleBox[{Typeset`open$$ = ! 
          FreeQ[OptionValue[Appearance], "Open"], 
        Typeset`paused$$ = OptionValue[PausedTime], 
        Typeset`rate$$ = OptionValue[AnimationRate], 
        Typeset`dir$$ = OptionValue[AnimationDirection]}, 
       StyleBox[DynamicBox[man05[
          Dynamic[x],
          Dynamic[x],
          range,
          imagesize,
          inputfieldsize,
          enabled,
          continuousaction,
          appearance,
          labeled,
          opener,
          inputfield,
          appearanceelements,
          autoaction,
          exclusions,
          Dynamic[Typeset`open$$],
          Dynamic[Typeset`paused$$],
          Dynamic[Typeset`rate$$],
          Dynamic[Typeset`dir$$],
          FilterRules[{opts}, Options[Slider]],
          FilterRules[{opts}, Options[Animator]],
          FilterRules[{opts}, Options[InputField]],
          FilterRules[{opts}, Options[InputField]],(* 
          inline InputField *)
          Expression,(* 
          InputField type *)
          OptionValue[BaseStyle],
          OptionValue[LabelStyle]
          ]
         ],
        DynamicUpdating -> True], DynamicModuleValues :> {}], 
      BaselinePosition -> (OptionValue[BaselinePosition] /. 
         Automatic -> Baseline), 
      ImageMargins -> OptionValue[ImageMargins]], StandardForm]]]

man05 = StyleBox[
            PaneSelectorBox[{
                False -> GridBox[{
                        Join[
                            {SliderBox[#2, #3, Join[#19, {
                                        ImageSize -> #4,
                                        Enabled -> #6,
                                        ContinuousAction -> #7,
                                        Appearance -> #8,
                                        AutoAction -> #13,
                                        Exclusions -> #14}]]},
                            If[#10,
                                {ItemBox[TooltipBox[TogglerBox[#15, {

                True -> DynamicBox[
                  FEPrivate`FrontEndResource["FEBitmaps", 
                   "SquareMinusIcon"]],

                False -> DynamicBox[
                  FEPrivate`FrontEndResource["FEBitmaps", 
                   "SquarePlusIcon"]]},
                                    Appearance -> None,
                                    ImageSize -> Automatic
                                    ], 
              FEPrivate`FrontEndResource["FEStrings", 
               "ManipulatorTooltipShowControls"]], 
             Alignment -> {Left}]}, {}],
                            If[#9,
                                {InputFieldBox[#1, #23, Join[#22, {
                                        FieldSize -> {{4, 10}, {1, 2}},
                                        Enabled -> #6,
                                        Appearance -> {"Frameless", #8},

               BaseStyle -> Join[{"Manipulator"}, #25]}]]}, {}]
                            ]},
                        ColumnSpacings -> 0.6,
                        BaselinePosition -> {{1, 1}, Axis}],
                True -> GridBox[{
                        Join[
                            {SliderBox[#2, #3, Join[#19, {
                                        ImageSize -> #4,
                                        Enabled -> #6,
                                        ContinuousAction -> #7,
                                        Appearance -> #8,
                                        AutoAction -> #13,
                                        Exclusions -> #14}]]},
                            If[#10,
                                {ItemBox[TooltipBox[TogglerBox[#15, {

                True -> DynamicBox[
                  FEPrivate`FrontEndResource["FEBitmaps", 
                   "SquareMinusIcon"]],

                False -> DynamicBox[
                  FEPrivate`FrontEndResource["FEBitmaps", 
                   "SquarePlusIcon"]]},
                                    Appearance -> None,
                                    ImageSize -> Automatic
                                    ], 
              FEPrivate`FrontEndResource["FEStrings", 
               "ManipulatorTooltipHideControls"]], 
             Alignment -> {Left}]}, {}],
                            If[#9,
                                {InputFieldBox[#1, #23, Join[#22, {
                                        FieldSize -> {{4, 10}, {1, 2}},
                                        Enabled -> #6,
                                        Appearance -> {"Frameless", #8},

               BaseStyle -> Join[{"Manipulator"}, #25]}]]}, {}]
                            ],
                        Join[
                            {GridBox[{{
                                    StyleBox[
                                        InputFieldBox[#1, #23, Join[#21, {
                                                ImageSize -> {60, #5},
                                                ImageMargins -> {{10, 0}, {0, 0}},
                                                Enabled -> #6,
                                                Appearance -> #8,

                  BaseStyle -> 
                   Join[{"Manipulator"}, #24, {FontSize :> 
                    CurrentValue[{"ControlsFontSize", #5}]}]}]],
                                        ShowContents -> #11],
                                    AnimatorBox[#2, #3, Join[#20, {
                                            AnimationRate -> #17,
                                            ImageSize -> #4,
                                            Enabled -> #6,
                                            PausedTime -> #16,
                                            AnimationDirection -> #18,
                                            Appearance -> #8,
                                            AppearanceElements -> Dynamic[

                   If[#12 === All, {"StepLeftButton", 
                    "StepRightButton", "PlayPauseButton", 
                    "FasterSlowerButtons", "DirectionButton"}, #12]],
                                            Exclusions -> #14}]]
                                    }},
                                    ColumnSpacings -> {1.1},
                                    RowAlignments -> Center,
                                    RowMinHeight -> 0]},
                            If[#9, {"\[SpanFromLeft]"}, {}],
                            If[#10, {"\[SpanFromLeft]"}, {}]
                        ]},
                        ColumnAlignments -> Left,
                        ColumnSpacings -> 0.6,
                        RowSpacings -> {0.4, 0},
                        RowMinHeight -> 0.1,
                        BaselinePosition -> {{1, 1}, Axis}]},
                #15, 
                ImageSize -> Automatic, BaselinePosition -> Baseline],
            Deployed -> True,
            TooltipBoxOptions -> {ActionDelay -> Automatic}] &;
$\endgroup$
1
  • $\begingroup$ I still wonder why FEPrivate`FrontEndResource["FEExpressions", "Manipulator05"] in place of man05 does not work. Perhaps it late and I'm too tired to recognize a mistake somewhere. $\endgroup$
    – Michael E2
    Commented Jan 15, 2015 at 3:04
4
$\begingroup$

It is easy to make a Play button to make it play once. Does it have to use the Mathematica build in play button? Here is an example: (if this is not what you meant, please provide more information)

Manipulate[
 tick;
 If[f < maxFrequency,
  tick = Not[tick];
  f += 0.01];
 Plot[Sin[2 Pi f t], {t, -10, 10}],
 {{maxFrequency, 1, "max frequencey? (Hz)"}, .1, 2, .01},
 Button["play once", f = 0; tick = Not[tick]],
 {{tick, False}, None},
 TrackedSymbols :> {tick}
 ]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you for your answer, I would like to show you my Mathematica command, but I dont know how to embedded into this comment. $\endgroup$
    – marfi
    Commented Jan 27, 2015 at 19:53
  • $\begingroup$ As a plain copy paste, my code goes like this: Manipulate[ Plot[1/(Sqrt[2 [Pi]] ([Sigma] + t)) E^(-x^2/([Sigma] + t)^2), {x, -50, 50}, PlotRange -> {{-30, 30}, {0, 0.5}}], {[Sigma], 1, 2}, {t, 0, 50}] $\endgroup$
    – marfi
    Commented Jan 27, 2015 at 19:59
  • $\begingroup$ Maybe you can copy paste it to Mathematica notepad, to have it neat. My point is that when I run the variable t in Manipulate window, I would like it to run only once, not repeat. I wish a nice evening $\endgroup$
    – marfi
    Commented Jan 27, 2015 at 20:02
4
$\begingroup$

You can set AnimationRepetitions -> 1 as a suboption in DefaultOptions:

Manipulate[
 Plot[u x, {x, 0, 100}, PlotRange -> {0, 100}], 
 {u, 0.1, 1, DefaultOptions -> {Animator -> {AnimationRepetitions -> 1}}}]

enter image description here

Add AnimationDirection -> ForwardBackward as an option either in control

Manipulate[
 Plot[u x, {x, 0, 100}, PlotRange -> {0, 100}], 
 {u, 0.1, 1, 
  AnimationDirection -> ForwardBackward, 
  DefaultOptions -> {Animator -> {AnimationRepetitions -> 1}}}]

or inside DefaultOptions:

Manipulate[
 Plot[u x, {x, 0, 100}, PlotRange -> {0, 100}], 
 {u, 0.1, 1, 
  DefaultOptions -> 
     {Animator -> {AnimationRepetitions -> 1, 
         AnimationDirection -> ForwardBackward}}}]

to get

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Very helpful... (Oh... for what it is worth, I thanked you in my newly published book, Pixels & paintings.) $\endgroup$ Commented Dec 15, 2023 at 22:00
  • 1
    $\begingroup$ @DavidG.Stork, thank you thank you thank you. Beautiful book! (Very timely too -- as a gift for my daughter's upcoming birthday:) $\endgroup$
    – kglr
    Commented Dec 16, 2023 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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