limit-pan.html 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">
  5. <script src="../dist/svg-pan-zoom.js"></script>
  6. </head>
  7. <body>
  8. <div id="limit-div" style="width: 602px; height: 420px; border:1px solid black; ">
  9. <svg id="limit-svg" xmlns="http://www.w3.org/2000/svg" style="display: inline; width: inherit; min-width: inherit; max-width: inherit; height: inherit; min-height: inherit; max-height: inherit;" version="1.1">
  10. <defs>
  11. <linearGradient id="linear-gradient" x1="0%" y1="0%" x2="0%" y2="100%">
  12. <stop offset="0%" style="stop-color:rgb(56,121,217);stop-opacity:1" />
  13. <stop offset="100%" style="stop-color:rgb(138,192,7);stop-opacity:1" />
  14. </linearGradient>
  15. </defs>
  16. <g fill="none">
  17. <g stroke="#000" fill="#FFF">
  18. <rect x="5" y="5" width="240" height="240" fill="url(#linear-gradient)"/>
  19. <path d="M 5 5 L 245 245 Z"/>
  20. </g>
  21. </g>
  22. </svg>
  23. </div>
  24. <script>
  25. // Don't use window.onLoad like this in production, because it can only listen to one function.
  26. window.onload = function() {
  27. var beforePan
  28. beforePan = function(oldPan, newPan){
  29. var stopHorizontal = false
  30. , stopVertical = false
  31. , gutterWidth = 100
  32. , gutterHeight = 100
  33. // Computed variables
  34. , sizes = this.getSizes()
  35. , leftLimit = -((sizes.viewBox.x + sizes.viewBox.width) * sizes.realZoom) + gutterWidth
  36. , rightLimit = sizes.width - gutterWidth - (sizes.viewBox.x * sizes.realZoom)
  37. , topLimit = -((sizes.viewBox.y + sizes.viewBox.height) * sizes.realZoom) + gutterHeight
  38. , bottomLimit = sizes.height - gutterHeight - (sizes.viewBox.y * sizes.realZoom)
  39. customPan = {}
  40. customPan.x = Math.max(leftLimit, Math.min(rightLimit, newPan.x))
  41. customPan.y = Math.max(topLimit, Math.min(bottomLimit, newPan.y))
  42. return customPan
  43. }
  44. // Expose to window namespace for testing purposes
  45. window.panZoom = svgPanZoom('#limit-svg', {
  46. zoomEnabled: true
  47. , controlIconsEnabled: true
  48. , fit: 1
  49. , center: 1
  50. , beforePan: beforePan
  51. });
  52. // panZoom.setBeforePan(beforePan)
  53. };
  54. </script>
  55. </body>
  56. </html>