diff --git a/examples/user_interfaces/embedding_webagg_sgskip.py b/examples/user_interfaces/embedding_webagg_sgskip.py index 7984c0f557d9..74688d627640 100644 --- a/examples/user_interfaces/embedding_webagg_sgskip.py +++ b/examples/user_interfaces/embedding_webagg_sgskip.py @@ -71,7 +71,15 @@ def create_figure(): window.open('download.' + format, '_blank'); }; - $(document).ready( + function ready(fn) { + if (document.readyState != "loading") { + fn(); + } else { + document.addEventListener("DOMContentLoaded", fn); + } + } + + ready( function() { /* It is up to the application to provide a websocket that the figure will use to communicate to the server. This websocket object can @@ -89,7 +97,7 @@ def create_figure(): // A function called when a file type is selected for download ondownload, // The HTML element in which to place the figure - $('div#figure')); + document.getElementById("figure")); } ); diff --git a/lib/matplotlib/backends/web_backend/all_figures.html b/lib/matplotlib/backends/web_backend/all_figures.html index 41f48dc6d226..034a9be5bf6a 100644 --- a/lib/matplotlib/backends/web_backend/all_figures.html +++ b/lib/matplotlib/backends/web_backend/all_figures.html @@ -10,25 +10,33 @@ MPL | WebAgg current figures diff --git a/lib/matplotlib/backends/web_backend/ipython_inline_figure.html b/lib/matplotlib/backends/web_backend/ipython_inline_figure.html index eed5af0f4346..9cc6aa9020e2 100644 --- a/lib/matplotlib/backends/web_backend/ipython_inline_figure.html +++ b/lib/matplotlib/backends/web_backend/ipython_inline_figure.html @@ -12,7 +12,7 @@ function init_figure{{ fig_id }}(e) { $('div.output').off('resize'); - var output_div = $(e.target).find('div.output_subarea'); + var output_div = e.target.querySelector('div.output_subarea'); var websocket_type = mpl.get_websocket_type(); var websocket = new websocket_type( "ws://" + window.location.hostname + ":{{ port }}{{ prefix}}/" + diff --git a/lib/matplotlib/backends/web_backend/js/mpl.js b/lib/matplotlib/backends/web_backend/js/mpl.js index 4986680f5f2b..ae8e5acf0007 100644 --- a/lib/matplotlib/backends/web_backend/js/mpl.js +++ b/lib/matplotlib/backends/web_backend/js/mpl.js @@ -45,11 +45,11 @@ mpl.figure = function (figure_id, websocket, ondownload, parent_element) { this.image_mode = 'full'; - this.root = $('
'); + this.root = document.createElement('div'); this._root_extra_style(this.root); - this.root.attr('style', 'display: inline-block'); + this.root.setAttribute('style', 'display: inline-block'); - $(parent_element).append(this.root); + parent_element.appendChild(this.root); this._init_header(this); this._init_canvas(this); @@ -88,17 +88,18 @@ mpl.figure = function (figure_id, websocket, ondownload, parent_element) { }; mpl.figure.prototype._init_header = function () { - var titlebar = $( - '
' - ); - var titletext = $( - '
' + var titlebar = document.createElement('div'); + titlebar.classList = + 'ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix'; + var titletext = document.createElement('div'); + titletext.classList = 'ui-dialog-title'; + titletext.setAttribute( + 'style', + 'width: 100%; text-align: center; padding: 3px;' ); - titlebar.append(titletext); - this.root.append(titlebar); - this.header = titletext[0]; + titlebar.appendChild(titletext); + this.root.appendChild(titlebar); + this.header = titletext; }; mpl.figure.prototype._canvas_extra_style = function (_canvas_div) {}; @@ -108,26 +109,35 @@ mpl.figure.prototype._root_extra_style = function (_canvas_div) {}; mpl.figure.prototype._init_canvas = function () { var fig = this; - var canvas_div = $('
'); - - canvas_div.attr('style', 'position: relative; clear: both; outline: 0'); + var canvas_div = (this.canvas_div = document.createElement('div')); + canvas_div.setAttribute( + 'style', + 'position: relative; clear: both; outline: 0' + ); - function canvas_keyboard_event(event) { - return fig.key_event(event, event['data']); + function on_keyboard_event_closure(name) { + return function (event) { + return fig.key_event(event, name); + }; } - canvas_div.keydown('key_press', canvas_keyboard_event); - canvas_div.keyup('key_release', canvas_keyboard_event); - this.canvas_div = canvas_div; + canvas_div.addEventListener( + 'keydown', + on_keyboard_event_closure('key_press') + ); + canvas_div.addEventListener( + 'keyup', + on_keyboard_event_closure('key_release') + ); + this._canvas_extra_style(canvas_div); - this.root.append(canvas_div); + this.root.appendChild(canvas_div); - var canvas = $(''); - canvas.addClass('mpl-canvas'); - canvas.attr('style', 'left: 0; top: 0; z-index: 0; outline: 0'); + var canvas = (this.canvas = document.createElement('canvas')); + canvas.classList.add('mpl-canvas'); + canvas.setAttribute('style', 'left: 0; top: 0; z-index: 0; outline: 0'); - this.canvas = canvas[0]; - this.context = canvas[0].getContext('2d'); + this.context = canvas.getContext('2d'); var backingStore = this.context.backingStorePixelRatio || @@ -140,15 +150,17 @@ mpl.figure.prototype._init_canvas = function () { mpl.ratio = (window.devicePixelRatio || 1) / backingStore; - var rubberband = $(''); - rubberband.attr( + var rubberband_canvas = (this.rubberband_canvas = document.createElement( + 'canvas' + )); + rubberband_canvas.setAttribute( 'style', 'position: absolute; left: 0; top: 0; z-index: 1;' ); var pass_mouse_events = true; - canvas_div.resizable({ + $(canvas_div).resizable({ start: function (_event, _ui) { pass_mouse_events = false; }, @@ -161,54 +173,67 @@ mpl.figure.prototype._init_canvas = function () { }, }); - function mouse_event_fn(event) { - if (pass_mouse_events) { - return fig.mouse_event(event, event['data']); - } + function on_mouse_event_closure(name) { + return function (event) { + if (pass_mouse_events) { + return fig.mouse_event(event, name); + } + }; } - rubberband.mousedown('button_press', mouse_event_fn); - rubberband.mouseup('button_release', mouse_event_fn); + rubberband_canvas.addEventListener( + 'mousedown', + on_mouse_event_closure('button_press') + ); + rubberband_canvas.addEventListener( + 'mouseup', + on_mouse_event_closure('button_release') + ); // Throttle sequential mouse events to 1 every 20ms. - rubberband.mousemove('motion_notify', mouse_event_fn); + rubberband_canvas.addEventListener( + 'mousemove', + on_mouse_event_closure('motion_notify') + ); - rubberband.mouseenter('figure_enter', mouse_event_fn); - rubberband.mouseleave('figure_leave', mouse_event_fn); + rubberband_canvas.addEventListener( + 'mouseenter', + on_mouse_event_closure('figure_enter') + ); + rubberband_canvas.addEventListener( + 'mouseleave', + on_mouse_event_closure('figure_leave') + ); - canvas_div.on('wheel', function (event) { - event = event.originalEvent; - event['data'] = 'scroll'; + canvas_div.addEventListener('wheel', function (event) { if (event.deltaY < 0) { event.step = 1; } else { event.step = -1; } - mouse_event_fn(event); + on_mouse_event_closure('scroll')(event); }); - canvas_div.append(canvas); - canvas_div.append(rubberband); + canvas_div.appendChild(canvas); + canvas_div.appendChild(rubberband_canvas); - this.rubberband = rubberband; - this.rubberband_canvas = rubberband[0]; - this.rubberband_context = rubberband[0].getContext('2d'); + this.rubberband_context = rubberband_canvas.getContext('2d'); this.rubberband_context.strokeStyle = '#000000'; this._resize_canvas = function (width, height) { // Keep the size of the canvas, canvas container, and rubber band // canvas in synch. - canvas_div.css('width', width); - canvas_div.css('height', height); + canvas_div.style.width = width; + canvas_div.style.height = height; - canvas.attr('width', width * mpl.ratio); - canvas.attr('height', height * mpl.ratio); - canvas.attr( + canvas.setAttribute('width', width * mpl.ratio); + canvas.setAttribute('height', height * mpl.ratio); + canvas.setAttribute( 'style', 'width: ' + width + 'px; height: ' + height + 'px;' ); - rubberband.attr('width', width); - rubberband.attr('height', height); + rubberband_canvas.setAttribute('width', width); + rubberband_canvas.setAttribute('height', height); }; // Set the figure to an initial 600x600px, this will subsequently be updated @@ -216,7 +241,7 @@ mpl.figure.prototype._init_canvas = function () { this._resize_canvas(600, 600); // Disable right mouse context menu. - $(this.rubberband_canvas).bind('contextmenu', function (_e) { + this.rubberband_canvas.addEventListener('contextmenu', function (_e) { return false; }); @@ -231,16 +256,20 @@ mpl.figure.prototype._init_canvas = function () { mpl.figure.prototype._init_toolbar = function () { var fig = this; - var nav_element = $('
'); - nav_element.attr('style', 'width: 100%'); - this.root.append(nav_element); + var toolbar = document.createElement('div'); + toolbar.setAttribute('style', 'width: 100%'); + this.root.appendChild(toolbar); - // Define a callback function for later on. - function toolbar_event(event) { - return fig.toolbar_button_onclick(event['data']); + function on_click_closure(name) { + return function (_event) { + return fig.toolbar_button_onclick(name); + }; } - function toolbar_mouse_event(event) { - return fig.toolbar_button_onmouseover(event['data']); + + function on_mouseover_closure(tooltip) { + return function (_event) { + return fig.toolbar_button_onmouseover(tooltip); + }; } for (var toolbar_ind in mpl.toolbar_items) { @@ -253,60 +282,48 @@ mpl.figure.prototype._init_toolbar = function () { // put a spacer in here. continue; } - var button = $('