Live demo
calcPointsCirc takes 4 arguments, center x / y, dash radius and length. It returns an array of points, x, y, ex, ey. You can simply scroll the points to draw a dotted circle. There probably are much more elegant ways to do this, but the curly identifier gives it a chance.
function calcPointsCirc( cx,cy, rad, dashLength) { var n = rad/dashLength, alpha = Math.PI * 2 / n, pointObj = {}, points = [], i = -1; while( i < n ) { var theta = alpha * i, theta2 = alpha * (i+1); points.push({x : (Math.cos(theta) * rad) + cx, y : (Math.sin(theta) * rad) + cy, ex : (Math.cos(theta2) * rad) + cx, ey : (Math.sin(theta2) * rad) + cy}); i+=2; } return points; } var canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'); canvas.width = canvas.height= 200; var pointArray= calcPointsCirc(50,50,50, 1); ctx.strokeStyle = "rgb(255,0,0)"; ctx.beginPath(); for(p = 0; p < pointArray.length; p++){ ctx.moveTo(pointArray[p].x, pointArray[p].y); ctx.lineTo(pointArray[p].ex, pointArray[p].ey); ctx.stroke(); } ctx.closePath();
Loktar Jun 09 '11 at 16:19 2011-06-09 16:19
source share