Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c08e251

Browse files
committed
Fix scale of mouse events when in HiDPI mode
1 parent 17c7259 commit c08e251

File tree

1 file changed

+11
-11
lines changed
  • lib/matplotlib/backends/web_backend

1 file changed

+11
-11
lines changed

lib/matplotlib/backends/web_backend/mpl.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ mpl.figure = function(figure_id, websocket, ondownload, parent_element) {
6060
this.ws.onopen = function () {
6161
fig.send_message("supports_binary", {value: fig.supports_binary});
6262
fig.send_message("send_image_mode", {});
63-
if (fig.ratio != 1) {
64-
fig.send_message("set_dpi_ratio", {'dpi_ratio': fig.ratio});
63+
if (mpl.ratio != 1) {
64+
fig.send_message("set_dpi_ratio", {'dpi_ratio': mpl.ratio});
6565
}
6666
fig.send_message("refresh", {});
6767
}
@@ -139,7 +139,7 @@ mpl.figure.prototype._init_canvas = function() {
139139
this.context.oBackingStorePixelRatio ||
140140
this.context.backingStorePixelRatio || 1;
141141

142-
this.ratio = (window.devicePixelRatio || 1) / backingStore;
142+
mpl.ratio = (window.devicePixelRatio || 1) / backingStore;
143143

144144
var rubberband = $('<canvas/>');
145145
rubberband.attr('style', "position: absolute; left: 0; top: 0; z-index: 1;")
@@ -197,8 +197,8 @@ mpl.figure.prototype._init_canvas = function() {
197197
canvas_div.css('width', width)
198198
canvas_div.css('height', height)
199199

200-
canvas.attr('width', width * this.ratio);
201-
canvas.attr('height', height * this.ratio);
200+
canvas.attr('width', width * mpl.ratio);
201+
canvas.attr('height', height * mpl.ratio);
202202
canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');
203203

204204
rubberband.attr('width', width);
@@ -332,10 +332,10 @@ mpl.figure.prototype.handle_resize = function(fig, msg) {
332332
}
333333

334334
mpl.figure.prototype.handle_rubberband = function(fig, msg) {
335-
var x0 = msg['x0'];
336-
var y0 = fig.canvas.height - msg['y0'];
337-
var x1 = msg['x1'];
338-
var y1 = fig.canvas.height - msg['y1'];
335+
var x0 = msg['x0'] / mpl.ratio;
336+
var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;
337+
var x1 = msg['x1'] / mpl.ratio;
338+
var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;
339339
x0 = Math.floor(x0) + 0.5;
340340
y0 = Math.floor(y0) + 0.5;
341341
x1 = Math.floor(x1) + 0.5;
@@ -491,8 +491,8 @@ mpl.figure.prototype.mouse_event = function(event, name) {
491491
this.canvas_div.focus();
492492
}
493493

494-
var x = canvas_pos.x;
495-
var y = canvas_pos.y;
494+
var x = canvas_pos.x * mpl.ratio;
495+
var y = canvas_pos.y * mpl.ratio;
496496

497497
this.send_message(name, {x: x, y: y, button: event.button,
498498
step: event.step,

0 commit comments

Comments
 (0)