使用getBBox()获取文本区域

编程入门 行业动态 更新时间:2024-10-28 16:16:45
本文介绍了使用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()获取文本区域

本文发布于:2023-11-10 00:26:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573848.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   区域   getBBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!