简体   繁体   English

减少svg文本的区域

[英]Reduce area of svg text

I am currently creating a word cloud using an in house developed library, it uses the svg element text to display the words, the problem I have encounter is that the area of some words sometimes overlaps other words as you can see if you inspect test1 in this jsfiddle , this becomes a problem if the words must be clickable. 我目前正在使用内部开发的库创建一个词云,它使用svg元素text来显示单词,我遇到的问题是某些单词的区域有时会与其他单词重叠,如果你检查test1 in这个jsfiddle ,如果单词必须可点击,这就成了一个问题。

I want to know if it is possible to reduce the area of the text to the minimum, just wrapping the word, a small padding is accepted. 我想知道是否可以将文本区域减少到最小,只是包装单词,接受一个小填充。

I have already tried the solution posted in this answer but it didn't work. 我已经尝试过在这个答案中发布的解决方案,但它没有用。

I would prefer a css solution if it exists rather than messing with svg but if there is no other option that will do. 我更喜欢一个css解决方案,如果它存在而不是搞乱svg,但如果没有其他选项可以做。

Edit: Ok, enough reputation to post images. 编辑:好的,有足够的声誉来发布图片。 What I currently have: 我目前拥有的:

在此输入图像描述

What I would like to have: 我想拥有什么:

在此输入图像描述

There are two problems; 有两个问题; I currently have only a solution to one. 我目前只有一个解决方案。 Your text example is misleading. 您的文字示例具有误导性。 Try Text1g instead to see the descent (ie the amount of space below the baseline which the g needs). 尝试使用Text1g来查看下降(即g需要的基线下方的空间量)。 If you do this, then you'll see that the texts really overlap - you just don't notice because your test text doesn't contain a good set of test characters. 如果你这样做,那么你会发现文本真正重叠 - 你只是没有注意到,因为你的测试文本不包含一组好的测试字符。

Apart from that, I see that the element is 67px high while the font-size is only 60px. 除此之外,我看到元素高67pxfont-size只有60px. I don't see where the additional 7 pixels are coming from. 我没有看到额外的7个像素来自哪里。 It's not padding and not margin :-/ 这不是填充而不是保证金: - /

Why do you need to know the minimum bounding box? 为什么你需要知道最小边界框?

If it is because you are linking with the element, or applying click events to the words, then you should investigate the pointer-events attribute. 如果是因为您要链接元素,或者将click事件应用于单词,那么您应该研究pointer-events属性。

You possibly want something like: 你可能想要这样的东西:

<text ... pointer-events="fill">ejecutar</text>

You will only get events when the pointer is over the fill of the words. 只有当指针位于单词的填充上时,才会获得事件。 This might be a bit fiddly for clicking though because the holes in words will not be clickable. 这可能有点繁琐,因为单词中的漏洞不可点击。

You could ease that by putting an invisible <rect> of an appropriate size in front of the word with pointer-events="fill" . 您可以通过使用pointer-events="fill"在单词前面放置一个合适大小的不可见<rect>来缓解这种情况。 The "fill" value will attract events for where the fill would be even if it is invisible. 即使不可见,“填充”值也会吸引填充所在的事件。 However that requires you know the bbox of the word, which we already established you don't have (?). 但是,这需要你知道我们已经建立的单词的bbox,你没有(?)。

You could give the words an invisible fat stroke and use pointer-events="all" . 你可以给单词一个看不见的胖pointer-events="all"并使用pointer-events="all" The invisible stroke will make the clickable area (invisbly) fatter and hence the inter-word holes smaller. 不可见的笔划将使可点击区域(不可见)更胖,因此字间孔更小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM