Skip to content

Commit d47bdb1

Browse files
authored
Merge pull request #393 from martinRenou/throttling_pan_zoom
Add configurable throttling for pan_zoom
2 parents 62ae3af + 6845bad commit d47bdb1

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-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: 24 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,10 @@ export class MPLCanvasView extends DOMWidgetView {
517520
);
518521
top_canvas.addEventListener(
519522
'mousemove',
520-
this.mouse_event('motion_notify')
523+
throttle(
524+
this.mouse_event('motion_notify'),
525+
this.model.get('pan_zoom_throttle')
526+
)
521527
);
522528

523529
top_canvas.addEventListener(
@@ -529,7 +535,13 @@ export class MPLCanvasView extends DOMWidgetView {
529535
this.mouse_event('figure_leave')
530536
);
531537

532-
top_canvas.addEventListener('wheel', this.mouse_event('scroll'));
538+
top_canvas.addEventListener(
539+
'wheel',
540+
throttle(
541+
this.mouse_event('scroll'),
542+
this.model.get('pan_zoom_throttle')
543+
)
544+
);
533545

534546
canvas_div.appendChild(canvas);
535547
canvas_div.appendChild(top_canvas);
@@ -656,7 +668,6 @@ export class MPLCanvasView extends DOMWidgetView {
656668
}
657669

658670
mouse_event(name: string) {
659-
let last_update = 0;
660671
return (event: any) => {
661672
const canvas_pos = utils.get_mouse_position(event, this.top_canvas);
662673

@@ -708,22 +719,16 @@ export class MPLCanvasView extends DOMWidgetView {
708719
}
709720
}
710721

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-
}
722+
const x = canvas_pos.x * this.model.ratio;
723+
const y = canvas_pos.y * this.model.ratio;
724+
725+
this.model.send_message(name, {
726+
x: x,
727+
y: y,
728+
button: event.button,
729+
step: event.step,
730+
guiEvent: utils.get_simple_keys(event),
731+
});
727732
};
728733
}
729734

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)