Skip to content

Commit e04a9c2

Browse files
authored
Merge pull request #140 from martinRenou/animations_docs
Add documentation for animations
2 parents 74cb7f7 + 2ba86b8 commit e04a9c2

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

docs/source/animations.rst

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.

docs/source/drawing_shapes.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _drawing_shapes:
2+
13
Drawing simple shapes
24
=====================
35

@@ -125,3 +127,45 @@ There is one command for drawing a straight line from one point to another:
125127
canvas
126128
127129
.. image:: images/lines.png
130+
131+
132+
Vectorized methods
133+
------------------
134+
135+
Some methods like ``fill_rect`` and ``fill_circle`` have a vectorized counterpart: ``fill_rects`` and ``fill_cicles``. It is essential
136+
to use those methods when you want to draw the same shape multiple times with the same style.
137+
138+
For example, it is way faster to run:
139+
140+
.. code:: Python
141+
142+
from ipycanvas import Canvas
143+
144+
canvas = Canvas(width=300, height=300)
145+
146+
canvas.global_alpha = 0.01
147+
148+
size = [i for i in range(300)]
149+
position = [300 - i for i in range(300)]
150+
151+
canvas.fill_rects(position, position, size)
152+
153+
canvas
154+
155+
instead of running:
156+
157+
.. code:: Python
158+
159+
from ipycanvas import Canvas
160+
161+
canvas = Canvas(width=300, height=300)
162+
163+
canvas.global_alpha = 0.01
164+
165+
for i in range(300):
166+
size = i
167+
position = 300 - i
168+
169+
canvas.fill_rect(position, position, size)
170+
171+
canvas

docs/source/usage.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Usage
1313
drawing_text
1414
drawing_images
1515
retrieve_images
16+
animations
1617
canvas_state
1718
transformations
1819
events

0 commit comments

Comments
 (0)