Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

use of getComputedTextLength()

755 views
Skip to first unread message

Georg Irmer

unread,
Jun 24, 2005, 4:35:17 AM6/24/05
to
In a little svg-file I want to insert tooltips moving with the pointer.
Because of using object dependend text I'm going to calculate tooltip
box length by using the function getComputedTextLength(). Looking at the
current implementation status of SVG I find that getComputedTextLength()
is not "unimplemented" for SVGTextElements.

But the JavaScript Console of Deer Park Alpha I (Gecko/20050609
Firefox/1.0+) shows an error: "ShortInfoText.getComputedTextLength() is
not a function" I couldn't find out the real problem behind that message
yet.

Here now comes a section of my script to make clear the context:
(with MaxLength I want to find the length for the box to show text)
(nearly needless to mention: adobe plugin is doing this as I expect it)

*******************
.
.
ShortInfoText = SVGDocument.createElement("text");
ShortInfoTextValue = SVGDocument.createTextNode("");
ShortInfoTextValue.nodeValue = "some text from somewhere";
ShortInfoText.setAttribute("style","font-size:"+Height);
ShortInfoText.setAttribute("x",8);
ShortInfoText.setAttribute("y",6 + (LineNumber*(Height+1)));
ShortInfoText.appendChild(ShortInfoTextValue);
if (ShortInfoText.getComputedTextLength() > MaxLength) {
MaxLength = ShortInfoText.getComputedTextLength ();
}
.
.
*******************

thanks in advance
Georg

Martin Honnen

unread,
Jun 25, 2005, 9:11:52 AM6/25/05
to

Georg Irmer wrote:

> In a little svg-file I want to insert tooltips moving with the pointer.
> Because of using object dependend text I'm going to calculate tooltip
> box length by using the function getComputedTextLength(). Looking at the
> current implementation status of SVG I find that getComputedTextLength()
> is not "unimplemented" for SVGTextElements.
>
> But the JavaScript Console of Deer Park Alpha I (Gecko/20050609
> Firefox/1.0+) shows an error: "ShortInfoText.getComputedTextLength() is
> not a function" I couldn't find out the real problem behind that message
> yet.
>
> Here now comes a section of my script to make clear the context:
> (with MaxLength I want to find the length for the box to show text)
> (nearly needless to mention: adobe plugin is doing this as I expect it)
>
> *******************
> .
> .
> ShortInfoText = SVGDocument.createElement("text");

I don't know where you get SVGDocument from but as SVG is XML and SVG
elements are in a certain namespace it might be that you need
ShortInfoText =
document.createElementNS('http://www.w3.org/2000/svg', 'text')
to properly create an SVGTextElement.

> ShortInfoTextValue = SVGDocument.createTextNode("");
> ShortInfoTextValue.nodeValue = "some text from somewhere";
> ShortInfoText.setAttribute("style","font-size:"+Height);
> ShortInfoText.setAttribute("x",8);
> ShortInfoText.setAttribute("y",6 + (LineNumber*(Height+1)));
> ShortInfoText.appendChild(ShortInfoTextValue);
> if (ShortInfoText.getComputedTextLength() > MaxLength) {
> MaxLength = ShortInfoText.getComputedTextLength ();

I am however not sure a computed text length can be provided without
drawing the text so you might insert the text element in the graphics
first before you then try to read out the text length.

For instance, when I do

var svgNamespace = 'http://www.w3.org/2000/svg';
var svgElement = document.createElementNS(svgNamespace, 'svg');
svgElement.setAttribute('width', '200');
svgElement.setAttribute('height', '200');
var svgTextElement = document.createElementNS(svgNamespace, 'text');
svgTextElement.appendChild(document.createTextNode(
'Kibology for all.'));
svgTextElement.setAttribute('style', 'font-size: 13px;');
svgTextElement.setAttribute('x', '10');
svgTextElement.setAttribute('y', '20');
svgElement.appendChild(svgTextElement);
document.body.appendChild(svgElement);
svgTextElement.getComputedTextLength()

in an XHTML document then getComputedLength gives me a value
(88.86719512939453) while it yields 0 if the text element is not inserted.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Georg Irmer

unread,
Jun 27, 2005, 2:30:36 AM6/27/05
to
Dear Martin,

thanks a lot for your answer: to pay attention on the proper namespace
was the right way to get expected results.

Georg

Martin Honnen schrieb:

0 new messages