gpt4 book ai didi

d3.js - 使用 getBBox() 获取文本区域

转载 作者:行者123 更新时间:2023-12-03 20:00:31 24 4
gpt4 key购买 nike

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() 获取每个文本的文本区域?

最佳答案

此处的最佳方法取决于您要尝试做什么。大多数 d3 回调函数会将当前 DOM 元素提供为 this ,所以这应该有效:

text.each(function() {
console.log(this.getBBox());
});

除此之外,问题是您需要使用该数字的上下文。例如,要获得文本宽度的总和,您可以执行以下操作:
var textWidth = 0;
text.each(function() {
textWidth += this.getBBox().width;
});

关于d3.js - 使用 getBBox() 获取文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13922862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com