Skip to content

Commit 30ccca2

Browse files
authored
Merge pull request #185 from ondravondra/clockexample
added simple clock to the examples
2 parents 8cb5f93 + 0c44e96 commit 30ccca2

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

examples/clock.ipynb

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

0 commit comments

Comments
 (0)