Skip to content

Commit 707237b

Browse files
committed
First working version
1 parent d42eee6 commit 707237b

File tree

6 files changed

+294
-46
lines changed

6 files changed

+294
-46
lines changed

examples/introduction.ipynb

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Introduction"
7+
"# ipycanvas: Interactive Canvas"
88
]
99
},
1010
{
@@ -13,6 +13,8 @@
1313
"metadata": {},
1414
"outputs": [],
1515
"source": [
16+
"from math import pi\n",
17+
"\n",
1618
"import ipycanvas"
1719
]
1820
},
@@ -22,7 +24,8 @@
2224
"metadata": {},
2325
"outputs": [],
2426
"source": [
25-
"w = ipycanvas.Canvas()"
27+
"c = ipycanvas.Canvas()\n",
28+
"c.layout.height = '200px'"
2629
]
2730
},
2831
{
@@ -31,7 +34,39 @@
3134
"metadata": {},
3235
"outputs": [],
3336
"source": [
34-
"w"
37+
"c"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"# Draw smiley face\n",
47+
"c.begin_path();\n",
48+
"c.arc(75, 75, 50, 0, pi * 2, True); # Outer circle\n",
49+
"c.move_to(110, 75);\n",
50+
"c.arc(75, 75, 35, 0, pi, False); # Mouth (clockwise)\n",
51+
"c.move_to(65, 65);\n",
52+
"c.arc(60, 65, 5, 0, pi * 2, True); # Left eye\n",
53+
"c.move_to(95, 65);\n",
54+
"c.arc(90, 65, 5, 0, pi * 2, True); # Right eye\n",
55+
"c.stroke();"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"# Draw simple triangle shape\n",
65+
"c.begin_path();\n",
66+
"c.move_to(75, 50);\n",
67+
"c.line_to(100, 75);\n",
68+
"c.line_to(100, 25);\n",
69+
"c.fill();"
3570
]
3671
}
3772
],

ipycanvas/canvas.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,53 @@ class Canvas(DOMWidget):
1616
_view_name = Unicode('CanvasView').tag(sync=True)
1717
_view_module = Unicode(module_name).tag(sync=True)
1818
_view_module_version = Unicode(module_version).tag(sync=True)
19+
20+
# Rectangles methods
21+
def fill_rect(self, x, y, width, height):
22+
"""Draw a filled rectangle."""
23+
self._send_canvas_msg('fillRect', x, y, width, height)
24+
25+
def stroke_rect(self, x, y, width, height):
26+
"""Draw a rectangular outline."""
27+
self._send_canvas_msg('strokeRect', x, y, width, height)
28+
29+
def clear_rect(self, x, y, width, height):
30+
"""Clear the specified rectangular area, making it fully transparent."""
31+
self._send_canvas_msg('clearRect', x, y, width, height)
32+
33+
# Paths methods
34+
def begin_path(self):
35+
self._send_canvas_msg('beginPath')
36+
37+
def close_path(self):
38+
self._send_canvas_msg('closePath')
39+
40+
def stroke(self):
41+
self._send_canvas_msg('stroke')
42+
43+
def fill(self):
44+
self._send_canvas_msg('fill')
45+
46+
def move_to(self, x, y):
47+
self._send_canvas_msg('moveTo', x, y)
48+
49+
def line_to(self, x, y):
50+
self._send_canvas_msg('lineTo', x, y)
51+
52+
def arc(self, x, y, radius, start_angle, end_angle, anticlockwise):
53+
self._send_canvas_msg('arc', x, y, radius, start_angle, end_angle, anticlockwise)
54+
55+
def arc_to(self, x1, y1, x2, y2, radius):
56+
self._send_canvas_msg('arcTo', x1, y1, x2, y2, radius)
57+
58+
def quadratic_curve_to(self, cp1x, cp1y, x, y):
59+
self._send_canvas_msg('quadraticCurveTo', cp1x, cp1y, x, y)
60+
61+
def bezier_curve_to(self, cp1x, cp1y, cp2x, cp2y, x, y):
62+
self._send_canvas_msg('bezierCurveTo', cp1x, cp1y, cp2x, cp2y, x, y)
63+
64+
def rect(self, x, y, width, height):
65+
self._send_canvas_msg('rect', x, y, width, height)
66+
67+
def _send_canvas_msg(self, msg_name, *args):
68+
self.send({'msg': msg_name, 'args': args})

0 commit comments

Comments
 (0)