custom-event-handlers.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. <style>
  7. svg{
  8. opacity: 0.5;
  9. }
  10. svg.active{
  11. opacity: 1;
  12. }
  13. svg.active.hovered{
  14. border: 3px solid blue;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="limit-div" style="width: 602px; height: 420px; border:1px solid black; ">
  20. <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">
  21. <defs>
  22. <linearGradient id="linear-gradient" x1="0%" y1="0%" x2="0%" y2="100%">
  23. <stop offset="0%" style="stop-color:rgb(56,121,217);stop-opacity:1" />
  24. <stop offset="100%" style="stop-color:rgb(138,192,7);stop-opacity:1" />
  25. </linearGradient>
  26. </defs>
  27. <g fill="none">
  28. <g stroke="#000" fill="#FFF">
  29. <rect x="5" y="5" width="240" height="240" fill="url(#linear-gradient)"/>
  30. <path d="M 5 5 L 245 245 Z"/>
  31. </g>
  32. </g>
  33. </svg>
  34. </div>
  35. <script>
  36. // Don't use window.onLoad like this in production, because it can only listen to one function.
  37. window.onload = function() {
  38. var svgActive = false
  39. , svgHovered = false
  40. // Expose to window namespace for testing purposes
  41. window.panZoom = svgPanZoom('#limit-svg', {
  42. zoomEnabled: true
  43. , controlIconsEnabled: true
  44. , zoomEnabled: false
  45. , fit: 1
  46. , center: 1
  47. , customEventsHandler: {
  48. init: function(options){
  49. function updateSvgClassName(){
  50. options.svgElement.setAttribute('class', '' + (svgActive ? 'active':'') + (svgHovered ? ' hovered':''))
  51. }
  52. this.listeners = {
  53. click: function(){
  54. if (svgActive) {
  55. options.instance.disableZoom()
  56. svgActive = false
  57. } else {
  58. options.instance.enableZoom()
  59. svgActive = true
  60. }
  61. updateSvgClassName()
  62. },
  63. mouseenter: function(){
  64. svgHovered = true
  65. updateSvgClassName()
  66. },
  67. mouseleave: function(){
  68. svgActive = false
  69. svgHovered = false
  70. options.instance.disableZoom()
  71. updateSvgClassName()
  72. }
  73. }
  74. this.listeners.mousemove = this.listeners.mouseenter
  75. for (var eventName in this.listeners){
  76. options.svgElement.addEventListener(eventName, this.listeners[eventName])
  77. }
  78. }
  79. , destroy: function(options){
  80. for (var eventName in this.listeners){
  81. options.svgElement.removeEventListener(eventName, this.listeners[eventName])
  82. }
  83. }
  84. }
  85. });
  86. };
  87. </script>
  88. </body>
  89. </html>