Skip to content

Commit 293b685

Browse files
committed
Add debouncing, zoom with wheel
1 parent dfb9791 commit 293b685

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ channels:
44
dependencies:
55
- branca
66
- ipycanvas=0.4.1
7+
- ipyevents

examples/mapping.ipynb

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,45 @@
88
"source": [
99
"from math import pow, cos, pi, radians, degrees, atan, tan, sinh, log\n",
1010
"from ipywidgets import Image, Output, IntSlider\n",
11-
"from ipycanvas import Canvas, MultiCanvas"
11+
"from ipycanvas import Canvas, MultiCanvas\n",
12+
"from ipyevents import Event\n",
13+
"import asyncio"
14+
]
15+
},
16+
{
17+
"cell_type": "code",
18+
"execution_count": null,
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"class Timer:\n",
23+
" def __init__(self, timeout, callback):\n",
24+
" self._timeout = timeout\n",
25+
" self._callback = callback\n",
26+
" self._task = asyncio.ensure_future(self._job())\n",
27+
"\n",
28+
" async def _job(self):\n",
29+
" await asyncio.sleep(self._timeout)\n",
30+
" self._callback()\n",
31+
"\n",
32+
" def cancel(self):\n",
33+
" self._task.cancel()\n",
34+
"\n",
35+
"def debounce(wait):\n",
36+
" \"\"\" Decorator that will postpone a function's\n",
37+
" execution until after wait seconds\n",
38+
" have elapsed since the last time it was invoked. \"\"\"\n",
39+
" def decorator(fn):\n",
40+
" def debounced(*args, **kwargs):\n",
41+
" def call_it():\n",
42+
" fn(*args, **kwargs)\n",
43+
" try:\n",
44+
" debounced.t.cancel()\n",
45+
" except(AttributeError):\n",
46+
" pass\n",
47+
" debounced.t = Timer(wait, call_it)\n",
48+
" return debounced\n",
49+
" return decorator"
1250
]
1351
},
1452
{
@@ -166,7 +204,8 @@
166204
" self.dragging = True\n",
167205
" self.x_mouse = pixel_x\n",
168206
" self.y_mouse = pixel_y\n",
169-
" \n",
207+
" \n",
208+
" @debounce(0.01)\n",
170209
" def mouse_move_handler(self, pixel_x, pixel_y):\n",
171210
" if self.dragging:\n",
172211
" x, y, lat, lon = self.delta(pixel_x, pixel_y)\n",
@@ -205,16 +244,19 @@
205244
"metadata": {},
206245
"outputs": [],
207246
"source": [
208-
"slider = IntSlider(description='Zoom:', value=m.z, min=3, max=10, step=1)\n",
247+
"d = Event(source=m, watched_events=['wheel'])\n",
209248
"\n",
210-
"def on_slider_change(change):\n",
211-
" m.z = slider.value\n",
249+
"@out.capture()\n",
250+
"def zoom(event):\n",
251+
" m.z -= event['deltaY'] // 3\n",
252+
" m.z = max(m.z, 1)\n",
253+
" m.z = min(m.z, 19)\n",
254+
" print(m.z)\n",
212255
" m.tile_nb = numTiles(m.z)\n",
213256
" m.x, m.y = latlon2xy(m.lat, m.lon, m.z)\n",
214257
" m.show()\n",
215258
"\n",
216-
"slider.observe(on_slider_change, 'value')\n",
217-
"slider"
259+
"d.on_dom_event(zoom)"
218260
]
219261
},
220262
{
@@ -243,9 +285,9 @@
243285
"name": "python",
244286
"nbconvert_exporter": "python",
245287
"pygments_lexer": "ipython3",
246-
"version": "3.7.3"
288+
"version": "3.8.1"
247289
}
248290
},
249291
"nbformat": 4,
250-
"nbformat_minor": 2
292+
"nbformat_minor": 4
251293
}

0 commit comments

Comments
 (0)