Skip to content

Commit 72fa42d

Browse files
committed
improved zooming feature
1 parent 454e1b1 commit 72fa42d

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

js/jquery.mapael.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -419,35 +419,39 @@
419419
* @param options
420420
*/
421421
$.fn.mapael.initZoom = function($container, paper, mapWidth, mapHeight, options) {
422-
var $zoomIn = $("<div>").addClass(options.zoomInCssClass).html("+")
422+
var $parentContainer = $container.parent()
423+
, $zoomIn = $("<div>").addClass(options.zoomInCssClass).html("+")
423424
, $zoomOut = $("<div>").addClass(options.zoomOutCssClass).html("&#x2212;")
424-
, currentLevel = 0
425-
, vbCenterX = mapWidth / 2
426-
, vbCenterY = mapHeight / 2
427425
, mousedown = false
428426
, previousX = 0
429-
, previousY = 0
430-
, setZoom = function(e) {
431-
// Update zoom level
432-
currentLevel = Math.min(Math.max(currentLevel + e.data.offset, 0), options.maxLevel);
433-
if (currentLevel == 0) {
434-
vbCenterX = mapWidth / 2
435-
vbCenterY = mapHeight / 2
436-
paper.setViewBox(0, 0, mapWidth, mapHeight);
437-
} else {
438-
paper.setViewBox(
439-
Math.min(Math.max(0, vbCenterX - (mapWidth / (1 + currentLevel * options.step))/2), (mapWidth - (mapWidth / (1 + currentLevel * options.step)))),
440-
Math.min(Math.max(0, vbCenterY - (mapHeight / (1 + currentLevel * options.step))/2), (mapHeight - (mapHeight / (1 + currentLevel * options.step)))),
441-
mapWidth / (1 +currentLevel * options.step),
442-
mapHeight / (1 +currentLevel * options.step)
443-
);
444-
}
445-
};
446-
427+
, previousY = 0;
428+
429+
// Zoom
430+
$parentContainer.data("zoomLevel", 0);
447431
$container.append($zoomIn).append($zoomOut);
448432

449-
$zoomIn.on("click", null, {offset : 1} , setZoom);
450-
$zoomOut.on("click", null, {offset : -1}, setZoom);
433+
$parentContainer.on("zoom", function(e, level, x, y) {
434+
var currentLevel = Math.min(Math.max(level, 0), options.maxLevel);
435+
$parentContainer.data("zoomLevel", currentLevel);
436+
437+
(typeof x == "undefined") && (x = (paper._viewBox[0] + paper._viewBox[2] / 2));
438+
(typeof y == "undefined") && (y = (paper._viewBox[1] + paper._viewBox[3] / 2));
439+
440+
// Update zoom level of the map
441+
if (currentLevel == 0) {
442+
paper.setViewBox(0, 0, mapWidth, mapHeight);
443+
} else {
444+
paper.setViewBox(
445+
Math.min(Math.max(0, x - (mapWidth / (1 + currentLevel * options.step))/2), (mapWidth - (mapWidth / (1 + currentLevel * options.step)))),
446+
Math.min(Math.max(0, y - (mapHeight / (1 + currentLevel * options.step))/2), (mapHeight - (mapHeight / (1 + currentLevel * options.step)))),
447+
mapWidth / (1 + currentLevel * options.step),
448+
mapHeight / (1 + currentLevel * options.step)
449+
);
450+
}
451+
});
452+
453+
$zoomIn.on("click", function() {$parentContainer.trigger("zoom", $parentContainer.data("zoomLevel") + 1);});
454+
$zoomOut.on("click", function() {$parentContainer.trigger("zoom", $parentContainer.data("zoomLevel") - 1);});
451455

452456
// Panning
453457
$('body').on("mouseup", function(e) {
@@ -461,27 +465,25 @@
461465
previousY = e.pageY;
462466
return false;
463467
}).on("mousemove", function(e) {
468+
var currentLevel = $parentContainer.data("zoomLevel");
464469
if (mousedown && currentLevel != 0) {
465470
var offsetX = (previousX - e.pageX) / (1 + (currentLevel * options.step)) * (mapWidth / paper.width)
466471
, offsetY = (previousY - e.pageY) / (1 + (currentLevel * options.step)) * (mapHeight / paper.height);
467472

468473
if (Math.abs(offsetX) > 5 || Math.abs(offsetY) > 5) {
469-
var viewBoxX = Math.min(Math.max(0, paper._viewBox[0] + offsetX), (mapWidth - paper._viewBox[2]))
470-
, viewBoxY = Math.min(Math.max(0, paper._viewBox[1] + offsetY), (mapHeight - paper._viewBox[3]));
471-
472-
vbCenterX = viewBoxX + paper._viewBox[2] / 2;
473-
vbCenterY = viewBoxY + paper._viewBox[3] / 2;
474-
475-
paper.setViewBox(viewBoxX, viewBoxY, paper._viewBox[2], paper._viewBox[3]);
474+
paper.setViewBox(
475+
Math.min(Math.max(0, paper._viewBox[0] + offsetX), (mapWidth - paper._viewBox[2])),
476+
Math.min(Math.max(0, paper._viewBox[1] + offsetY), (mapHeight - paper._viewBox[3])),
477+
paper._viewBox[2],
478+
paper._viewBox[3]
479+
);
476480

477481
previousX = e.pageX;
478482
previousY = e.pageY;
479483
$.fn.mapael.panning = true;
480484
}
481485
}
482486
return false;
483-
}).on("scroll", function(e) {
484-
console.log("test");
485487
});
486488
}
487489

0 commit comments

Comments
 (0)