Attualmente sto lavorando con SVG. Ho bisogno di conoscere la lunghezza della stringa in pixel per fare un po 'di allineamento. Come posso fare per ottenere la lunghezza di una stringa in pixel?Ottieni la lunghezza in pixel di stringa in Svg
Aggiornamento: Grazie a nrabinowitz. Sulla base del suo aiuto, ora posso ottenere la lunghezza del testo con aggiunta dinamica. Ecco un esempio:
<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>
in che lingua stai lavorando? – Jasper
Intendo Grafica vettoriale scalabile. – nxhoaf
Chiaramente, ma stai elaborando un svg in qualche lingua, no? – Jasper