Skip to content

Commit 7f41ed0

Browse files
committed
Add configurable throttling for pan_zoom
1 parent 62ae3af commit 7f41ed0

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

ipympl/backend_nbagg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
CaselessStrEnum,
4949
CInt,
5050
Enum,
51+
Float,
5152
Instance,
5253
List,
5354
Unicode,
@@ -173,6 +174,7 @@ class Canvas(DOMWidget, FigureCanvasWebAggCore):
173174

174175
resizable = Bool(True).tag(sync=True)
175176
capture_scroll = Bool(False).tag(sync=True)
177+
pan_zoom_throttle = Float(33).tag(sync=True)
176178

177179
# This is a very special widget trait:
178180
# We set "sync=True" because we want ipywidgets to consider this

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@
8282
},
8383
"dependencies": {
8484
"@jupyter-widgets/base": "^2 || ^3 || ^4.0.0",
85-
"@types/node": "^14.14.35"
85+
"@types/node": "^14.14.35",
86+
"lodash": "^4.17.21"
8687
},
8788
"keywords": [
8889
"jupyter",

src/mpl_widget.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { throttle } from 'lodash';
2+
13
import {
24
DOMWidgetModel,
35
DOMWidgetView,
@@ -35,6 +37,7 @@ export class MPLCanvasModel extends DOMWidgetModel {
3537
toolbar_position: 'horizontal',
3638
resizable: true,
3739
capture_scroll: false,
40+
pan_zoom_throttle: 33,
3841
_data_url: null,
3942
_width: 0,
4043
_height: 0,
@@ -517,7 +520,7 @@ export class MPLCanvasView extends DOMWidgetView {
517520
);
518521
top_canvas.addEventListener(
519522
'mousemove',
520-
this.mouse_event('motion_notify')
523+
throttle(this.mouse_event('motion_notify'), this.model.get('pan_zoom_throttle'))
521524
);
522525

523526
top_canvas.addEventListener(
@@ -529,7 +532,7 @@ export class MPLCanvasView extends DOMWidgetView {
529532
this.mouse_event('figure_leave')
530533
);
531534

532-
top_canvas.addEventListener('wheel', this.mouse_event('scroll'));
535+
top_canvas.addEventListener('wheel', throttle(this.mouse_event('scroll'), this.model.get('pan_zoom_throttle')));
533536

534537
canvas_div.appendChild(canvas);
535538
canvas_div.appendChild(top_canvas);
@@ -656,7 +659,6 @@ export class MPLCanvasView extends DOMWidgetView {
656659
}
657660

658661
mouse_event(name: string) {
659-
let last_update = 0;
660662
return (event: any) => {
661663
const canvas_pos = utils.get_mouse_position(event, this.top_canvas);
662664

@@ -708,22 +710,16 @@ export class MPLCanvasView extends DOMWidgetView {
708710
}
709711
}
710712

711-
// Rate-limit the position text updates so that we don't overwhelm the
712-
// system.
713-
if (Date.now() > last_update + 16) {
714-
last_update = Date.now();
715-
716-
const x = canvas_pos.x * this.model.ratio;
717-
const y = canvas_pos.y * this.model.ratio;
718-
719-
this.model.send_message(name, {
720-
x: x,
721-
y: y,
722-
button: event.button,
723-
step: event.step,
724-
guiEvent: utils.get_simple_keys(event),
725-
});
726-
}
713+
const x = canvas_pos.x * this.model.ratio;
714+
const y = canvas_pos.y * this.model.ratio;
715+
716+
this.model.send_message(name, {
717+
x: x,
718+
y: y,
719+
button: event.button,
720+
step: event.step,
721+
guiEvent: utils.get_simple_keys(event),
722+
});
727723
};
728724
}
729725

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5636,7 +5636,7 @@ lodash.truncate@^4.4.2:
56365636
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
56375637
integrity sha1-WjUNoLERO4N+z//VgSy+WNbq4ZM=
56385638

5639-
lodash@4, [email protected], [email protected], lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.4, lodash@^4.7.0:
5639+
lodash@4, [email protected], [email protected], lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.21, lodash@^4.17.4, lodash@^4.7.0:
56405640
version "4.17.21"
56415641
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
56425642
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

0 commit comments

Comments
 (0)