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

Skip to content

Commit df390cc

Browse files
committed
nbagg: Improve resizing code.
Directly resize the canvas when the container div is resized, instead of waiting for the message round-trip to Python. Set a minimum size to prevent positive-finite errors in the console.
1 parent 7b82d4c commit df390cc

File tree

1 file changed

+26
-24
lines changed
  • lib/matplotlib/backends/web_backend/js

1 file changed

+26
-24
lines changed

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ mpl.figure.prototype._init_canvas = function () {
115115
'border: 1px solid #ddd;' +
116116
'box-sizing: content-box;' +
117117
'clear: both;' +
118+
'min-height: 1px;' +
119+
'min-width: 1px;' +
118120
'outline: 0;' +
119121
'overflow: hidden;' +
120122
'position: relative;' +
@@ -168,16 +170,32 @@ mpl.figure.prototype._init_canvas = function () {
168170
var nentries = entries.length;
169171
for (var i = 0; i < nentries; i++) {
170172
var entry = entries[i];
173+
var width, height;
171174
if (entry.contentBoxSize) {
172-
fig.request_resize(
173-
entry.contentBoxSize.inlineSize,
174-
entry.contentBoxSize.blockSize
175-
);
175+
width = entry.contentBoxSize.inlineSize;
176+
height = entry.contentBoxSize.blockSize;
176177
} else {
177-
fig.request_resize(
178-
entry.contentRect.width,
179-
entry.contentRect.height
180-
);
178+
width = entry.contentRect.width;
179+
height = entry.contentRect.height;
180+
}
181+
182+
// Keep the size of the canvas and rubber band canvas in sync with
183+
// the canvas container.
184+
canvas.setAttribute('width', width * mpl.ratio);
185+
canvas.setAttribute('height', height * mpl.ratio);
186+
canvas.setAttribute(
187+
'style',
188+
'width: ' + width + 'px; height: ' + height + 'px;'
189+
);
190+
191+
rubberband_canvas.setAttribute('width', width);
192+
rubberband_canvas.setAttribute('height', height);
193+
194+
// And update the size in Python. We ignore the initial 0/0 size
195+
// that occurs as the element is placed into the DOM, which should
196+
// otherwise not happen due to the minimum size styling.
197+
if (width != 0 && height != 0) {
198+
fig.request_resize(width, height);
181199
}
182200
}
183201
});
@@ -228,28 +246,12 @@ mpl.figure.prototype._init_canvas = function () {
228246
this.rubberband_context.strokeStyle = '#000000';
229247

230248
this._resize_canvas = function (width, height, forward) {
231-
// Keep the size of the canvas and rubber band canvas in sync with the
232-
// canvas container.
233249
if (forward) {
234250
canvas_div.style.width = width + 'px';
235251
canvas_div.style.height = height + 'px';
236252
}
237-
238-
canvas.setAttribute('width', width * mpl.ratio);
239-
canvas.setAttribute('height', height * mpl.ratio);
240-
canvas.setAttribute(
241-
'style',
242-
'width: ' + width + 'px; height: ' + height + 'px;'
243-
);
244-
245-
rubberband_canvas.setAttribute('width', width);
246-
rubberband_canvas.setAttribute('height', height);
247253
};
248254

249-
// Set the figure to an initial 600x600px, this will subsequently be updated
250-
// upon first draw.
251-
this._resize_canvas(600, 600, true);
252-
253255
// Disable right mouse context menu.
254256
this.rubberband_canvas.addEventListener('contextmenu', function (_e) {
255257
return false;

0 commit comments

Comments
 (0)