I solved this problem based on this brilliant answer: https://stackoverflow.com/a/10045537/4505826.
Remember the path must be a closed path, which means it has to end with Z.
When I run the code, the browser keep notifying these errors :
- path.getPointAtLength() is not a function
- path.getTotalLength() is not a function
- Uncaught TypeError: Cannot read property 'createElementNS' of undefined
:::: SOLUTION: Adjust the above code as follows ::::
path.getPointAtLength(d) -> path.node().getPointAtLength(d)
path.getTotalLength(d) -> path.node().getTotalLength(d)
And replace
var doc = path.ownerDocument;
var poly = doc.createElementNS('http://www.w3.org/2000/svg','polygon');
by
var poly = document.createElementNS('http://www.w3.org/2000/svg','polygon');
as from this answer https://stackoverflow.com/a/25611318/4505826.