最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I've defined a path in d3.js, it draws correctly, but .getTotalLength() is undefined - Stack Overflow

programmeradmin1浏览0评论

The following code generates a path from data.

var path = gameBoard.append('path')
   .attr("id", "snake" + snakeIndex)
   .attr("d", interpolator(data))
   .attr('stroke-width', snakeStroke)
   .attr('fill', 'none')
   .attr('stroke', config.snakeColor);

The curvy path defined by the data draws correctly.

Fails here getTotalLength() is not defined:

var totalLength = path.getTotalLength();

Additionally getPointAlongLength() is not defined either:

var point = path.getPointAtLength(position);

The following code generates a path from data.

var path = gameBoard.append('path')
   .attr("id", "snake" + snakeIndex)
   .attr("d", interpolator(data))
   .attr('stroke-width', snakeStroke)
   .attr('fill', 'none')
   .attr('stroke', config.snakeColor);

The curvy path defined by the data draws correctly.

Fails here getTotalLength() is not defined:

var totalLength = path.getTotalLength();

Additionally getPointAlongLength() is not defined either:

var point = path.getPointAtLength(position);
Share Improve this question edited Sep 13, 2016 at 20:01 GeeWhizBang asked Sep 13, 2016 at 19:50 GeeWhizBangGeeWhizBang 85711 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

Instead of:

var totalLength = path.getTotalLength();

It has to be:

var totalLength = path.node().getTotalLength();

getTotalLength() works on the node, but path is a D3 selection, not the DOM element itself. So, you have to use path.node().

yes, I discovered I had to use path.node() to get the right object. The d3.js documentation is extremely hard to read, with few examples.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论