|
1 | 1 | /* Put everything inside the global mpl namespace */ |
2 | 2 | window.mpl = {}; |
3 | 3 |
|
| 4 | + |
4 | 5 | mpl.get_websocket_type = function() { |
5 | 6 | if (typeof(WebSocket) !== 'undefined') { |
6 | 7 | return WebSocket; |
@@ -59,6 +60,9 @@ mpl.figure = function(figure_id, websocket, ondownload, parent_element) { |
59 | 60 | this.ws.onopen = function () { |
60 | 61 | fig.send_message("supports_binary", {value: fig.supports_binary}); |
61 | 62 | fig.send_message("send_image_mode", {}); |
| 63 | + if (mpl.ratio != 1) { |
| 64 | + fig.send_message("set_dpi_ratio", {'dpi_ratio': mpl.ratio}); |
| 65 | + } |
62 | 66 | fig.send_message("refresh", {}); |
63 | 67 | } |
64 | 68 |
|
@@ -128,6 +132,15 @@ mpl.figure.prototype._init_canvas = function() { |
128 | 132 | this.canvas = canvas[0]; |
129 | 133 | this.context = canvas[0].getContext("2d"); |
130 | 134 |
|
| 135 | + var backingStore = this.context.backingStorePixelRatio || |
| 136 | + this.context.webkitBackingStorePixelRatio || |
| 137 | + this.context.mozBackingStorePixelRatio || |
| 138 | + this.context.msBackingStorePixelRatio || |
| 139 | + this.context.oBackingStorePixelRatio || |
| 140 | + this.context.backingStorePixelRatio || 1; |
| 141 | + |
| 142 | + mpl.ratio = (window.devicePixelRatio || 1) / backingStore; |
| 143 | + |
131 | 144 | var rubberband = $('<canvas/>'); |
132 | 145 | rubberband.attr('style', "position: absolute; left: 0; top: 0; z-index: 1;") |
133 | 146 |
|
@@ -184,8 +197,9 @@ mpl.figure.prototype._init_canvas = function() { |
184 | 197 | canvas_div.css('width', width) |
185 | 198 | canvas_div.css('height', height) |
186 | 199 |
|
187 | | - canvas.attr('width', width); |
188 | | - canvas.attr('height', height); |
| 200 | + canvas.attr('width', width * mpl.ratio); |
| 201 | + canvas.attr('height', height * mpl.ratio); |
| 202 | + canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;'); |
189 | 203 |
|
190 | 204 | rubberband.attr('width', width); |
191 | 205 | rubberband.attr('height', height); |
@@ -318,10 +332,10 @@ mpl.figure.prototype.handle_resize = function(fig, msg) { |
318 | 332 | } |
319 | 333 |
|
320 | 334 | mpl.figure.prototype.handle_rubberband = function(fig, msg) { |
321 | | - var x0 = msg['x0']; |
322 | | - var y0 = fig.canvas.height - msg['y0']; |
323 | | - var x1 = msg['x1']; |
324 | | - 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; |
325 | 339 | x0 = Math.floor(x0) + 0.5; |
326 | 340 | y0 = Math.floor(y0) + 0.5; |
327 | 341 | x1 = Math.floor(x1) + 0.5; |
@@ -477,8 +491,8 @@ mpl.figure.prototype.mouse_event = function(event, name) { |
477 | 491 | this.canvas_div.focus(); |
478 | 492 | } |
479 | 493 |
|
480 | | - var x = canvas_pos.x; |
481 | | - var y = canvas_pos.y; |
| 494 | + var x = canvas_pos.x * mpl.ratio; |
| 495 | + var y = canvas_pos.y * mpl.ratio; |
482 | 496 |
|
483 | 497 | this.send_message(name, {x: x, y: y, button: event.button, |
484 | 498 | step: event.step, |
|
0 commit comments