From 05876b546e5e6a93a61823600480839271e82bff Mon Sep 17 00:00:00 2001 From: Jason Park Date: Fri, 20 May 2016 04:28:04 -0500 Subject: [PATCH] able to zoom array2d&1d --- css/stylesheet.css | 3 +- js/module/array1d.js | 1 - js/module/array2d.js | 70 ++++++++++++++++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/css/stylesheet.css b/css/stylesheet.css index a1e9683..43a4a19 100644 --- a/css/stylesheet.css +++ b/css/stylesheet.css @@ -136,7 +136,7 @@ section { bottom: 50%; left: 0; right: 0; - overflow: scroll; + overflow: hidden; } .tab_container { @@ -245,7 +245,6 @@ pre { } .mtbl-cell { - padding: 3px 6px; display: table-cell; vertical-align: middle; text-align: center; diff --git a/js/module/array1d.js b/js/module/array1d.js index ca87481..069334b 100644 --- a/js/module/array1d.js +++ b/js/module/array1d.js @@ -12,7 +12,6 @@ Array1DTracer.prototype.createRandomData = function (N, min, max) { // Override Array1DTracer.prototype.setData = function (D) { - this.D = D; return Array2DTracer.prototype.setData.call(this, [D]); }; diff --git a/js/module/array2d.js b/js/module/array2d.js index 3939de0..36442ef 100644 --- a/js/module/array2d.js +++ b/js/module/array2d.js @@ -3,6 +3,7 @@ var $table = null; function Array2DTracer(module) { if (Tracer.call(this, module || Array2DTracer)) { initTable(); + initNavigator(this); return true; } return false; @@ -45,12 +46,18 @@ Array2DTracer.prototype.setData = function (D) { this.D = D; if (Tracer.prototype.setData.call(this, arguments)) return true; + this.viewX = this.viewY = 0; + this.paddingH = 6; + this.paddingV = 3; + this.fontSize = 16; $table.empty(); for (var i = 0; i < D.length; i++) { var $row = $('
'); $table.append($row); for (var j = 0; j < D[i].length; j++) { - var $cell = $('
').text(D[i][j]); + var $cell = $('
') + .css(this.getCellCss()) + .text(D[i][j]); $row.append($cell); } } @@ -158,23 +165,22 @@ Array2DTracer.prototype.processStep = function (step, options) { } }; +Array2DTracer.prototype.getCellCss = function () { + return { + padding: this.paddingV + 'px ' + this.paddingH + 'px', + 'font-size': this.fontSize + }; +}; + // Override Array2DTracer.prototype.refresh = function () { Tracer.prototype.refresh.call(this); var $parent = $table.parent(); - var top = $parent.height() / 2 - $table.height() / 2; - var left = $parent.width() / 2 - $table.width() / 2; - if (top < 0) { - $table.css('margin-top', 0); - $parent.scrollTop(-top); - } - else $table.css('margin-top', top); - if (left < 0) { - $table.css('margin-left', 0); - $parent.scrollLeft(-left); - } - else $table.css('margin-left', left); + var top = $parent.height() / 2 - $table.height() / 2 + this.viewY; + var left = $parent.width() / 2 - $table.width() / 2 + this.viewX; + $table.css('margin-top', top); + $table.css('margin-left', left); }; // Override @@ -198,6 +204,44 @@ var initTable = function () { $('.module_container').append($table); }; +var initNavigator = function (tracer) { + var x, y, dragging = false; + var $parent = $table.parent(); + $parent.mousedown(function (e) { + x = e.pageX; + y = e.pageY; + dragging = true; + }); + $parent.mousemove(function (e) { + if (dragging) { + tracer.viewX += e.pageX - x; + tracer.viewY += e.pageY - y; + x = e.pageX; + y = e.pageY; + tracer.refresh(); + } + }); + $(document).mouseup(function (e) { + dragging = false; + }); + + $parent.bind('DOMMouseScroll mousewheel', function (e, delta) { + e = e.originalEvent; + var delta = (e.wheelDelta !== undefined && e.wheelDelta) || + (e.detail !== undefined && -e.detail); + var weight = 1.01; + var ratio = delta > 0 ? 1 / weight : weight; + if (tracer.fontSize < 8 && ratio < 1) return false; + if (tracer.fontSize > 36 && ratio > 1) return false; + tracer.paddingV *= ratio; + tracer.paddingH *= ratio; + tracer.fontSize *= ratio; + $('.mtbl-cell').css(tracer.getCellCss()); + tracer.refresh(); + e.preventDefault(); + }); +}; + var paintColor = function (sx, sy, ex, ey, colorClass, addClass) { for (var i = sx; i <= ex; i++) { var $row = $table.find('.mtbl-row').eq(i); -- GitLab