From 02583f420f21b1691233d0e5c4b2145e7e46d34e Mon Sep 17 00:00:00 2001 From: drasmuss Date: Sat, 6 Feb 2016 14:38:22 -0500 Subject: [PATCH] Cache jquery calls to reduce overhead --- nengo_gui/static/components/netgraph.js | 34 +++++++++++--------- nengo_gui/static/components/netgraph_item.js | 21 ++++++------ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/nengo_gui/static/components/netgraph.js b/nengo_gui/static/components/netgraph.js index f5ccab60..7075d838 100644 --- a/nengo_gui/static/components/netgraph.js +++ b/nengo_gui/static/components/netgraph.js @@ -125,8 +125,10 @@ Nengo.NetGraph = function(parent, args) { parent.appendChild(this.svg); this.parent = parent; - this.old_width = $(this.svg).width(); - this.old_height = $(this.svg).height(); + this.width = $(this.svg).width(); + this.height = $(this.svg).height(); + + this.tool_height = $(toolbar.toolbar).height(); /** three separate layers, so that expanded networks are at the back, * then connection lines, and then other items (nodes, ensembles, and @@ -203,9 +205,8 @@ Nengo.NetGraph = function(parent, args) { event.preventDefault(); self.menu.hide_any(); - var tool_h = $(toolbar.toolbar).height(); - var x = (event.clientX) / $(self.svg).width() - var y = (event.clientY - tool_h) / $(self.svg).height(); + var x = (event.clientX) / self.width + var y = (event.clientY - self.tool_height) / self.height; switch (event.deltaMode) { @@ -496,17 +497,17 @@ Nengo.NetGraph.prototype.on_resize = function(event) { for (var key in this.svg_objects) { var item = this.svg_objects[key]; if (item.depth == 1) { - var new_width = item.get_width()* - this.old_width/width / this.scale; - var new_height = item.get_height()* - this.old_height/height / this.scale; + var new_width = item.get_width() / this.scale; + var new_height = item.get_height() / this.scale; item.size = [new_width/(2*width), new_height/(2*height)]; } } } - this.old_width = width; - this.old_height = height; + this.width = width; + this.height = height; + this.mm_width = $(this.minimap).width(); + this.mm_height = $(this.minimap).height(); this.redraw(); }; @@ -514,13 +515,13 @@ Nengo.NetGraph.prototype.on_resize = function(event) { /** return the pixel width of the SVG times the current scale factor */ Nengo.NetGraph.prototype.get_scaled_width = function() { - return $(this.svg).width() * this.scale; + return this.width * this.scale; } /** return the pixel height of the SVG times the current scale factor */ Nengo.NetGraph.prototype.get_scaled_height = function() { - return $(this.svg).height() * this.scale; + return this.height * this.scale; } @@ -608,6 +609,9 @@ Nengo.NetGraph.prototype.create_minimap = function () { this.minimap.appendChild(this.g_conns_mini); this.minimap.appendChild(this.g_items_mini); + this.mm_width = $(this.minimap).width(); + this.mm_height = $(this.minimap).height(); + // default display minimap this.mm_display = true; this.toggleMiniMap(); @@ -690,8 +694,8 @@ Nengo.NetGraph.prototype.scaleMiniMap = function () { * main viewport and scale the viewbox to reflect that. */ Nengo.NetGraph.prototype.scaleMiniMapViewBox = function () { if (this.mm_display == true) { - var mm_w = $(this.minimap).width(); - var mm_h = $(this.minimap).height(); + var mm_w = this.mm_width + var mm_h = this.mm_height var w = mm_w * this.mm_scale; var h = mm_h * this.mm_scale; diff --git a/nengo_gui/static/components/netgraph_item.js b/nengo_gui/static/components/netgraph_item.js index 74cae1c2..95c7fc6f 100644 --- a/nengo_gui/static/components/netgraph_item.js +++ b/nengo_gui/static/components/netgraph_item.js @@ -188,9 +188,8 @@ Nengo.NetGraphItem = function(ng, info, minimap, mini_item) { var vertical_resize = event.edges.bottom || event.edges.top; var horizontal_resize = event.edges.left || event.edges.right; - var screen_offset = $('#netgraph').offset(); - var w = pos[0] - event.clientX + screen_offset.left; - var h = pos[1] - event.clientY + screen_offset.top; + var w = pos[0] - event.clientX + this.ng.offsetX; + var h = pos[1] - event.clientY + this.ng.offsetY; if (event.edges.right) { w *= -1; @@ -752,10 +751,10 @@ Nengo.NetGraphItem.prototype.get_width = function() { } if (this.minimap == false) { - var w = $(this.ng.svg).width(); + var w = this.ng.width; var screen_w = this.get_nested_width() * w * this.ng.scale; } else { - var w = $(this.ng.minimap).width(); + var w = this.ng.mm_width; var screen_w = this.get_nested_width() * w * this.ng.mm_scale; }; @@ -774,10 +773,10 @@ Nengo.NetGraphItem.prototype.get_height = function() { } if (this.minimap == false) { - var h = $(this.ng.svg).height(); + var h = this.ng.height; var screen_h = this.get_nested_height() * h * this.ng.scale; } else { - var h = $(this.ng.minimap).height(); + var h = this.ng.mm_height; var screen_h = this.get_nested_height() * h * this.ng.mm_scale; }; @@ -802,14 +801,14 @@ Nengo.NetGraphItem.prototype.get_screen_location = function() { if (this.minimap && !this.ng.mm_display) { return [1, 1]; } if (this.minimap == false) { - var w = $(this.ng.svg).width() * this.ng.scale; - var h = $(this.ng.svg).height() * this.ng.scale; + var w = this.ng.width * this.ng.scale; + var h = this.ng.height * this.ng.scale; var offsetX = this.ng.offsetX * w; var offsetY = this.ng.offsetY * h; } else { - var mm_w = $(this.ng.minimap).width(); - var mm_h = $(this.ng.minimap).height(); + var mm_w = this.ng.mm_width; + var mm_h = this.ng.mm_height; var w = mm_w * this.ng.mm_scale; var h = mm_h * this.ng.mm_scale;