简体   繁体   English

如何使用 SvgPanZoom 平移和缩放以适合元素

[英]How to pan and zoom to fit an element with SvgPanZoom

I'm using svg-pan-zoom library and I need to pan/zoom the view to fit a particular element.我正在使用 svg-pan-zoom 库,我需要平移/缩放视图以适合特定元素。 I could use fit method but it fits the whole content in this case I need to fit only one particular element.我可以使用 fit 方法,但它适合整个内容,在这种情况下我只需要适合一个特定元素。 Another option can be to calculate the pan and zoom required and use the custom control, but how to get the pan/zoom of an element to fit the window?另一种选择是计算所需的平移和缩放并使用自定义控件,但如何让元素的平移/缩放适合窗口?

UPDATE更新

I tried to follow the @bumbu "easier" solution.我试图遵循@bumbu“更简单”的解决方案。 That was my first thought but I have encountered some troubled with the zooming point position.这是我的第一个想法,但我遇到了缩放点位置的问题。

This is a fiddle to show the expected behaviour and the calculation attempt.这是显示预期行为和计算尝试的小提琴。

http://jsfiddle.net/mgv5fuyw/2/ http://jsfiddle.net/mgv5fuyw/2/

this is the calculation:这是计算:

var bb=$("#target")[0].getBBox();
var x=bb.x+bb.width/2;
var y=bb.y+bb.height/2;

计算

But somehow the zooming center expected (225,225) is not the right one.但不知何故,预期的缩放中心 (225,225) 不是正确的。

I found a solution panning before zooming, I could not find the right way to use zoomAtPoint() method.我在缩放之前找到了一个解决方案平移,我找不到使用 zoomAtPoint() 方法的正确方法。

http://jsfiddle.net/mgv5fuyw/3/ http://jsfiddle.net/mgv5fuyw/3/

var bb=$("#target")[0].getBBox();
var vbb=panZoomInstance.getSizes().viewBox;
var x=vbb.width/2-bb.x-bb.width/2;
var y=vbb.height/2-bb.y-bb.height/2;
var rz=panZoomInstance.getSizes().realZoom;
var zoom=vbb.width/bb.width;
panZoomInstance.panBy({x:x*rz,y:y*rz});
panZoomInstance.zoom(zoom);

Without going into detail I'd try 2 approaches:没有详细说明,我会尝试两种方法:

Easier:更轻松:

  • Init the svg-pan-zoom library初始化 svg-pan-zoom 库
  • Fit and center you SVG适合并居中 SVG
  • Calculate positions (top-left and bottom-right, or center and size) of the elements you're interested in计算您感兴趣的元素的位置(左上角和右下角,或中心和大小)
  • Now based on viewport size you should be able to calculate zoom level and center point of each element现在根据视口大小,您应该能够计算每个元素的缩放级别和中心点

Harder:更难:

  • Figure out relative position of the original objects relative to original viewport找出原始对象相对于原始视口的相对位置
  • Based on current viewport size you should be able to calculate zoom level and center point of each element根据当前视口大小,您应该能够计算每个元素的缩放级别和中心点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM