【发布时间】:2018-08-07 23:32:03
【问题描述】:
我正在尝试计算 svg <text> 在内存中的长度(不是在实际的 DOM 中)。这可能吗?如果不是,有什么可能的解决方案?
我有下面的 sn-p 但它说
"message": "Uncaught TypeError: text.node(...).getComputedTextLength is not a function",
这是sn-p:
var svg = d3.select('svg');
var text1 = svg.append("text")
.attr("x", 10)
.attr("y", 30)
.text("Hello world!");
console.log("the text in DOCUMENT has " + text1.node().getComputedTextLength() + " px")
var div = document.createElement('div');
var svg = d3.select(div).append('svg');
text2 = svg.append("text")
.attr("x", 10)
.attr("y", 30)
.text("Hello world!");
console.log("the text in MEMORY has " + text2.node().getComputedTextLength() + " px")
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>
【问题讨论】:
标签: javascript d3.js svg