Skip to content

Commit

Permalink
Merge pull request #258 from martinRenou/update_docs
Browse files Browse the repository at this point in the history
Update docs
  • Loading branch information
martinRenou authored Mar 8, 2022
2 parents a195fb1 + 7936dbc commit 5b4ee81
Show file tree
Hide file tree
Showing 8 changed files with 1,170 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ipycanvas
sphinx===4.4.0
jupyterlite-sphinx
pydata-sphinx-theme
122 changes: 122 additions & 0 deletions docs/source/animation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import micropip\n",
"\n",
"await micropip.install(\"ipycanvas\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"\n",
"from ipycanvas import Canvas, hold_canvas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from math import pi, cos, sin"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def fast_draw(canvas, t):\n",
" \"\"\"Same as draw, but using NumPy and the vectorized version of fill_circle: fill_circles\"\"\"\n",
" size = 1000\n",
" step = 20\n",
" t1 = t / 1000.0\n",
"\n",
" x = np.linspace(0, size, int(size / step))\n",
" y = np.linspace(0, size, int(size / step))\n",
" xv, yv = np.meshgrid(x, y)\n",
"\n",
" x_angle = y_angle = 2 * pi\n",
"\n",
" angle = x_angle * (xv / size) + y_angle * (yv / size)\n",
"\n",
" particle_x = xv + 20 * np.cos(2 * pi * t1 + angle)\n",
" particle_y = yv + 20 * np.sin(2 * pi * t1 + angle)\n",
"\n",
" canvas.fill_circles(particle_x, particle_y, 6)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"size = 1000\n",
"canvas = Canvas(width=size, height=size)\n",
"canvas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from time import sleep\n",
"\n",
"for i in range(200):\n",
" with hold_canvas(canvas):\n",
" canvas.clear()\n",
" canvas.fill_style = \"white\"\n",
" canvas.fill_rect(0, 0, size, size)\n",
" canvas.fill_style = \"#fcba03\"\n",
"\n",
" fast_draw(canvas, i * 20.0)\n",
"\n",
" await asyncio.sleep(20 / 1000.0)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
159 changes: 159 additions & 0 deletions docs/source/clock.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import micropip\n",
"\n",
"await micropip.install(\"ipycanvas\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipycanvas import Canvas, hold_canvas\n",
"import numpy as np\n",
"\n",
"CLOCK_RADIUS = 100\n",
"\n",
"canvas = Canvas(width=CLOCK_RADIUS * 2.5, height=CLOCK_RADIUS * 2.5)\n",
"canvas.translate(CLOCK_RADIUS * 1.2, CLOCK_RADIUS * 1.2)\n",
"\n",
"\n",
"def clear_drawing():\n",
" canvas.clear_rect(\n",
" -CLOCK_RADIUS * 1.2, -CLOCK_RADIUS * 1.2, canvas.width, canvas.height\n",
" )\n",
"\n",
"\n",
"def minutes_vec(minutes):\n",
" a = minutes * np.pi / 30\n",
" return [np.sin(a), -np.cos(a)]\n",
"\n",
"\n",
"def draw_dial():\n",
" ht = 10\n",
" mt = 6\n",
" ho = 20\n",
" mo = 10\n",
"\n",
" canvas.text_align = \"center\"\n",
" canvas.text_baseline = \"middle\"\n",
"\n",
" canvas.line_width = 2\n",
" canvas.stroke_circle(0, 0, CLOCK_RADIUS)\n",
"\n",
" for m in range(60):\n",
" a = m * np.pi / 30\n",
" x, y = np.sin(a), -np.cos(a)\n",
"\n",
" canvas.line_width = 1\n",
" if m % 5 == 0:\n",
" canvas.stroke_line(\n",
" x * (CLOCK_RADIUS - ht),\n",
" y * (CLOCK_RADIUS - ht),\n",
" x * CLOCK_RADIUS,\n",
" y * CLOCK_RADIUS,\n",
" )\n",
" canvas.font = \"12px serif\"\n",
" canvas.stroke_text(str(m), x * (CLOCK_RADIUS + mo), y * (CLOCK_RADIUS + mo))\n",
" canvas.font = \"16px serif\"\n",
" canvas.stroke_text(\n",
" str(m // 5 if m > 0 else 12),\n",
" x * (CLOCK_RADIUS - ho),\n",
" y * (CLOCK_RADIUS - ho),\n",
" )\n",
" else:\n",
" canvas.stroke_line(\n",
" x * (CLOCK_RADIUS - mt),\n",
" y * (CLOCK_RADIUS - mt),\n",
" x * CLOCK_RADIUS,\n",
" y * CLOCK_RADIUS,\n",
" )\n",
"\n",
"\n",
"def draw_hands(minutes):\n",
" ms = 35\n",
" hs = 50\n",
"\n",
" hrs = minutes // 60\n",
" mins = minutes % 60\n",
"\n",
" mv = minutes_vec(mins)\n",
" hv = minutes_vec(hrs * 5 + (mins / 12))\n",
"\n",
" canvas.line_width = 5\n",
" canvas.stroke_line(0, 0, mv[0] * (CLOCK_RADIUS - ms), mv[1] * (CLOCK_RADIUS - ms))\n",
" canvas.stroke_line(0, 0, hv[0] * (CLOCK_RADIUS - hs), hv[1] * (CLOCK_RADIUS - hs))\n",
"\n",
"\n",
"def draw_clock(hours, minutes):\n",
" with hold_canvas(canvas):\n",
" clear_drawing()\n",
" draw_dial()\n",
" draw_hands((hours % 12) * 60 + minutes)\n",
"\n",
"\n",
"canvas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"import ipywidgets as widgets\n",
"\n",
"now = datetime.datetime.now()\n",
"\n",
"hour_text = widgets.IntText(\n",
" value=now.hour, continuous_update=True, layout={\"width\": \"50px\"}\n",
")\n",
"minute_text = widgets.IntText(\n",
" value=now.minute, continuous_update=True, layout={\"width\": \"50px\"}\n",
")\n",
"\n",
"\n",
"def on_text_change(change):\n",
" draw_clock(int(hour_text.value), int(minute_text.value))\n",
"\n",
"\n",
"hour_text.observe(on_text_change, names=\"value\")\n",
"minute_text.observe(on_text_change, names=\"value\")\n",
"\n",
"on_text_change(0)\n",
"\n",
"widgets.HBox([hour_text, widgets.Label(value=\":\"), minute_text])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 5b4ee81

Please sign in to comment.