Skip to content

Commit 20da623

Browse files
committed
Add docs for gradients
Signed-off-by: martinRenou <[email protected]>
1 parent 9af28f0 commit 20da623

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
6.02 KB
Loading
57.7 KB
Loading

docs/source/rough_canvas.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _rough_canvas:
2+
13
Rough Canvas
24
============
35

docs/source/styles_and_colors.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,79 @@ You can also change the global transparency.
2828
2929
.. image:: images/colored_rect.png
3030

31+
Color Gradients
32+
---------------
33+
34+
There are two canvas methods that allow you to create color gradients (either linear or radial) that you can use as a ``fill_style`` or ``stroke_style``.
35+
36+
- ``create_linear_gradient(x0, y0, x1, y1, color_stops)``: Create a ``LinearGradient`` object given the start point ``(x0, y0)``, end point ``(x1, y1)``, and color stops.
37+
- ``create_radial_gradient(x0, y0, r0, x1, y1, r1, color_stops)``: Create a ``RadialGradient`` object given the start circle ``(x0, y0, r0)``, end circle ``(x1, y1, r1)``, and color stops.
38+
39+
You need to provide the start point and end point of your gradient (those coordinates are global to the entire canvas, it's not per-shape).
40+
The color stops is a list of tuple ``(offset, color)``, ``offset`` being a number between 0 and 1, inclusive, representing the position of the
41+
color stop. 0 represents the start of the gradient and 1 represents the end.
42+
43+
.. code:: Python
44+
45+
from ipycanvas import Canvas
46+
47+
canvas = Canvas(width=700, height=50)
48+
49+
gradient = canvas.create_linear_gradient(
50+
0, 0, # Start position (x0, y0)
51+
700, 0, # End position (x1, y1)
52+
# List of color stops
53+
[
54+
(0, 'red'),
55+
(1 / 6, 'orange'),
56+
(2 / 6, 'yellow'),
57+
(3 / 6, 'green'),
58+
(4 / 6, 'blue'),
59+
(5 / 6, '#4B0082'),
60+
(1, 'violet')
61+
]
62+
)
63+
64+
canvas.fill_style = gradient
65+
canvas.fill_rect(0, 0, 700, 50)
66+
67+
canvas
68+
69+
.. image:: images/linear_gradient.png
70+
71+
.. code:: Python
72+
73+
from ipycanvas import Canvas
74+
75+
canvas = Canvas(width=570, height=200)
76+
77+
radial_gradient = canvas.create_radial_gradient(
78+
238, 50, 10, # Start circle (x0, y0, r0)
79+
238, 50, 300, # End circle (x1, y1, r1)
80+
[
81+
(0, '#8ED6FF'),
82+
(1, '#004CB3'),
83+
]
84+
)
85+
86+
canvas.fill_style = radial_gradient
87+
88+
canvas.fill_rect(0, 0, 570, 200)
89+
90+
canvas
91+
92+
.. image:: images/radial_gradient.png
93+
94+
Patterns
95+
--------
96+
97+
98+
99+
RoughCanvas
100+
-----------
101+
102+
ipycanvas provides a special ``Canvas`` class which will automatically give a hand-drawn style to your drawings: see the :ref:`rough_canvas` section.
103+
31104
Shadows
32105
-------
33106

0 commit comments

Comments
 (0)