@@ -46,8 +46,8 @@ mpl.figure = function (figure_id, websocket, ondownload, parent_element) {
4646 this . image_mode = 'full' ;
4747
4848 this . root = document . createElement ( 'div' ) ;
49- this . _root_extra_style ( this . root ) ;
5049 this . root . setAttribute ( 'style' , 'display: inline-block' ) ;
50+ this . _root_extra_style ( this . root ) ;
5151
5252 parent_element . appendChild ( this . root ) ;
5353
@@ -112,7 +112,15 @@ mpl.figure.prototype._init_canvas = function () {
112112 var canvas_div = ( this . canvas_div = document . createElement ( 'div' ) ) ;
113113 canvas_div . setAttribute (
114114 'style' ,
115- 'position: relative; clear: both; outline: 0'
115+ 'border: 1px solid #ddd;' +
116+ 'box-sizing: content-box;' +
117+ 'clear: both;' +
118+ 'min-height: 1px;' +
119+ 'min-width: 1px;' +
120+ 'outline: 0;' +
121+ 'overflow: hidden;' +
122+ 'position: relative;' +
123+ 'resize: both;'
116124 ) ;
117125
118126 function on_keyboard_event_closure ( name ) {
@@ -135,7 +143,7 @@ mpl.figure.prototype._init_canvas = function () {
135143
136144 var canvas = ( this . canvas = document . createElement ( 'canvas' ) ) ;
137145 canvas . classList . add ( 'mpl-canvas' ) ;
138- canvas . setAttribute ( 'style' , 'left: 0; top: 0; z-index: 0; outline: 0 ' ) ;
146+ canvas . setAttribute ( 'style' , 'box-sizing: content-box; ' ) ;
139147
140148 this . context = canvas . getContext ( '2d' ) ;
141149
@@ -155,29 +163,47 @@ mpl.figure.prototype._init_canvas = function () {
155163 ) ) ;
156164 rubberband_canvas . setAttribute (
157165 'style' ,
158- 'position: absolute; left: 0; top: 0; z-index: 1;'
166+ 'box-sizing: content-box; position: absolute; left: 0; top: 0; z-index: 1;'
159167 ) ;
160168
161- var pass_mouse_events = true ;
162-
163- $ ( canvas_div ) . resizable ( {
164- start : function ( _event , _ui ) {
165- pass_mouse_events = false ;
166- } ,
167- resize : function ( _event , ui ) {
168- fig . request_resize ( ui . size . width , ui . size . height ) ;
169- } ,
170- stop : function ( _event , ui ) {
171- pass_mouse_events = true ;
172- fig . request_resize ( ui . size . width , ui . size . height ) ;
173- } ,
169+ var resizeObserver = new ResizeObserver ( function ( entries ) {
170+ var nentries = entries . length ;
171+ for ( var i = 0 ; i < nentries ; i ++ ) {
172+ var entry = entries [ i ] ;
173+ var width , height ;
174+ if ( entry . contentBoxSize ) {
175+ width = entry . contentBoxSize . inlineSize ;
176+ height = entry . contentBoxSize . blockSize ;
177+ } else {
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 ) ;
199+ }
200+ }
174201 } ) ;
202+ resizeObserver . observe ( canvas_div ) ;
175203
176204 function on_mouse_event_closure ( name ) {
177205 return function ( event ) {
178- if ( pass_mouse_events ) {
179- return fig . mouse_event ( event , name ) ;
180- }
206+ return fig . mouse_event ( event , name ) ;
181207 } ;
182208 }
183209
@@ -219,27 +245,13 @@ mpl.figure.prototype._init_canvas = function () {
219245 this . rubberband_context = rubberband_canvas . getContext ( '2d' ) ;
220246 this . rubberband_context . strokeStyle = '#000000' ;
221247
222- this . _resize_canvas = function ( width , height ) {
223- // Keep the size of the canvas, canvas container, and rubber band
224- // canvas in synch.
225- canvas_div . style . width = width ;
226- canvas_div . style . height = height ;
227-
228- canvas . setAttribute ( 'width' , width * mpl . ratio ) ;
229- canvas . setAttribute ( 'height' , height * mpl . ratio ) ;
230- canvas . setAttribute (
231- 'style' ,
232- 'width: ' + width + 'px; height: ' + height + 'px;'
233- ) ;
234-
235- rubberband_canvas . setAttribute ( 'width' , width ) ;
236- rubberband_canvas . setAttribute ( 'height' , height ) ;
248+ this . _resize_canvas = function ( width , height , forward ) {
249+ if ( forward ) {
250+ canvas_div . style . width = width + 'px' ;
251+ canvas_div . style . height = height + 'px' ;
252+ }
237253 } ;
238254
239- // Set the figure to an initial 600x600px, this will subsequently be updated
240- // upon first draw.
241- this . _resize_canvas ( 600 , 600 ) ;
242-
243255 // Disable right mouse context menu.
244256 this . rubberband_canvas . addEventListener ( 'contextmenu' , function ( _e ) {
245257 return false ;
@@ -360,7 +372,7 @@ mpl.figure.prototype.handle_save = function (fig, _msg) {
360372mpl . figure . prototype . handle_resize = function ( fig , msg ) {
361373 var size = msg [ 'size' ] ;
362374 if ( size [ 0 ] !== fig . canvas . width || size [ 1 ] !== fig . canvas . height ) {
363- fig . _resize_canvas ( size [ 0 ] , size [ 1 ] ) ;
375+ fig . _resize_canvas ( size [ 0 ] , size [ 1 ] , msg [ 'forward' ] ) ;
364376 fig . send_message ( 'refresh' , { } ) ;
365377 }
366378} ;
0 commit comments