raphael icon indicating copy to clipboard operation
raphael copied to clipboard

Chrome and tuneText()

Open se-tob opened this issue 13 years ago • 6 comments

In Chrome 19 and Raphael 2.1.0 I experienced a random problem with svg text. First in line 4308: fontSize = node.firstChild ? toInt(R._g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;

fontSize sometimes is NaN.

Second, line 4337: dy attribute in sometimes equals y attribute in parent node , so that the text is vertically misplaced .

se-tob avatar Jun 20 '12 10:06 se-tob

I've ran into this problem too when manipulating attributes on text elements that have multiple line breaks. The text renders fine at first but once you start adjusting attributes raphael will re-render the element and getComputedStyle returns "" which the toInt turns into a NaN

the font-size is needed to compute the dy attribute so my fix is to check to see if fontSize has gone NaN and if so attempt to set fontSize with font-size out of the attribute hash a

rcyrus avatar Dec 04 '12 15:12 rcyrus

https://github.com/DmitryBaranovskiy/raphael/blob/master/dev/raphael.svg.js#L576

It seems that if the text element have detached from document,say you have a text element on paper,then you do papaer.clear(),now set the text element's attributes will cause this error.

@rcyrus explained the reason, e.g.

parseInt(document.defaultView.getComputedStyle(
    document.createElement('div'),null).getPropertyValue("font-size"),10)

will get NaN.

CJex avatar Nov 28 '14 12:11 CJex

Bump; I'm somewhat of a newbie with Raphael, but is there a solution (or proposed solution) to this problem?

hbarudin avatar Sep 06 '15 19:09 hbarudin

@hbarudin It's been ages since i've worked with Raphael but if i recall correctly I created my own, rather inelegant patch, that simply chose a more appropriate value for fontSize once the value that Rapheal was tracking went NaN. I can't remember the exact case where fontSize went bad but it had something to do with resizing the text container.

My patch went around this particular line of code (no idea what line it is now, it probably has changed in the past couple years) and watched for the change and then once it went NaN I looked for a CSS value in font-size and set fontSize to that value.

Like i said, this was inelegant and was used in a pinch. I eventually switched from using rapheal to something else for this particular project.

Best of luck.

rcyrus avatar Sep 08 '15 15:09 rcyrus

Hi, thank your for this thread and the suggestions. I've added

if(isNaN(fontSize)) {
    fontSize = 10;
}

after the line fontSize = node.firstChild ? toInt(R._g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10; (6147 in Raphael 2.3.0) and this solved it.

riffy avatar Aug 19 '20 23:08 riffy

Does this pr https://github.com/DmitryBaranovskiy/raphael/pull/1066 fix this problem? Why is it not merged?

oovm avatar Oct 26 '20 13:10 oovm