本文介绍了使用getBBox()获取文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var text = vis.selectAll("text")
.data(words, function(d) { return d.text.toLowerCase(); });
text.enter().append("text")
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")"})
.style("font-size", function(d) { return d.size + "px"; })
var bbox = text.node().getBBox();
如何使用getBBox()获取每个文本的文本区域?
How do I use getBBox() to get the text area of each text?
推荐答案这里最好的方法取决于你想做什么。大多数d3回调函数将提供当前DOM元素 this ,所以这应该工作:
The best approach here depends on what you're trying to do. Most d3 callback functions will supply the current DOM element as this, so this should work:
text.each(function() { console.log(this.getBBox()); });除此之外,问题是您需要使用该数字的上下文。例如,要获得文本宽度的总和,可以这样做:
Beyond that, the question is the context in which you need to use that number. For example, to get the sum of the text widths, you could do:
var textWidth = 0; text.each(function() { textWidth += this.getBBox().width; });更多推荐
使用getBBox()获取文本区域
发布评论