Svg getComputedTextLength function always returnin

2019-04-24 07:50发布

Hello I tried to integrate this d3 zoomable treemap from

http://mbostock.github.io/d3/talk/20111018/treemap.html

but the getComputedTextLength() always returns 0. I also tried getBBox(), but it returns an object with all properties with zero.

    .text(function(d) { return d.name; })
    .style("opacity", function(d) {
         d.w = this.getComputedTextLength(); 
         return d.dx > d.w ? 1 : 0;
    });

Do I need to put my text wrapped in a tspan?
What other reasons could generate this?

标签: svg d3.js
1条回答
贼婆χ
2楼-- · 2019-04-24 08:09

I realize that this is over a year late, but I ran into the same issue recently and this may help someone. this.getComputedTextLength() returns 0 if the text element hasn't been rendered yet or if the container or any of its parent containers have display set to none at the time the text element is being rendered.

查看更多
登录 后发表回答