Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5156948

Browse filesBrowse files
committed
Handle HiDPI displays in WebAgg/NbAgg backends
1 parent 4f07807 commit 5156948
Copy full SHA for 5156948

File tree

Expand file treeCollapse file tree

2 files changed

+34
-5
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+34
-5
lines changed

‎lib/matplotlib/backends/backend_webagg_core.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_webagg_core.py
+17-2Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ def __init__(self, *args, **kwargs):
158158
# to the connected clients.
159159
self._current_image_mode = 'full'
160160

161+
# Store the DPI ratio of the browser. This is the scaling that
162+
# occurs automatically for all images on a HiDPI display.
163+
self._dpi_ratio = 1
164+
161165
def show(self):
162166
# show the figure window
163167
from matplotlib.pyplot import show
@@ -342,7 +346,7 @@ def handle_refresh(self, event):
342346

343347
def handle_resize(self, event):
344348
x, y = event.get('width', 800), event.get('height', 800)
345-
x, y = int(x), int(y)
349+
x, y = int(x) * 2, int(y) * 2
346350
fig = self.figure
347351
# An attempt at approximating the figure size in pixels.
348352
fig.set_size_inches(x / fig.dpi, y / fig.dpi)
@@ -359,6 +363,17 @@ def handle_send_image_mode(self, event):
359363
# The client requests notification of what the current image mode is.
360364
self.send_event('image_mode', mode=self._current_image_mode)
361365

366+
def handle_set_dpi_ratio(self, event):
367+
dpi_ratio = event.get('dpi_ratio', 1)
368+
if dpi_ratio != self._dpi_ratio:
369+
# We don't want to scale up the figure dpi more than once.
370+
if not hasattr(self.figure, '_original_dpi'):
371+
self.figure._original_dpi = self.figure.dpi
372+
self.figure.dpi = dpi_ratio * self.figure._original_dpi
373+
self._dpi_ratio = dpi_ratio
374+
self._force_full = True
375+
self.draw_idle()
376+
362377
def send_event(self, event_type, **kwargs):
363378
self.manager._send_event(event_type, **kwargs)
364379

@@ -444,7 +459,7 @@ def _get_toolbar(self, canvas):
444459
return toolbar
445460

446461
def resize(self, w, h):
447-
self._send_event('resize', size=(w, h))
462+
self._send_event('resize', size=(w / self.canvas._dpi_ratio, h / self.canvas._dpi_ratio))
448463

449464
def set_window_title(self, title):
450465
self._send_event('figure_label', label=title)

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

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/web_backend/mpl.js
+17-3Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Put everything inside the global mpl namespace */
22
window.mpl = {};
33

4+
45
mpl.get_websocket_type = function() {
56
if (typeof(WebSocket) !== 'undefined') {
67
return WebSocket;
@@ -49,7 +50,7 @@ mpl.figure = function(figure_id, websocket, ondownload, parent_element) {
4950
$(parent_element).append(this.root);
5051

5152
this._init_header(this);
52-
this._init_canvas(this);
53+
ratio = this._init_canvas(this);
5354
this._init_toolbar(this);
5455

5556
var fig = this;
@@ -59,6 +60,7 @@ mpl.figure = function(figure_id, websocket, ondownload, parent_element) {
5960
this.ws.onopen = function () {
6061
fig.send_message("supports_binary", {value: fig.supports_binary});
6162
fig.send_message("send_image_mode", {});
63+
fig.send_message("set_dpi_ratio", {'dpi_ratio': ratio});
6264
fig.send_message("refresh", {});
6365
}
6466

@@ -184,8 +186,9 @@ mpl.figure.prototype._init_canvas = function() {
184186
canvas_div.css('width', width)
185187
canvas_div.css('height', height)
186188

187-
canvas.attr('width', width);
188-
canvas.attr('height', height);
189+
canvas.attr('width', width * ratio);
190+
canvas.attr('height', height * ratio);
191+
canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');
189192

190193
rubberband.attr('width', width);
191194
rubberband.attr('height', height);
@@ -206,6 +209,17 @@ mpl.figure.prototype._init_canvas = function() {
206209
}
207210

208211
window.setTimeout(set_focus, 100);
212+
213+
var backingStore = this.context.backingStorePixelRatio ||
214+
this.context.webkitBackingStorePixelRatio ||
215+
this.context.mozBackingStorePixelRatio ||
216+
this.context.msBackingStorePixelRatio ||
217+
this.context.oBackingStorePixelRatio ||
218+
this.context.backingStorePixelRatio || 1;
219+
220+
var ratio = (window.devicePixelRatio || 1) / backingStore;
221+
222+
return ratio
209223
}
210224

211225
mpl.figure.prototype._init_toolbar = function() {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.