This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 29809 - Accessing the offset of inner SVG elements
Summary: Accessing the offset of inner SVG elements
Status: NEW
Alias: None
Product: SVG
Classification: Unclassified
Component: SVG Document Object Model (DOM) (show other bugs)
Version: SVG 2.0
Hardware: PC All
: P2 normal
Target Milestone: Test Suite
Assignee: Doug Schepers
QA Contact: SVG Public List
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-09-05 08:50 UTC by Arun
Modified: 2016-10-16 21:34 UTC (History)
3 users (show)

See Also:


Attachments
code snippet to modify the inner svg elements (167.29 KB, image/png)
2016-10-16 18:07 UTC, Arun
Details

Description Arun 2016-09-05 08:50:05 UTC
Hi Team,

It seems that currently it isn't possible to access the offset values of inner elements in an SVG. Could you please look into this. It would greatly simplify drawing and creating visual connections with other elements if it's possible to access the offsets of inner elements in an SVG.

Use case: an online electronic schematic application where part pins need to connected with other components.

Thanks,
Arun
Comment 1 Nikos Andronikos 2016-09-06 23:38:48 UTC
Could you give an example of what you're trying to achieve please?
Comment 2 Arun 2016-09-07 01:42:24 UTC
I'm trying to make an online electronic schematic application. The electronic parts are visualized using SVG rectangles, text, lines etc, (see this example part: (https://jsbin.com/tapedam/edit?html,output).

The <line> elements represent the part pins. I want to connect the pins with other components and sustain the connections even if the components are moved anywhere in the browser window.

However the lines do not expose their offsets to the DOM as they're inside the SVG. This makes it difficult to sustain the connections of the parts.

If the offsets of inner elements are exposed it would make it easier to identify the position of specific element(line, text, rectangle).
Comment 3 Arun 2016-10-13 08:51:29 UTC
Hi Team,

Is there any update on this?

Thanks,
Arun
Comment 4 Robert Longson 2016-10-13 22:32:44 UTC
What's wrong with getBBox or getBoundingClientRect? Why don't either of these give you the offset you need?
Comment 5 Arun 2016-10-14 05:08:08 UTC
It seems that getbbox works if the SVG graphic is defined line in the html. However for my application the SVG graphics are in .svg files. This makes it impossible to get the offsets of inner svg components of a .svg file.
Comment 6 Robert Longson 2016-10-14 20:23:16 UTC
get the embedded contentDocument, use that to get the element or elements you want from the svg and then call either getBBox or getBoundingClientRect on that.
Comment 7 Arun 2016-10-16 05:22:12 UTC
The SVG objects placed on the HTML will be in the order of hundreds. If I use the contentDocument method I'll have to generate hundreds of IDs. Scalability seems to be a problem with this approach.

For example, assume that there are 5 SVG objects with 8 inner components each then I'll have to create  40 IDs to uniquely access each inner component.
Comment 8 Robert Longson 2016-10-16 08:33:43 UTC
In reply to Arun from comment #7)
> The SVG objects placed on the HTML will be in the order of hundreds. If I
> use the contentDocument method I'll have to generate hundreds of IDs.
> Scalability seems to be a problem with this approach.

Maybe, or alternatively you could use CSS selectors to pick them out.(

> 
> For example, assume that there are 5 SVG objects with 8 inner components
> each then I'll have to create  40 IDs to uniquely access each inner
> component.

use classes and select by class. Or just select by tag name.
Comment 9 Arun 2016-10-16 18:07:50 UTC
Created attachment 1658 [details]
code snippet to modify the inner svg elements
Comment 10 Arun 2016-10-16 18:22:52 UTC
It seems that you have not understood the concept well. Using css selectors it isn't possible to uniquely identify an element and if I use IDs I'll have to generate a large number of unique IDs

The challenge here is to get the offsets of inner SVG elements in the HTML DOM. Even if the SVG element is moved anywhere in the DOM we should be able to access the offsets of the inner elements which can help us sustain the connections with other SVG elements.

Anyways I tried the contentDocument that you have suggested but I'm unable to modify the inner SVG components (see attached screenshot). Here is the code snippet that I'm testing on google cloud (http://104.155.221.20/).

Also Here is the link that I referred to access the inner svg components in an object tag (https://benfrain.com/selecting-svg-inside-tags-with-javascript/)
Comment 11 Robert Longson 2016-10-16 21:34:48 UTC
The fill CSS property does not apply to line elements, try modifying the stroke.

And yes you can select individual elements with CSS selectors.

I don't see any bug here. You'd probably be better off asking this kind of question on some other forum e.g. stackoverflow