Skip to content

Commit 6e9f9fa

Browse files
committed
Update docs
Signed-off-by: martinRenou <[email protected]>
1 parent 7319c0b commit 6e9f9fa

File tree

4 files changed

+353
-353
lines changed

4 files changed

+353
-353
lines changed

docs/source/drawing_paths.rst

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
Drawing paths
2+
=============
3+
4+
A path is a list of points, connected by segments of lines that can be of different shapes, curved or not,
5+
of different width and of different color. A path can be closed. To make shapes using paths, we take some
6+
extra steps:
7+
8+
- First, you create the path with ``begin_path``
9+
- Then you use drawing commands to add shapes into the path
10+
- Once the path has been created, you can ``stroke`` or ``fill`` the path to render it
11+
12+
Here are the functions used to perform these steps:
13+
14+
- ``begin_path()``: Creates a new path. Once created, future drawing commands are directed into the path and used to build the path up.
15+
- Draw commands like ``line_to`` and ``arc``
16+
- ``close_path()``: Adds a straight line to the path, going to the start of the current path.
17+
- ``stroke()``: Draws the shape by stroking its outline.
18+
- ``fill(rule)``: Draws a solid shape by filling the path's content area. The given fill rule is applied, possible rules are `nonzero` and `evenodd`.
19+
20+
.. code:: Python
21+
22+
from ipycanvas import Canvas
23+
24+
canvas = Canvas(width=100, height=100)
25+
26+
# Draw simple triangle shape
27+
canvas.begin_path()
28+
canvas.move_to(75, 50)
29+
canvas.line_to(100, 75)
30+
canvas.line_to(100, 25)
31+
canvas.fill()
32+
33+
canvas
34+
35+
.. image:: images/triangle.png
36+
37+
38+
Draw commands
39+
-------------
40+
41+
Here are the available draw commands:
42+
43+
- ``move_to(x, y)``: Moves the pen to the coordinates specified by x and y. This does not actually draw anything.
44+
- ``line_to(x, y)``: Add a straight line to the current path by connecting the path’s last point to the specified (x, y) coordinates.
45+
- ``arc(x, y, radius, start_angle, end_angle, anticlockwise=False)``: Create a circular arc centered at (x, y) with a radius
46+
of ``radius``. The path starts at ``start_angle`` and ends at ``end_angle`` in radians, and travels in the direction given by
47+
``anticlockwise`` (defaulting to clockwise: False).
48+
- ``arc_to(x1, y1, x2, y2, radius)``: Add a circular arc to the current path. Using the given control points (``x1``, ``y1``)
49+
and (``x2``, ``y2``) and the ``radius``.
50+
- ``quadratic_curve_to(cp1x, cp1y, x, y)``: Add a quadratic Bezier curve to the current path.
51+
It requires two points: the first one is a control point and the second one is the end point. The starting point is the latest point in the current path, which can be changed using ``move_to()`` before creating the quadratic Bezier curve.
52+
- ``bezier_curve_to(cp1x, cp1y, cp2x, cp2y, x, y)``: Add a cubic Bezier curve to the current path.
53+
It requires three points: the first two are control points and the third one is the end point. The starting point is the latest point in the current path, which can be changed using ``move_to()`` before creating the Bezier curve.
54+
- ``rect(x, y, width, height)``: Draws a rectangle whose top-left corner is specified by (``x``, ``y``) with the specified ``width`` and ``height``.
55+
56+
57+
Examples
58+
--------
59+
60+
Stroke arcs
61+
+++++++++++
62+
63+
.. code:: Python
64+
65+
from math import pi
66+
67+
from ipycanvas import Canvas
68+
69+
canvas = Canvas(width=200, height=200)
70+
71+
# Draw smiley face
72+
canvas.begin_path()
73+
canvas.arc(75, 75, 50, 0, pi * 2, True) # Outer circle
74+
canvas.move_to(110, 75)
75+
canvas.arc(75, 75, 35, 0, pi, False) # Mouth (clockwise)
76+
canvas.move_to(65, 65)
77+
canvas.arc(60, 65, 5, 0, pi * 2, True) # Left eye
78+
canvas.move_to(95, 65)
79+
canvas.arc(90, 65, 5, 0, pi * 2, True) # Right eye
80+
canvas.stroke()
81+
82+
canvas
83+
84+
.. image:: images/smiley.png
85+
86+
Fill bezier curves
87+
++++++++++++++++++
88+
89+
.. code:: Python
90+
91+
from ipycanvas import Canvas
92+
93+
canvas = Canvas(width=200, height=200)
94+
95+
# Cubic curves example
96+
canvas.begin_path()
97+
canvas.move_to(75, 40)
98+
canvas.bezier_curve_to(75, 37, 70, 25, 50, 25)
99+
canvas.bezier_curve_to(20, 25, 20, 62.5, 20, 62.5)
100+
canvas.bezier_curve_to(20, 80, 40, 102, 75, 120)
101+
canvas.bezier_curve_to(110, 102, 130, 80, 130, 62.5)
102+
canvas.bezier_curve_to(130, 62.5, 130, 25, 100, 25)
103+
canvas.bezier_curve_to(85, 25, 75, 37, 75, 40)
104+
canvas.fill()
105+
106+
canvas
107+
108+
.. image:: images/heart.png
109+
110+
Change the fill rule
111+
++++++++++++++++++++
112+
113+
.. code:: Python
114+
115+
from math import pi
116+
from ipycanvas import Canvas
117+
118+
canvas = Canvas(width=100, height=100)
119+
120+
canvas.begin_path()
121+
canvas.arc(50, 50, 30, 0, pi * 2, True)
122+
canvas.arc(50, 50, 15, 0, pi * 2, True)
123+
canvas.fill('evenodd')
124+
125+
canvas
126+
127+
.. image:: images/fill_rule.png

0 commit comments

Comments
 (0)