<button onclick="doDrawing();">Start Drawing</button>
<button onclick="stopDrawing();">Stop Drawing</button>
<canvas id="myCanvas" width="400" height="400" style="border:1px solid #FFFF00;">
Your browser does not support the HTML5 canvas tag.</canvas>
JavaScript
var t = 0;
var c = document.getElementById("myCanvas");
var R = c.width / 2;
var ctx = c.getContext("2d");
function doDrawing() {
t = 0;
// Clear the Canvas
ctx.clear();
// Create a random color
var timesRun = 0;
var color = '#' + Math.floor(Math.random() * 16777215).toString(16);
// Initial x and y
var x = R + R * Math.cos(0);
var y = R + R * Math.sin(0);
// Start the Drawing
ctx.beginPath();
ctx.strokeStyle = color;
ctx.moveTo(x, y);
//Use the timer to create drawing
var interval = setInterval(function() {
timesRun += 1;
if (timesRun === 65) {
clearInterval(interval);
}
drawCircle();
}, 20);
}
function drawCircle() {
t += 0.1;
x = Math.floor(R + R * Math.cos(t));
y = Math.floor(R + R * Math.sin(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();
}
};
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.