Jump to content

modifying SVG text


meryet

Recommended Posts

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...