Skip to content

Commit 7936dbc

Browse files
committed
Update docs
1 parent bfdb9d5 commit 7936dbc

File tree

8 files changed

+1170
-13
lines changed

8 files changed

+1170
-13
lines changed

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ipycanvas
2+
sphinx===4.4.0
23
jupyterlite-sphinx
34
pydata-sphinx-theme

docs/source/animation.ipynb

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import micropip\n",
10+
"\n",
11+
"await micropip.install(\"ipycanvas\")"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"import asyncio\n",
21+
"\n",
22+
"from ipycanvas import Canvas, hold_canvas"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": null,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": [
31+
"import numpy as np"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"from math import pi, cos, sin"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"def fast_draw(canvas, t):\n",
50+
" \"\"\"Same as draw, but using NumPy and the vectorized version of fill_circle: fill_circles\"\"\"\n",
51+
" size = 1000\n",
52+
" step = 20\n",
53+
" t1 = t / 1000.0\n",
54+
"\n",
55+
" x = np.linspace(0, size, int(size / step))\n",
56+
" y = np.linspace(0, size, int(size / step))\n",
57+
" xv, yv = np.meshgrid(x, y)\n",
58+
"\n",
59+
" x_angle = y_angle = 2 * pi\n",
60+
"\n",
61+
" angle = x_angle * (xv / size) + y_angle * (yv / size)\n",
62+
"\n",
63+
" particle_x = xv + 20 * np.cos(2 * pi * t1 + angle)\n",
64+
" particle_y = yv + 20 * np.sin(2 * pi * t1 + angle)\n",
65+
"\n",
66+
" canvas.fill_circles(particle_x, particle_y, 6)"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"size = 1000\n",
76+
"canvas = Canvas(width=size, height=size)\n",
77+
"canvas"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"metadata": {},
84+
"outputs": [],
85+
"source": [
86+
"from time import sleep\n",
87+
"\n",
88+
"for i in range(200):\n",
89+
" with hold_canvas(canvas):\n",
90+
" canvas.clear()\n",
91+
" canvas.fill_style = \"white\"\n",
92+
" canvas.fill_rect(0, 0, size, size)\n",
93+
" canvas.fill_style = \"#fcba03\"\n",
94+
"\n",
95+
" fast_draw(canvas, i * 20.0)\n",
96+
"\n",
97+
" await asyncio.sleep(20 / 1000.0)"
98+
]
99+
}
100+
],
101+
"metadata": {
102+
"kernelspec": {
103+
"display_name": "Python 3",
104+
"language": "python",
105+
"name": "python3"
106+
},
107+
"language_info": {
108+
"codemirror_mode": {
109+
"name": "ipython",
110+
"version": 3
111+
},
112+
"file_extension": ".py",
113+
"mimetype": "text/x-python",
114+
"name": "python",
115+
"nbconvert_exporter": "python",
116+
"pygments_lexer": "ipython3",
117+
"version": "3.9.5"
118+
}
119+
},
120+
"nbformat": 4,
121+
"nbformat_minor": 4
122+
}

docs/source/clock.ipynb

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import micropip\n",
10+
"\n",
11+
"await micropip.install(\"ipycanvas\")"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"from ipycanvas import Canvas, hold_canvas\n",
21+
"import numpy as np\n",
22+
"\n",
23+
"CLOCK_RADIUS = 100\n",
24+
"\n",
25+
"canvas = Canvas(width=CLOCK_RADIUS * 2.5, height=CLOCK_RADIUS * 2.5)\n",
26+
"canvas.translate(CLOCK_RADIUS * 1.2, CLOCK_RADIUS * 1.2)\n",
27+
"\n",
28+
"\n",
29+
"def clear_drawing():\n",
30+
" canvas.clear_rect(\n",
31+
" -CLOCK_RADIUS * 1.2, -CLOCK_RADIUS * 1.2, canvas.width, canvas.height\n",
32+
" )\n",
33+
"\n",
34+
"\n",
35+
"def minutes_vec(minutes):\n",
36+
" a = minutes * np.pi / 30\n",
37+
" return [np.sin(a), -np.cos(a)]\n",
38+
"\n",
39+
"\n",
40+
"def draw_dial():\n",
41+
" ht = 10\n",
42+
" mt = 6\n",
43+
" ho = 20\n",
44+
" mo = 10\n",
45+
"\n",
46+
" canvas.text_align = \"center\"\n",
47+
" canvas.text_baseline = \"middle\"\n",
48+
"\n",
49+
" canvas.line_width = 2\n",
50+
" canvas.stroke_circle(0, 0, CLOCK_RADIUS)\n",
51+
"\n",
52+
" for m in range(60):\n",
53+
" a = m * np.pi / 30\n",
54+
" x, y = np.sin(a), -np.cos(a)\n",
55+
"\n",
56+
" canvas.line_width = 1\n",
57+
" if m % 5 == 0:\n",
58+
" canvas.stroke_line(\n",
59+
" x * (CLOCK_RADIUS - ht),\n",
60+
" y * (CLOCK_RADIUS - ht),\n",
61+
" x * CLOCK_RADIUS,\n",
62+
" y * CLOCK_RADIUS,\n",
63+
" )\n",
64+
" canvas.font = \"12px serif\"\n",
65+
" canvas.stroke_text(str(m), x * (CLOCK_RADIUS + mo), y * (CLOCK_RADIUS + mo))\n",
66+
" canvas.font = \"16px serif\"\n",
67+
" canvas.stroke_text(\n",
68+
" str(m // 5 if m > 0 else 12),\n",
69+
" x * (CLOCK_RADIUS - ho),\n",
70+
" y * (CLOCK_RADIUS - ho),\n",
71+
" )\n",
72+
" else:\n",
73+
" canvas.stroke_line(\n",
74+
" x * (CLOCK_RADIUS - mt),\n",
75+
" y * (CLOCK_RADIUS - mt),\n",
76+
" x * CLOCK_RADIUS,\n",
77+
" y * CLOCK_RADIUS,\n",
78+
" )\n",
79+
"\n",
80+
"\n",
81+
"def draw_hands(minutes):\n",
82+
" ms = 35\n",
83+
" hs = 50\n",
84+
"\n",
85+
" hrs = minutes // 60\n",
86+
" mins = minutes % 60\n",
87+
"\n",
88+
" mv = minutes_vec(mins)\n",
89+
" hv = minutes_vec(hrs * 5 + (mins / 12))\n",
90+
"\n",
91+
" canvas.line_width = 5\n",
92+
" canvas.stroke_line(0, 0, mv[0] * (CLOCK_RADIUS - ms), mv[1] * (CLOCK_RADIUS - ms))\n",
93+
" canvas.stroke_line(0, 0, hv[0] * (CLOCK_RADIUS - hs), hv[1] * (CLOCK_RADIUS - hs))\n",
94+
"\n",
95+
"\n",
96+
"def draw_clock(hours, minutes):\n",
97+
" with hold_canvas(canvas):\n",
98+
" clear_drawing()\n",
99+
" draw_dial()\n",
100+
" draw_hands((hours % 12) * 60 + minutes)\n",
101+
"\n",
102+
"\n",
103+
"canvas"
104+
]
105+
},
106+
{
107+
"cell_type": "code",
108+
"execution_count": null,
109+
"metadata": {},
110+
"outputs": [],
111+
"source": [
112+
"import datetime\n",
113+
"import ipywidgets as widgets\n",
114+
"\n",
115+
"now = datetime.datetime.now()\n",
116+
"\n",
117+
"hour_text = widgets.IntText(\n",
118+
" value=now.hour, continuous_update=True, layout={\"width\": \"50px\"}\n",
119+
")\n",
120+
"minute_text = widgets.IntText(\n",
121+
" value=now.minute, continuous_update=True, layout={\"width\": \"50px\"}\n",
122+
")\n",
123+
"\n",
124+
"\n",
125+
"def on_text_change(change):\n",
126+
" draw_clock(int(hour_text.value), int(minute_text.value))\n",
127+
"\n",
128+
"\n",
129+
"hour_text.observe(on_text_change, names=\"value\")\n",
130+
"minute_text.observe(on_text_change, names=\"value\")\n",
131+
"\n",
132+
"on_text_change(0)\n",
133+
"\n",
134+
"widgets.HBox([hour_text, widgets.Label(value=\":\"), minute_text])"
135+
]
136+
}
137+
],
138+
"metadata": {
139+
"kernelspec": {
140+
"display_name": "Python 3",
141+
"language": "python",
142+
"name": "python3"
143+
},
144+
"language_info": {
145+
"codemirror_mode": {
146+
"name": "ipython",
147+
"version": 3
148+
},
149+
"file_extension": ".py",
150+
"mimetype": "text/x-python",
151+
"name": "python",
152+
"nbconvert_exporter": "python",
153+
"pygments_lexer": "ipython3",
154+
"version": "3.9.5"
155+
}
156+
},
157+
"nbformat": 4,
158+
"nbformat_minor": 4
159+
}

0 commit comments

Comments
 (0)