SpiroGraph
by Mike Kerney
HTML
<button onclick="doDrawing();">Start Drawing</button>
<canvas id="myCanvas" width="400" height="400" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>
JavaScript
var t = 0;
var c = document.getElementById("myCanvas");
var R =c.width / 6;
var ctx = c.getContext("2d");
function doDrawing() {
t=0;
ctx.clear();
var r =Math.floor(Math.random() * R);
var O = Math.floor(Math.random() * 20);
var run = 0;
var x= 200+((R+r)*Math.cos(t) - (r+O)*Math.cos(((R+r)/r)*t));
var y = 200+((R+r)*Math.sin(t) - (r+O)*Math.sin(((R+r)/r)*t));
var color = '#' + Math.floor(Math.random() * 16777215).toString(16);
// Start the Drawing
ctx.beginPath();
ctx.strokeStyle = color;
ctx.moveTo(x, y);
//Use the timer to create drawing
var interval = setInterval(function () {
run += 1;
if (run ===1000) {
clearInterval(interval);
}
drawCircle(r,O);
},2);
}
function drawCircle(r,O) {
t += .03;
x = Math.floor(200+(R+r)*Math.cos(t) - (r+O)*Math.cos(((R+r)/r)*t));
y = Math.floor(200+(R+r)*Math.sin(t) - (r+O)*Math.sin(((R+r)/r)*t));
ctx.lineTo(x, y) ;
ctx.stroke();
}
CanvasRenderingContext2D.prototype.clear = CanvasRenderingContext2D.prototype.clear || function (preserveTransform) {
if (preserveTransform) {
this.save();
this.setTransform(1, 0, 0, 1, 0, 0);
}
this.clearRect(0, 0, this.canvas.width, this.canvas.height);
if (preserveTransform) {
this.restore();
}
};