|
419 | 419 | * @param options |
420 | 420 | */ |
421 | 421 | $.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("+") |
423 | 424 | , $zoomOut = $("<div>").addClass(options.zoomOutCssClass).html("−") |
424 | | - , currentLevel = 0 |
425 | | - , vbCenterX = mapWidth / 2 |
426 | | - , vbCenterY = mapHeight / 2 |
427 | 425 | , mousedown = false |
428 | 426 | , 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); |
447 | 431 | $container.append($zoomIn).append($zoomOut); |
448 | 432 |
|
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);}); |
451 | 455 |
|
452 | 456 | // Panning |
453 | 457 | $('body').on("mouseup", function(e) { |
|
461 | 465 | previousY = e.pageY; |
462 | 466 | return false; |
463 | 467 | }).on("mousemove", function(e) { |
| 468 | + var currentLevel = $parentContainer.data("zoomLevel"); |
464 | 469 | if (mousedown && currentLevel != 0) { |
465 | 470 | var offsetX = (previousX - e.pageX) / (1 + (currentLevel * options.step)) * (mapWidth / paper.width) |
466 | 471 | , offsetY = (previousY - e.pageY) / (1 + (currentLevel * options.step)) * (mapHeight / paper.height); |
467 | 472 |
|
468 | 473 | 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 | + ); |
476 | 480 |
|
477 | 481 | previousX = e.pageX; |
478 | 482 | previousY = e.pageY; |
479 | 483 | $.fn.mapael.panning = true; |
480 | 484 | } |
481 | 485 | } |
482 | 486 | return false; |
483 | | - }).on("scroll", function(e) { |
484 | | - console.log("test"); |
485 | 487 | }); |
486 | 488 | } |
487 | 489 |
|
|
0 commit comments