You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.1 KiB

7 months ago
  1. // Initialize the zoom level and step
  2. let isDragging = false; // Track whether the user is dragging
  3. let startX, startY; // Starting position for dragging
  4. let currentX, currentY;
  5. let zoomLevel = 1;
  6. const zoomStep = 0.1;
  7. const minZoomLevel = 0.1;
  8. const maxZoomLevel = 3;
  9. // Function to update the zoom and apply it to the SVG element
  10. function updateZoom() {
  11. const svgElement = document.querySelector("#scrollableOutput svg");
  12. if (svgElement) {
  13. svgElement.style.transform = `scale(${zoomLevel})`;
  14. // Center the SVG element within the parent div
  15. const scrollableDiv = document.getElementById("scrollableOutput");
  16. if (scrollableDiv) {
  17. // Adjust the scroll position to center the SVG element
  18. scrollableDiv.scrollLeft =
  19. (svgElement.clientWidth - scrollableDiv.clientWidth) / 2;
  20. scrollableDiv.scrollTop =
  21. (svgElement.clientHeight - scrollableDiv.clientHeight) / 2;
  22. }
  23. }
  24. }
  25. // Event listener for the zoom in button
  26. document.getElementById("zoomInBtn").addEventListener("click", function () {
  27. zoomLevel += zoomStep;
  28. if (zoomLevel > maxZoomLevel) {
  29. zoomLevel = maxZoomLevel;
  30. }
  31. updateZoom();
  32. });
  33. // Event listener for the zoom out button
  34. document.getElementById("zoomOutBtn").addEventListener("click", function () {
  35. zoomLevel -= zoomStep;
  36. if (zoomLevel < minZoomLevel) {
  37. zoomLevel = minZoomLevel;
  38. }
  39. updateZoom();
  40. });
  41. function startDragging(event) {
  42. isDragging = true; // Start dragging
  43. startX = event.clientX;
  44. startY = event.clientY;
  45. }
  46. // Event listener for mousemove on the document (applies when dragging)
  47. function dragSVG(event) {
  48. if (isDragging) {
  49. // Calculate the movement
  50. const deltaX = (event.clientX - startX) * 2;
  51. const deltaY = (event.clientY - startY) * 2;
  52. // Retrieve the SVG element from the #scrollableOutput div
  53. const svgElement = document.querySelector("#scrollableOutput svg");
  54. if (svgElement) {
  55. // Get the current translation values from the transform attribute
  56. const transform = svgElement.style.transform || "translate(0, 0)";
  57. const translateMatch = transform.match(
  58. /translate\((-?\d+)px, (-?\d+)px\)/
  59. );
  60. let translateX = translateMatch ? parseInt(translateMatch[1]) : 0;
  61. let translateY = translateMatch ? parseInt(translateMatch[2]) : 0;
  62. translateX += deltaX;
  63. translateY += deltaY;
  64. // Update the SVG transform with the new translation
  65. svgElement.style.transform = `${transform.replace(
  66. /translate\(-?\d+px, -?\d+px\)/,
  67. ""
  68. )} translate(${translateX}px, ${translateY}px)`;
  69. // Update the starting positions for the next drag
  70. startX = event.clientX;
  71. startY = event.clientY;
  72. }
  73. }
  74. }
  75. // Function to center the SVG element within the #scrollableOutput div
  76. function centerSVG() {
  77. const scrollableDiv = document.getElementById("scrollableOutput");
  78. const svgElement = document.querySelector("#scrollableOutput svg");
  79. if (svgElement) {
  80. // Calculate the horizontal and vertical offset to center the SVG
  81. const divWidth = scrollableDiv.clientWidth;
  82. const divHeight = scrollableDiv.clientHeight;
  83. const svgWidth = svgElement.getBBox().width;
  84. const svgHeight = svgElement.getBBox().height;
  85. // Calculate the scroll positions to center the SVG
  86. const scrollX = (svgWidth - divWidth) / 2;
  87. const scrollY = (svgHeight - divHeight) / 2;
  88. // Set the scroll positions of the div to center the SVG
  89. scrollableDiv.scrollLeft = scrollX;
  90. scrollableDiv.scrollTop = scrollY;
  91. }
  92. }
  93. document.getElementById("centerSvgButton").addEventListener("click", centerSVG);
  94. // Event listener for mouseup on the document (ends dragging)
  95. function stopDragging() {
  96. isDragging = false;
  97. }
  98. // Event listeners to handle dragging
  99. document.addEventListener("mousedown", (event) => {
  100. // Only start dragging if the mousedown event occurs on the SVG element
  101. const svgElement = document.querySelector("#scrollableOutput svg");
  102. if (svgElement && svgElement.contains(event.target)) {
  103. startDragging(event);
  104. }
  105. });
  106. document.addEventListener("mousemove", dragSVG);
  107. document.addEventListener("mouseup", stopDragging);