mobile.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. <script src="../demo/hammer.js"></script>
  7. </head>
  8. <body>
  9. <div id="mobile-div" style="width: 602px; height: 420px; border:1px solid black; ">
  10. <svg id="mobile-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">
  11. <defs>
  12. <linearGradient id="linear-gradient" x1="0%" y1="0%" x2="0%" y2="100%">
  13. <stop offset="0%" style="stop-color:rgb(56,121,217);stop-opacity:1" />
  14. <stop offset="100%" style="stop-color:rgb(138,192,7);stop-opacity:1" />
  15. </linearGradient>
  16. </defs>
  17. <g fill="none">
  18. <g stroke="#000" fill="#FFF">
  19. <rect x="5" y="5" width="240" height="240" fill="url(#linear-gradient)"/>
  20. <path d="M 5 5 L 245 245 Z"/>
  21. </g>
  22. </g>
  23. </svg>
  24. </div>
  25. <script>
  26. // Don't use window.onLoad like this in production, because it can only listen to one function.
  27. window.onload = function() {
  28. var eventsHandler;
  29. eventsHandler = {
  30. haltEventListeners: ['touchstart', 'touchend', 'touchmove', 'touchleave', 'touchcancel']
  31. , init: function(options) {
  32. var instance = options.instance
  33. , initialScale = 1
  34. , pannedX = 0
  35. , pannedY = 0
  36. // Init Hammer
  37. // Listen only for pointer and touch events
  38. this.hammer = Hammer(options.svgElement, {
  39. inputClass: Hammer.SUPPORT_POINTER_EVENTS ? Hammer.PointerEventInput : Hammer.TouchInput
  40. })
  41. // Enable pinch
  42. this.hammer.get('pinch').set({enable: true})
  43. // Handle double tap
  44. this.hammer.on('doubletap', function(ev){
  45. instance.zoomIn()
  46. })
  47. // Handle pan
  48. this.hammer.on('panstart panmove', function(ev){
  49. // On pan start reset panned variables
  50. if (ev.type === 'panstart') {
  51. pannedX = 0
  52. pannedY = 0
  53. }
  54. // Pan only the difference
  55. instance.panBy({x: ev.deltaX - pannedX, y: ev.deltaY - pannedY})
  56. pannedX = ev.deltaX
  57. pannedY = ev.deltaY
  58. })
  59. // Handle pinch
  60. this.hammer.on('pinchstart pinchmove', function(ev){
  61. // On pinch start remember initial zoom
  62. if (ev.type === 'pinchstart') {
  63. initialScale = instance.getZoom()
  64. instance.zoomAtPoint(initialScale * ev.scale, {x: ev.center.x, y: ev.center.y})
  65. }
  66. instance.zoomAtPoint(initialScale * ev.scale, {x: ev.center.x, y: ev.center.y})
  67. })
  68. // Prevent moving the page on some devices when panning over SVG
  69. options.svgElement.addEventListener('touchmove', function(e){ e.preventDefault(); });
  70. }
  71. , destroy: function(){
  72. this.hammer.destroy()
  73. }
  74. }
  75. // Expose to window namespace for testing purposes
  76. window.panZoom = svgPanZoom('#mobile-svg', {
  77. zoomEnabled: true
  78. , controlIconsEnabled: true
  79. , fit: 1
  80. , center: 1
  81. , customEventsHandler: eventsHandler
  82. });
  83. };
  84. </script>
  85. </body>
  86. </html>