Skip to content

Commit bb98ecb

Browse files
authored
Merge pull request #45 from martinRenou/add_shadows
Add shadows parameters
2 parents b8f39ad + d03be12 commit bb98ecb

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

docs/source/drawing_shapes.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,37 @@ You can also change the global transparency.
252252
253253
.. image:: images/colored_rect.png
254254

255+
Shadows
256+
+++++++
257+
258+
You can easily draw shadows by tweaking the following attributes:
259+
260+
- ``shadow_offset_x``: (float) Indicates the horizontal distance the shadow should extend from the object. This value isn't affected by the transformation matrix. The default is ``0``.
261+
- ``shadow_offset_y``: (float) Indicates the vertical distance the shadow should extend from the object. This value isn't affected by the transformation matrix. The default is ``0``.
262+
- ``shadow_blur``: (float) Indicates the size of the blurring effect; this value doesn't correspond to a number of pixels and is not affected by the current transformation matrix. The default value is ``0``.
263+
- ``shadow_color``: (valid HTML color) A standard CSS color value indicating the color of the shadow effect; by default, it is fully-transparent black: ``'rgba(0, 0, 0, 0)'``.
264+
265+
.. code:: Python
266+
267+
from ipycanvas import Canvas
268+
269+
canvas = Canvas(size=(200, 200))
270+
271+
canvas.shadow_color = 'green'
272+
canvas.shadow_offset_x = 2
273+
canvas.shadow_offset_y = 3
274+
canvas.shadow_blur = 3
275+
276+
canvas.fill_rect(25, 25, 100, 100)
277+
canvas.clear_rect(45, 45, 60, 60)
278+
279+
canvas.shadow_color = 'blue'
280+
canvas.stroke_rect(50, 50, 50, 50)
281+
282+
canvas
283+
284+
.. image:: images/shadows.png
285+
255286
Lines styles
256287
++++++++++++
257288

docs/source/drawing_text.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ You can change the text style by changing the following ``Canvas`` attributes:
4444
- ``direction``: (str) Directionality. Possible values: ``"ltr"``, ``"rtl"``, ``"inherit"``. The default value is ``"inherit"``.
4545

4646
.. note::
47-
You can still use ``fill_style`` and ``stroke_style`` for coloring text
47+
You can still use ``fill_style``, ``stroke_style``, ``shadow_color`` etc for coloring text and applying shadows.

docs/source/images/shadows.png

2.93 KB
Loading

ipycanvas/canvas.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import numpy as np
1010

11-
from traitlets import Enum, Float, Instance, List, Tuple, Unicode, observe
11+
from traitlets import Enum, Float, Instance, List, Tuple, Unicode
1212

1313
from ipywidgets import CallbackDispatcher, Color, DOMWidget, Image, widget_serialization
1414

@@ -71,6 +71,22 @@ class Canvas(DOMWidget):
7171
default_value='source-over'
7272
)
7373

74+
#: (float) Indicates the horizontal distance the shadow should extend from the object.
75+
#: This value isn't affected by the transformation matrix. The default is 0.
76+
shadow_offset_x = Float(0.0)
77+
78+
#: (float) Indicates the vertical distance the shadow should extend from the object.
79+
#: This value isn't affected by the transformation matrix. The default is 0.
80+
shadow_offset_y = Float(0.0)
81+
82+
#: (float) Indicates the size of the blurring effect; this value doesn't correspond to a number of pixels
83+
#: and is not affected by the current transformation matrix. The default value is 0.
84+
shadow_blur = Float(0.0)
85+
86+
#: (valid HTML color) A standard CSS color value indicating the color of the shadow effect; by default,
87+
#: it is fully-transparent black.
88+
shadow_color = Color('rgba(0, 0, 0, 0)')
89+
7490
#: (float) Sets the width of lines drawn in the future, must be a positive number. Default to ``1.0``.
7591
line_width = Float(1.0)
7692

@@ -428,7 +444,8 @@ def __setattr__(self, name, value):
428444

429445
canvas_attrs = ['fill_style', 'stroke_style', 'global_alpha', 'font', 'text_align',
430446
'text_baseline', 'direction', 'global_composite_operation',
431-
'line_width', 'line_cap', 'line_join', 'miter_limit', 'line_dash_offset']
447+
'line_width', 'line_cap', 'line_join', 'miter_limit', 'line_dash_offset',
448+
'shadow_offset_x', 'shadow_offset_y', 'shadow_blur', 'shadow_color']
432449
if name in canvas_attrs:
433450
command = {
434451
'name': 'set',

0 commit comments

Comments
 (0)