The problem is that this solution only works if all these values are set in
the document's
svg element. I am sorry if you misunderstood me, unfortunately my English
is lousy.
I will try to provide some more information to make my problem clear:
I automatically generate SVG documents from little svg "snippets". In the
resulting document these snippets are placed in a <defs> block and are
referenced via <use> tags. A snippet can reference another snippet.
A generated document may look like this:
<svg>
<g>
...
<use xlink:href="#snippet1" x="100" y="100" />
...
</g>
<defs>
<g id="snippet1">
...
<use xlink:href="#snippet2" x="80" y="-90"/>
</g>
<g id="snippet2">
...
</g>
</defs>
So there are no viewBox, width, height, viewPort values for that document.
The problem is, that
depending on where a snippet is added (or at which coordinates the snippet
which is referenced
first begins), parts of the document are not visible because they are
located in the negative area
of my coordinate system.
To adjust values like viewBox (and to add a dimension arrow without
overlaying somthing)
something, I need to know absolute top-left X, Y and bottom-right X, Y
coordinates.
Christian
You can get those values by simply reading the x, y, and viewport values of
svgNode. Here is how you do it.
I am modifying the same code so that you can read all values.
function whatever()
{
//reference to entire document
var svgdoc = document.rootElement.getOwnerDocument();
//reference to <svg......> tag
var svgtag = svgdoc.getDocumentElement();
/*******************************************/
//CODE FOR GETTING VIEWPORT VALUES
/*******************************************/
var docX = svgtag.getAttribute("x");
var docY = svgtag.getAttribute("y");
var docWidth = svgtag.getAttribute("widht");
var docWidth = svgtag.getAttribute("height");
/*******************************************/
//CODE FOR GETTING VIEWBOX VALUES
/*******************************************/
//returns viewBox value as string
var vBox = svgtag.getAttribute("viewBox");
//splits string into array separated by space
var vBoxArray = vBox.split(/ /);
var minX = parseInt(vBoxArray[0]);
var minY = parseInt(vBoxArray[1]);
//by adding width to minX you can get maxX
var width = parseInt(vBoxArray[2]);
//by adding hegith to minY you can getmaxY
var height = parseInt(vBoxArray[3]);
}
Irfan Ali
Software Developer
Universal Map, Inc.
www.universalmap.com
Ph: (517)655-1759
I mean the absolute top-left X, Y and bottom-right X, Y of the whole SVG
document
(including what is NOT visible on the screen).
Christian
Do you mean the top-left X, Y and bottom-right X, Y of SVG Document thats
visible on screen?
If yes all you need to do is read the viewBox values. Here is a little
javascript code that does this.
function whatever()
{
var svgdoc = document.rootElement.getOwnerDocument(); //reference
to entire document
var svgtag = svgdoc.getDocumentElement(); //reference to <svg
......> tag
var vBox = svgtag.getAttribute("viewBox"); //returns viewBox value
as string
var vBoxArray = vBox.split(/ /); //splits string into array
separated by space
var minX = parseInt(vBoxArray[0]);
var minY = parseInt(vBoxArray[1]);
var width = parseInt(vBoxArray[2]); //by adding width to minX you
can get maxX
var height = parseInt(vBoxArray[3]); //by adding hegith to minY you
can getmaxY
}
Hope this will help.
Irfan Ali
Software Developer
Universal Map, Inc.
www.universalmap.com
Ph: (517)655-1759
Hello,
I need to know the minimum and maximum coordinates of an SVG document or
in other words: what are the lowest and highest x and y values in a whole
document.
My only idea is to parse all the elements, get the coordinates and
calculate the
min and max values, but that seems very costly to me so how can I fetch
minX, minY,
maxX, maxY easier?
Christian
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]