Skip to content

Instantly share code, notes, and snippets.

@cmargroff
Last active June 17, 2016 19:56
Show Gist options
  • Select an option

  • Save cmargroff/f7000aa09249e95aaacde2ca2ff3214f to your computer and use it in GitHub Desktop.

Select an option

Save cmargroff/f7000aa09249e95aaacde2ca2ff3214f to your computer and use it in GitHub Desktop.
Object.defineProperty(SVGPolygonElement.prototype, 'pathLength', {
get: function(){
var numPoints = this.points.length || this.points.numberOfItems;
var l = 0;
for (var i = 1; i < numPoints; i++) {
var point2 = this.points[i] || this.points.getItem(i);
var point1 = this.points[i-1] || this.points.getItem(i-1);
l += Math.sqrt(Math.pow(Math.abs(point2.x-point1.x),2)+Math.pow(Math.abs(point2.y-point1.y),2));
}
return l;
},
set: function(val){ return undefined; }
});
Object.defineProperty(SVGCircleElement.prototype, 'pathLength', {
get: function(){
return 2*Math.PI*this.getAttribute("r");
},
set: function(val){ return undefined; }
});
Object.defineProperty(SVGRectElement.prototype, 'pathLength', {
get: function(){
return 2*this.getAttribute("width")+2*this.getAttribute("height");
},
set: function(val){ return undefined; }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment