// http://stackoverflow.com/questions/4576724/dotted-stroke-in-canvas // To draw dashed / dotted lines on a canvas var CP = window.CanvasRenderingContext2D && CanvasRenderingContext2D.prototype; if(CP && CP.lineTo) CP.dashedLine = function(x, y, x2, y2, dashArray){ if(! dashArray) dashArray=[10,5]; var dashCount = dashArray.length; var dx = (x2 - x); var dy = (y2 - y); var xSlope = (Math.abs(dx) > Math.abs(dy)); var slope = (xSlope) ? dy / dx : dx / dy; this.moveTo(x, y); var distRemaining = Math.sqrt(dx * dx + dy * dy); var dashIndex = 0; while(distRemaining >= 0.1){ var dashLength = Math.min(distRemaining, dashArray[dashIndex % dashCount]); var step = Math.sqrt(dashLength * dashLength / (1 + slope * slope)); if(xSlope){ if(dx < 0) step = -step; x += step y += slope * step; }else{ if(dy < 0) step = -step; x += slope * step; y += step; } this[(dashIndex % 2 == 0) ? 'lineTo' : 'moveTo'](x, y); distRemaining -= dashLength; dashIndex++; } } // To draw dashed / dotted circles on a canvas if(CP && CP.lineTo) CP.dashedArc = function (x, y, r, startAngle, endAngle, antiClockwise, dashArray) { if(! dashArray) dashArray=[10,5]; var dashCount = dashArray.length; var distRemaining = (endAngle - startAngle) * (r - 1); var dashIndex = 0; stepStartAngle = 0; while(distRemaining >= 0.1){ var dashLength = Math.min(distRemaining, dashArray[dashIndex % dashCount]); var stepEndAngle = stepStartAngle + (dashLength / r); if(dashIndex % 2 == 0){ this.arc(x,y,r,stepStartAngle,stepEndAngle,antiClockwise); }else{ this.moveTo(x + (Math.cos(stepEndAngle) * r), y + (Math.sin(stepEndAngle) * r)); } stepStartAngle = stepEndAngle; distRemaining -= dashLength; dashIndex++; } } // To draw text on a circle if(CP && CP.lineTo) CP.fillTextCircle = function(text,x,y,radius,startRotation){ this.save(); this.translate(x,y); this.rotate(startRotation); var distance = 0; for(var i=0;i