SVG - 从Window坐标到ViewBox坐标

时间:2010-05-28 16:10:50

标签: javascript firefox svg

基本上我有一个svg“SecondSVG”进入一个svg“FirstSVG”进入一个svg“MainSVG”。每个svg都有自己的ViewBox。此页面可以通过另一页加载到屏幕上的任何位置 所以我基本上如何找到“SecondSVG”的viewBox的屏幕x,知道这个svg基本上可以根据调用页面加载到任何地方? event.clientX为自己提供了屏幕的x坐标。如果我不知道ViewBox的“SecondSVG”的坐标,那么如何在“SecondSVG”的ViewBox中找到x坐标?

我正在使用Firefox 3.6.3并且我有一个事件对象,我可以从中提取clientX,clientY和其他相对于屏幕的坐标。然而,我需要的是ViewBox内的坐标。

1 个答案:

答案 0 :(得分:6)

function click(evt)
{

var root = document.getElementById('your svg');

        var uupos = root.createSVGPoint();

        uupos.x = evt.pageX;

        uupos.y = evt.pageY;

        var ctm = evt.target.getScreenCTM();

        if (ctm = ctm.inverse())

            uupos = uupos.matrixTransform(ctm);



 ///the new x y are : uupos.x , uupos.y 
}