simple-animation.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="../dist/svg-pan-zoom.js"></script>
  5. </head>
  6. <body>
  7. <h1>Demo for svg-pan-zoom: In-line SVG</h1>
  8. <div id="container" style="width: 500px; height: 500px; border:1px solid black; ">
  9. <svg id="svg-id" 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. <button id="animate">Animate</button>
  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. // Expose to window namespase for testing purposes
  29. window.panZoomInstance = svgPanZoom('#svg-id', {
  30. zoomEnabled: true,
  31. controlIconsEnabled: true,
  32. fit: true,
  33. center: true,
  34. minZoom: 0.1
  35. });
  36. // Zoom out
  37. panZoomInstance.zoom(0.2);
  38. function customPanBy(amount){ // {x: 1, y: 2}
  39. var animationTime = 300 // ms
  40. , animationStepTime = 15 // one frame per 30 ms
  41. , animationSteps = animationTime / animationStepTime
  42. , animationStep = 0
  43. , intervalID = null
  44. , stepX = amount.x / animationSteps
  45. , stepY = amount.y / animationSteps
  46. intervalID = setInterval(function(){
  47. if (animationStep++ < animationSteps) {
  48. panZoomInstance.panBy({x: stepX, y: stepY})
  49. } else {
  50. // Cancel interval
  51. clearInterval(intervalID)
  52. }
  53. }, animationStepTime)
  54. }
  55. var button = document.getElementById("animate")
  56. button.addEventListener("click", function() {
  57. // Pan by any values from -80 to 80
  58. customPanBy({x: Math.round(Math.random() * 160 - 80), y: Math.round(Math.random() * 160 - 80)})
  59. });
  60. };
  61. </script>
  62. </body>
  63. </html>