|
| 1 | +Animations |
| 2 | +========== |
| 3 | + |
| 4 | +Animation loop |
| 5 | +-------------- |
| 6 | + |
| 7 | +The "slow" approach |
| 8 | ++++++++++++++++++++ |
| 9 | + |
| 10 | +You can make an animation loop using a simple ``for-loop`` in Python and using the ``sleep`` function from the standard ``time`` module. |
| 11 | +It is essential that you use the ``hold_canvas`` context manager in order to improve the performances of your animation. |
| 12 | + |
| 13 | +A simple animation loop will look like the following: |
| 14 | + |
| 15 | +.. code:: Python |
| 16 | +
|
| 17 | + from time import sleep |
| 18 | +
|
| 19 | + from ipycanvas import Canvas, hold_canvas |
| 20 | +
|
| 21 | + canvas = Canvas() |
| 22 | + display(canvas) |
| 23 | +
|
| 24 | + # Number of steps in your animation |
| 25 | + steps_number = 200 |
| 26 | +
|
| 27 | + for i in range(steps_number): |
| 28 | + with hold_canvas(canvas): |
| 29 | + # Clear the old animation step |
| 30 | + canvas.clear() |
| 31 | +
|
| 32 | + # Perfom all your drawings here |
| 33 | + # ... |
| 34 | + # ... |
| 35 | +
|
| 36 | + # Animation frequency ~50Hz = 1./50. seconds |
| 37 | + sleep(0.02) |
| 38 | +
|
| 39 | +
|
| 40 | +You can also make an infinite animation using a ``while`` loop: |
| 41 | + |
| 42 | +.. code:: Python |
| 43 | +
|
| 44 | + from time import sleep |
| 45 | +
|
| 46 | + from ipycanvas import Canvas, hold_canvas |
| 47 | +
|
| 48 | + canvas = Canvas() |
| 49 | + display(canvas) |
| 50 | +
|
| 51 | + while(True): |
| 52 | + with hold_canvas(canvas): |
| 53 | + # Clear the old animation step |
| 54 | + canvas.clear() |
| 55 | +
|
| 56 | + # Perfom all your drawings here |
| 57 | + # ... |
| 58 | + # ... |
| 59 | +
|
| 60 | + # Animation frequency ~50Hz = 1./50. seconds |
| 61 | + sleep(0.02) |
| 62 | +
|
| 63 | +
|
| 64 | +Making an animation with ``from time import sleep`` should be fast enough in most cases. |
| 65 | +If you draw lots of shapes in your animation loop, you can decrease the animation frequency by sleeping a bit longer: *e.g.* ``sleep(0.033)`` for a ~30Hz animation. |
| 66 | +You should also make use of the vectorized versions of the methods ``fill_rect``, ``fill_circle`` etc if you use them a lot, see :ref:`drawing_shapes`. |
| 67 | + |
| 68 | +This approach might be slow if there is latency between the server and the Jupyter client, and if the server and the client are not the same machine (on MyBinder for example). |
| 69 | +In that case, the next approach is preferable. |
| 70 | + |
| 71 | + |
| 72 | +The "fast" approach |
| 73 | ++++++++++++++++++++ |
| 74 | + |
| 75 | +Because it's more complicated, this approach is only recommended if the "slow" approach is not fast enough in your case, or if the server is not on the same machine as the client. |
| 76 | + |
| 77 | +Unlike the slow approach, we will use the ``sleep`` method of your canvas instead of using the ``time`` module. |
| 78 | +The ``sleep`` method will ask your canvas to sleep for a certain amount of time, unlike the ``time`` module, the canvas's ``sleep`` method takes a duration in millisecond. |
| 79 | + |
| 80 | +Using this approach, it is recommended to wrap the entire animation in the ``hold_canvas`` context. This way, you will send the entire animation as a single message |
| 81 | +to the Jupyter client, and the animation will run entirely without any communication with the server. |
| 82 | + |
| 83 | +.. code:: Python |
| 84 | +
|
| 85 | + from time import sleep |
| 86 | +
|
| 87 | + from ipycanvas import Canvas, hold_canvas |
| 88 | +
|
| 89 | + canvas = Canvas() |
| 90 | + display(canvas) |
| 91 | +
|
| 92 | + # Number of steps in your animation |
| 93 | + steps_number = 200 |
| 94 | +
|
| 95 | + # Note how `hold_canvas` now wraps the entire for-loop |
| 96 | + with hold_canvas(canvas): |
| 97 | + for i in range(steps_number): |
| 98 | + # Clear the old animation step |
| 99 | + canvas.clear() |
| 100 | +
|
| 101 | + # Perfom all your drawings here |
| 102 | + # ... |
| 103 | + # ... |
| 104 | +
|
| 105 | + # Animation frequency ~50Hz = 1000./50. milliseconds |
| 106 | + canvas.sleep(20) |
| 107 | +
|
| 108 | +
|
| 109 | +You cannot make an infinite animation using this approach. |
0 commit comments