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

Skip to content

Commit eea9f63

Browse files
committed
Merge pull request #18361 from QuLogic/nbagg-fig-ratio
FIX: Store DPI ratio on figure instead of window for nbagg/webagg
1 parent 0b1eb01 commit eea9f63

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

lib/matplotlib/backends/web_backend/js/mpl.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ mpl.figure = function (figure_id, websocket, ondownload, parent_element) {
6262
this.ws.onopen = function () {
6363
fig.send_message('supports_binary', { value: fig.supports_binary });
6464
fig.send_message('send_image_mode', {});
65-
if (mpl.ratio !== 1) {
66-
fig.send_message('set_dpi_ratio', { dpi_ratio: mpl.ratio });
65+
if (fig.ratio !== 1) {
66+
fig.send_message('set_dpi_ratio', { dpi_ratio: fig.ratio });
6767
}
6868
fig.send_message('refresh', {});
6969
};
@@ -156,7 +156,10 @@ mpl.figure.prototype._init_canvas = function () {
156156
this.context.backingStorePixelRatio ||
157157
1;
158158

159-
mpl.ratio = (window.devicePixelRatio || 1) / backingStore;
159+
this.ratio = (window.devicePixelRatio || 1) / backingStore;
160+
if (this.ratio !== 1) {
161+
fig.send_message('set_dpi_ratio', { dpi_ratio: this.ratio });
162+
}
160163

161164
var rubberband_canvas = (this.rubberband_canvas = document.createElement(
162165
'canvas'
@@ -200,8 +203,8 @@ mpl.figure.prototype._init_canvas = function () {
200203
entry.devicePixelContentBoxSize[0].blockSize
201204
);
202205
} else {
203-
canvas.setAttribute('width', width * mpl.ratio);
204-
canvas.setAttribute('height', height * mpl.ratio);
206+
canvas.setAttribute('width', width * fig.ratio);
207+
canvas.setAttribute('height', height * fig.ratio);
205208
}
206209
canvas.setAttribute(
207210
'style',
@@ -399,10 +402,10 @@ mpl.figure.prototype.handle_resize = function (fig, msg) {
399402
};
400403

401404
mpl.figure.prototype.handle_rubberband = function (fig, msg) {
402-
var x0 = msg['x0'] / mpl.ratio;
403-
var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;
404-
var x1 = msg['x1'] / mpl.ratio;
405-
var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;
405+
var x0 = msg['x0'] / fig.ratio;
406+
var y0 = (fig.canvas.height - msg['y0']) / fig.ratio;
407+
var x1 = msg['x1'] / fig.ratio;
408+
var y1 = (fig.canvas.height - msg['y1']) / fig.ratio;
406409
x0 = Math.floor(x0) + 0.5;
407410
y0 = Math.floor(y0) + 0.5;
408411
x1 = Math.floor(x1) + 0.5;
@@ -415,8 +418,8 @@ mpl.figure.prototype.handle_rubberband = function (fig, msg) {
415418
fig.rubberband_context.clearRect(
416419
0,
417420
0,
418-
fig.canvas.width / mpl.ratio,
419-
fig.canvas.height / mpl.ratio
421+
fig.canvas.width / fig.ratio,
422+
fig.canvas.height / fig.ratio
420423
);
421424

422425
fig.rubberband_context.strokeRect(min_x, min_y, width, height);
@@ -599,8 +602,8 @@ mpl.figure.prototype.mouse_event = function (event, name) {
599602
this.canvas_div.focus();
600603
}
601604

602-
var x = canvas_pos.x * mpl.ratio;
603-
var y = canvas_pos.y * mpl.ratio;
605+
var x = canvas_pos.x * this.ratio;
606+
var y = canvas_pos.y * this.ratio;
604607

605608
this.send_message(name, {
606609
x: x,

lib/matplotlib/backends/web_backend/js/nbagg_mpl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mpl.mpl_figure_comm = function (comm, msg) {
5151
};
5252

5353
mpl.figure.prototype.handle_close = function (fig, msg) {
54-
var width = fig.canvas.width / mpl.ratio;
54+
var width = fig.canvas.width / fig.ratio;
5555
fig.root.removeEventListener('remove', this._remove_fig_handler);
5656

5757
// Update the output cell to use the data from the current canvas.
@@ -72,7 +72,7 @@ mpl.figure.prototype.close_ws = function (fig, msg) {
7272

7373
mpl.figure.prototype.push_to_output = function (_remove_interactive) {
7474
// Turn the data on the canvas into data in the output cell.
75-
var width = this.canvas.width / mpl.ratio;
75+
var width = this.canvas.width / this.ratio;
7676
var dataURL = this.canvas.toDataURL();
7777
this.cell_info[1]['text/html'] =
7878
'<img src="' + dataURL + '" width="' + width + '">';

0 commit comments

Comments
 (0)