1
$\begingroup$

I have a random walk and I created a checkbox for the animation of the walk.

I want the checkbox to do the followings, but I have not found a solution yet:

  • check the checkbox -> the slider of n gets expanded and the animation starts

Here is the MWE:

pts1 = Table[{x, x^2}, {x, -10, 10}]
pts2 = Table[{x, 0.5*x^2}, {x, -10, 10}]

Manipulate[
 Graphics[
  {{Line[Take[pts1, n]],
    Line[Take[pts2, n]]
    }},
  Axes -> True, PlotRange -> {{-20, 20}, {0, 100}}
  ],

 {{check, False}, {True, False}},

 {{n, 21}, 1, 21, 1,
  Animator, AnimationRunning -> check
  }]

Also, I really want the animation to run for only once. I tried

{{n, 21}, 1, 21, 1, AnimationRepetitions -> 1,
 Animator, AnimationRunning -> check
 }

but it seems to freeze the animation.

Anyone knows how to deal with the two problems?

$\endgroup$
2
  • $\begingroup$ check the checkbox -> the slider of n gets expanded and the animation starts I do not understand the above. As it is now, when I checked the box, it started running OK and the slider was moving. What difference do you want in this part? It seems to do this already. Or are you saying you do not want the slider to show up at all unless the check is checked? $\endgroup$
    – Nasser
    Jun 2, 2020 at 4:35
  • $\begingroup$ @Nasser Yes. I want the slider to be hidden at first, and to be shown only when I check the checkbox. $\endgroup$
    – 5000101
    Jun 2, 2020 at 4:41

2 Answers 2

2
$\begingroup$

I want the slider to be hidden at first, and to be shown only when I check the checkbox

In this case I suggest you use OpenerView

enter image description here

code

pts1 = Table[{x, x^2}, {x, -10, 10}]
pts2 = Table[{x, 0.5*x^2}, {x, -10, 10}]

Manipulate[
 tick;

 g = Graphics[{{Line[Take[pts1, n]], Line[Take[pts2, n]]}}, 
   Axes -> True, PlotRange -> {{-20, 20}, {0, 100}}, 
   ImageSize -> {300, 200}];
 If[state == "auto" && n < max,
  n++;
  tick = Not[tick]
  ];
 Grid[{{Row[{"n = ", n}]}, {g}}]
 ,

 OpenerView[{"open",
   Grid[{{
      Row[{Button[
         Style["run", Small], {state = "auto"; If[n == max, n = 1]; 
          tick = Not[tick]}, ImageSize -> {30, 30}],
        Button[
         Style["stop", Small], {state = "manual"; tick = Not[tick]}, 
         ImageSize -> {30, 30}]}]
      ,
      Row[{
        "n ", 
        Manipulator[
         Dynamic[n, {n = #; state = "manual"; 
            tick = Not[tick]} &], {0, 21, 1}, ImageSize -> Tiny]}
       ]}
     }, Spacings -> {1, 1}
    ]
   }]
 ,
 {{tick, False}, None},
 {{state, "manual"}, None},
 {{n, 0}, None},
 {{max, 21}, None},
 ControlPlacement -> Above,
 TrackedSymbols :> {tick}
 ]
$\endgroup$
1
  • $\begingroup$ It's not what I initially expected but it works fairly well on my laptop. Thanks! $\endgroup$
    – 5000101
    Jun 2, 2020 at 18:01
1
$\begingroup$

One possibility is to nest Manipulate

pts1 = Table[{x, x^2}, {x, -10, 10}];
pts2 = Table[{x, 0.5*x^2}, {x, -10, 10}];

Manipulate[
 If[check,
  Manipulate[
   Graphics[
    {Line[pts1[[1 ;; n]]], Line[pts2[[1 ;; n]]]},
    Axes -> True,
    PlotRange -> {{-20, 20}, {0, 100}}],
   {{n, 21}, 1, 21, 1, Animator,
    AnimationRepetitions -> 2,
    AnimationRunning -> True},
   Paneled -> False],
  Graphics[
   {Line[pts1], Line[pts2]},
   Axes -> True,
   PlotRange -> {{-20, 20}, {0, 100}}]],
 {{check, False, "Enabled"}, {True, False}}]
$\endgroup$
1
  • $\begingroup$ It works for the MWE, but my Graphics contains a lot of other stuffs, so a nested Manipulate would not be applied well for me. Anyway, thanks for your advice! $\endgroup$
    – 5000101
    Jun 2, 2020 at 18:03

Your Answer

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

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