如何放大处于视图中的 SVG 缩放框中心。

huangapple go评论57阅读模式
英文:

How to zoom in on center of svg zoom box that is in view

问题

以下是翻译好的部分:

"Hello i am trying to add zooming in my svg what i am trying to do is to zoom on center of the box that is currently visible on screen. i need that because their is panning so if someone drag. the box and it is only half in view so then i had to zoom on the center part of the box that is in view. i have provided the codePen please check it out"

你好,我正在尝试在我的SVG中添加缩放功能,我想要的是在当前屏幕上可见的框的中心进行缩放。我需要这样做是因为有拖动操作,所以如果有人拖动了框,而且只有一半在视野中,那么我需要缩放到视野中框的中心部分。我已经提供了Codepen链接,请查看:

Codepen链接

接下来的部分是JavaScript代码,无需翻译。如果您需要进一步的帮助,请告诉我。

英文:

Hello i am trying to add zooming in my svg what i am trying to do is to zoom on center of the box that is currently visible on screen. i need that because their is panning so if someone drag. the box and it is only half in view so then i had to zoom on the center part of the box that is in view. i have provided the codePen please check it out

Codepen Link

const panBox = document.querySelector(".pan-box");
const zoomBox = document.querySelector(".zoom-box");

const dot = document.querySelector("#dot");
const table = document.querySelector("#table");
const tableGroup = document.querySelectorAll(".round-table");

const addDragAndPanning = Draggable.create(".round-table, .pan-box", {
  onPress: function (e) {
    e.stopPropagation();
  }
});

let radius = gsap.getProperty(table, "r");
const changeCircleSize = Draggable.create(dot, {
  type: "y",
  onDragStart: function () {
    dragCircle[0].disable();
  },
  onDrag: function () {
    table.setAttribute(
      "r",
      gsap.utils.clamp(20, Infinity, Math.abs(radius - this.y))
    );
  },
  onDragEnd: function () {
    dragCircle[0].enable();
  }
});

/* ------------------ ZOOM ----------------- */

window.addEventListener("wheel", onWheel);
let zoom = 1;
function onWheel(event) {

  /* --------------------- Responsible for Zoom in and out -------------------- */
  let delta = event.wheelDelta;
  if (delta > 0) {
    svgBox.style.transform =
      zoom >= 0.08 ? `scale(${(zoom -= 0.02)})` : `scale(${zoom})`;
  } else {
    svgBox.style.transform =
      zoom < 1.3 ? `scale(${(zoom += 0.02)})` : `scale(${zoom})`;
  }

  event.preventDefault();
}

Here are the Explaining images

如何放大处于视图中的 SVG 缩放框中心。

如何放大处于视图中的 SVG 缩放框中心。

如何放大处于视图中的 SVG 缩放框中心。

I tried different ways searched on stackoverflow but can't make it. Most of the answers were on image but i am trying this in svg. Please help me solve this.

答案1

得分: 1

以下是翻译好的部分:

  1. 将SVG平移,使SVG坐标,即您的指针所在位置,现在位于原点(0,0)。
  2. 按照您的缩放比例缩放SVG。
  3. 将SVG在与指针位置相反的方向平移回去。
    但请记住,由于SVG已被缩放,平移距离现在将不同。因此,您需要将平移坐标除以刚刚缩放的量。

您可能会遇到一些问题,因为您在平移方面使用了第三方库,而在缩放方面使用了自己的代码。但我希望这能帮助您达到目标。

英文:

The way to think about what you need to do is to follow these three steps when you zoom.

  1. Translate the SVG so that the SVG coordinates, that are where your pointer is, are now at the origin (0,0)
  2. Scale the SVG by your zoom ratio
  3. Translate the SVG back in the opposite direction to the pointer position.
    But remember that the translate distance will now be different because the SVG has been scaled. So you'll need to divide the translate coords by the amount you just zoomed.

You may have a few problems because you are using a third-party library for your panning, and your own code for the zoom.

But I hope this helps you get there.

huangapple
  • 本文由 发表于 2023年6月22日 02:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定