简体   繁体   English

使用svg.js和svg-pan-zoom如何获取当前的“视口”中心点?

[英]Using svg.js and svg-pan-zoom how can I get current “viewport” center point?

I'm using svg.js and the great svg-pan-zoom plugin ( https://github.com/ariutta/svg-pan-zoom ). 我正在使用svg.js和出色的svg-pan-zoom插件( https://github.com/ariutta/svg-pan-zoom )。

Now I want to have a button that when I click on it it draws a circle on the current center point of the viewport am I seeing (so the position is dynamic depending on the pan/zoom I've done). 现在,我想有一个按钮,当我单击它时,它会在我所看到的视口的当前中心点绘制一个圆(因此位置是动态的,具体取决于我完成的平移/缩放)。

I setup a quick example here that draws the circle statically on a specified position. 在这里设置了一个简单的示例 ,该示例在指定位置上静态绘制了圆。

Please note I have some specific needs: I'm showing a "background" image with a scaling factor like this: 请注意,我有一些特定的需求:我正在显示具有这样缩放比例的“背景”图像:

var map = mainWindow.group();
map.addClass('seatMap');
map.image('http://upload.wikimedia.org/wikipedia/commons/b/b3/World-map-2004-cia-factbook-large-1.7m-whitespace-removed.jpg').loaded(function () {
    map.cx(viewboxW / 2);
    map.cy(viewboxH / 2);
}).scale(0.70);

So how can I get the current center point? 那么如何获得当前的中心点呢?

Find the initial centerX and centerY positions(Before zoom) using the code below. 使用以下代码查找初始centerX和centerY位置(缩放之前)。

var viewPort = document.getElementsByClassName("viewport")[0];
var bbox = viewPort.getBBox();
var initX = (bbox.x+bbox.width)/2,
    initY = (bbox.y+bbox.height)/2;

You can calculate the center point, after zoom using the code written below. 缩放后,您可以使用下面编写的代码计算中心点。

var pt =  draw.node.createSVGPoint();
pt.x = initX;
pt.y = initY;
pt = pt.matrixTransform(viewPort.getCTM());

pt.x will be the centerX and pt.y will be the centerY positions. pt.x将是的centerX和pt.y将是centerY位置。

Hope this helps. 希望这可以帮助。

svg-pan-zoom has 2 API methods that may help you: svg-pan-zoom有2种API方法可以帮助您:

  • getSizes
  • getPan

You may be mostly interested in getSizes as it will return current size of bounding box, real zoom and viewport sizes and positinos. 您可能对getSizes最感兴趣,因为它将返回边界框的当前大小,实际缩放和视口大小以及positinos。

I had a similar problem: I wanted to add a rect on the top-left corner of the actual zoomed canvas (the zoomable svg), so I made it this way: 我有一个类似的问题:我想在实际的缩放画布(可缩放的svg)的左上角添加一个rect,所以我这样做是这样的:

var positionX = -1*svgPanZoom.getPan.x/svgPanZoom.getSizes.realZoom;
var positionY = -1*svgPanZoom.getPan.y/svgPanZoom.getSizes.realZoom;

Hope it helps 希望能帮助到你

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

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