meryet Posted May 24, 2008 Share Posted May 24, 2008 hi all,I want to modify the text content of a SVG text item, that is, what is between <text> tags.I found a solution in Java, namely: text.setTrait("#text", "new text"); does anyone know how to do the same in javascript?thanks! Link to comment Share on other sites More sharing options...
Ingolme Posted May 24, 2008 Share Posted May 24, 2008 Access the DOM node somehow and change its nodeValue.You'll have to find something that identifies the text item, for example, maybe it's the third <text> element in the document: document.getElementsByTagName("text")[3] Link to comment Share on other sites More sharing options...
meryet Posted May 24, 2008 Author Share Posted May 24, 2008 ok, and then what should I do? the setTrait() function doesn't seem to work. Link to comment Share on other sites More sharing options...
Ingolme Posted May 24, 2008 Share Posted May 24, 2008 That's not Javascript.What you'd do it something like this:document.getElementsByTagName("text")[3].firstChild.nodeValue = "new Text";This isn't going to work, you need to identify the <text> node within the document somehow. You use XML DOM to work with SVG Link to comment Share on other sites More sharing options...
meryet Posted May 25, 2008 Author Share Posted May 25, 2008 ok great! thanks!and I have another question. I want to apply a transform on a path element. Thus I have to calculate the center of this path. Is there an easy way to do it? an already existing function? Link to comment Share on other sites More sharing options...
Synook Posted May 25, 2008 Share Posted May 25, 2008 The center of a path can be calculated by the formula ((x2 + x1) / 2, (y2 + y1) / 2) function getCenter(line) { var x1 = line.getAttribute("x1"); var x2 = line.getAttribute("x2"); var y1 = line.getAttribute("y1"); var y2 = line.getAttribute("y2"); var x = (x2 + x1) / 2; var y = (y2 + y1) / 2; return [x, y];} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.