Skip to content

Commit b590dfa

Browse files
committed
able to zoom array2d&1d
1 parent 3ce85c4 commit b590dfa

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

css/stylesheet.css

+1-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ section {
136136
bottom: 50%;
137137
left: 0;
138138
right: 0;
139-
overflow: scroll;
139+
overflow: hidden;
140140
}
141141

142142
.tab_container {
@@ -245,7 +245,6 @@ pre {
245245
}
246246

247247
.mtbl-cell {
248-
padding: 3px 6px;
249248
display: table-cell;
250249
vertical-align: middle;
251250
text-align: center;

js/module/array1d.js

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Array1DTracer.prototype.createRandomData = function (N, min, max) {
1212

1313
// Override
1414
Array1DTracer.prototype.setData = function (D) {
15-
this.D = D;
1615
return Array2DTracer.prototype.setData.call(this, [D]);
1716
};
1817

js/module/array2d.js

+57-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var $table = null;
33
function Array2DTracer(module) {
44
if (Tracer.call(this, module || Array2DTracer)) {
55
initTable();
6+
initNavigator(this);
67
return true;
78
}
89
return false;
@@ -45,12 +46,18 @@ Array2DTracer.prototype.setData = function (D) {
4546
this.D = D;
4647
if (Tracer.prototype.setData.call(this, arguments)) return true;
4748

49+
this.viewX = this.viewY = 0;
50+
this.paddingH = 6;
51+
this.paddingV = 3;
52+
this.fontSize = 16;
4853
$table.empty();
4954
for (var i = 0; i < D.length; i++) {
5055
var $row = $('<div class="mtbl-row">');
5156
$table.append($row);
5257
for (var j = 0; j < D[i].length; j++) {
53-
var $cell = $('<div class="mtbl-cell">').text(D[i][j]);
58+
var $cell = $('<div class="mtbl-cell">')
59+
.css(this.getCellCss())
60+
.text(D[i][j]);
5461
$row.append($cell);
5562
}
5663
}
@@ -158,23 +165,22 @@ Array2DTracer.prototype.processStep = function (step, options) {
158165
}
159166
};
160167

168+
Array2DTracer.prototype.getCellCss = function () {
169+
return {
170+
padding: this.paddingV + 'px ' + this.paddingH + 'px',
171+
'font-size': this.fontSize
172+
};
173+
};
174+
161175
// Override
162176
Array2DTracer.prototype.refresh = function () {
163177
Tracer.prototype.refresh.call(this);
164178

165179
var $parent = $table.parent();
166-
var top = $parent.height() / 2 - $table.height() / 2;
167-
var left = $parent.width() / 2 - $table.width() / 2;
168-
if (top < 0) {
169-
$table.css('margin-top', 0);
170-
$parent.scrollTop(-top);
171-
}
172-
else $table.css('margin-top', top);
173-
if (left < 0) {
174-
$table.css('margin-left', 0);
175-
$parent.scrollLeft(-left);
176-
}
177-
else $table.css('margin-left', left);
180+
var top = $parent.height() / 2 - $table.height() / 2 + this.viewY;
181+
var left = $parent.width() / 2 - $table.width() / 2 + this.viewX;
182+
$table.css('margin-top', top);
183+
$table.css('margin-left', left);
178184
};
179185

180186
// Override
@@ -198,6 +204,44 @@ var initTable = function () {
198204
$('.module_container').append($table);
199205
};
200206

207+
var initNavigator = function (tracer) {
208+
var x, y, dragging = false;
209+
var $parent = $table.parent();
210+
$parent.mousedown(function (e) {
211+
x = e.pageX;
212+
y = e.pageY;
213+
dragging = true;
214+
});
215+
$parent.mousemove(function (e) {
216+
if (dragging) {
217+
tracer.viewX += e.pageX - x;
218+
tracer.viewY += e.pageY - y;
219+
x = e.pageX;
220+
y = e.pageY;
221+
tracer.refresh();
222+
}
223+
});
224+
$(document).mouseup(function (e) {
225+
dragging = false;
226+
});
227+
228+
$parent.bind('DOMMouseScroll mousewheel', function (e, delta) {
229+
e = e.originalEvent;
230+
var delta = (e.wheelDelta !== undefined && e.wheelDelta) ||
231+
(e.detail !== undefined && -e.detail);
232+
var weight = 1.01;
233+
var ratio = delta > 0 ? 1 / weight : weight;
234+
if (tracer.fontSize < 8 && ratio < 1) return false;
235+
if (tracer.fontSize > 36 && ratio > 1) return false;
236+
tracer.paddingV *= ratio;
237+
tracer.paddingH *= ratio;
238+
tracer.fontSize *= ratio;
239+
$('.mtbl-cell').css(tracer.getCellCss());
240+
tracer.refresh();
241+
e.preventDefault();
242+
});
243+
};
244+
201245
var paintColor = function (sx, sy, ex, ey, colorClass, addClass) {
202246
for (var i = sx; i <= ex; i++) {
203247
var $row = $table.find('.mtbl-row').eq(i);

0 commit comments

Comments
 (0)