Skip to content

Commit 294d402

Browse files
committed
Add documentation for the Canvas sizing
Signed-off-by: martinRenou <[email protected]>
1 parent 80b66c4 commit 294d402

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

docs/source/basic_usage.rst

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,47 @@ that does not need to update much while other objects moves a lot on the screen.
3636
Resize Canvas
3737
-------------
3838

39-
You can dynamically resize the ``Canvas`` and ``MultiCanvas``, note that this will clear the canvas.
39+
The ``Canvas`` and ``MultiCanvas`` have two sizes: the size of the color buffer in pixels, and the actual size
40+
displayed on the screen.
41+
42+
Color buffer size
43+
^^^^^^^^^^^^^^^^^
44+
45+
The color buffer size can dynamically be updated through the ``width`` and ``height`` properties (value in pixels), note that this will clear the canvas.
4046

4147
.. code:: Python
4248
4349
canvas.width = 300
4450
canvas.height = 600
4551
52+
Screen size
53+
^^^^^^^^^^^
54+
55+
The size on the screen can be updated through the ``layout`` property, which comes from ipywidgets (see https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Styling.html#The-layout-attribute). The ``layout`` property is an object which contains CSS properties for the canvas.
56+
57+
The default value for the ``width`` and ``height`` of the layout is "auto", this means the canvas will take the same screen size as the actual color buffer size: a ``Canvas`` of
58+
size ``800x600`` will take ``800x600`` pixels on the screen.
59+
60+
.. code:: Python
61+
62+
canvas.layout.width = 'auto'
63+
canvas.layout.height = 'auto'
64+
65+
In order to get a "responsive" ``Canvas`` which takes as much space as available while still respecting the aspect ratio, you will need to set the ``width``
66+
property to ``100%``, the ``height`` will automatically get computed:
67+
68+
.. code:: Python
69+
70+
canvas.layout.width = '100%'
71+
canvas.layout.height = 'auto'
72+
73+
One can also set the screen size value in pixels:
74+
75+
.. code:: Python
76+
77+
canvas.layout.width = '200px'
78+
canvas.layout.height = '500px'
79+
4680
Clear Canvas
4781
------------
4882

@@ -63,9 +97,9 @@ Optimizing drawings
6397

6498
By default, the Python ``Canvas`` object sends all the drawings commands like ``fill_rect``
6599
and ``arc`` one by one through the widgets communication layer. This communication is limited
66-
to 1000 commands/s and it can be extremely slow to send commands one after the other.
100+
to 1000 commands/s and it can be extremely slow to send commands one after the other.
67101
You can increase this limit via internal Jupyter `parameters <https://github.com/martinRenou/ipycanvas/issues/102>`_,
68-
however this is not recommended as it can lead to instability. Instead we provide a ``hold_canvas``
102+
however this is not recommended as it can lead to instability. Instead we provide a ``hold_canvas``
69103
context manager which allows you to hold all the commands and send them in a single batch at the end. For
70104
optimal performance you should try to use ``hold_canvas`` as much as possible.
71105

0 commit comments

Comments
 (0)