Actualmente estoy trabajando con SVG. Necesito saber la longitud de la cadena en píxeles para hacer algo de alineación. ¿Cómo puedo hacer para obtener la longitud de una cadena en píxeles?Obtener la longitud de píxel de String en Svg
Actualización: Gracias a nrabinowitz. Con base en su ayuda, ahora puedo obtener la longitud del texto agregado dinámicamente. Aquí hay un ejemplo:
<svg id="main"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
width="1020"
height="620"
viewBox="0 0 1020 620"
onload="startup(evt)">
<script>
<![CDATA[
var startup = function (evt) {
var width;
var svgNS = "http://www.w3.org/2000/svg";
var txtNode = document.createTextNode("Hello");
text = document.createElementNS(svgNS,"text");
text.setAttributeNS(null,"x",100);
text.setAttributeNS(null,"y",100);
text.setAttributeNS(null,"fill","black");
text.appendChild(txtNode);
width = text.getComputedTextLength();
alert(" Width before appendChild: "+ width);
document.getElementById("main").appendChild(text);
width = text.getComputedTextLength();
alert(" Width after appendChild: "+ width)
document.getElementById("main").removeChild(text);
}
//]]>
</script>
</svg>
en que idioma estas trabajando? – Jasper
Me refiero a Scalable Vector Graphics. – nxhoaf
Claramente, pero está procesando un svg en algún idioma, ¿verdad? – Jasper