A5 Canvas HTML
Testing out canvas
by scotp71
HTML
<canvas id="myCanvas" width="400" height="400" style="border: 1px solid black">Your browser does not support the HTML5 canvas tag.</canvas>
<div>
<button onclick="doDrawing();">Draw</button>
</div>
JavaScript
var t = 0;
var r = 0;
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var center = c.width/2;
var R = center/2;
var itr = 360;
var O = 45
function doDrawing() {
t = 0;
r = Math.random();
// 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);
var x = (center+(R+r)*Math.cos(t) - (r+O)*Math.cos(((R+r)/r)*t));
var y = (center+(R+r)*Math.sin(t) - (r+O)*Math.sin(((R+r)/r)*t));
// 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 === itr){
clearInterval(interval); }
drawSpiro();}, 20);
}
function drawSpiro()
{
t += 0.1;
x = Math.floor(center+(R+r)*Math.cos(t) - (r+O)*Math.cos(((R+r)/r)*t));
y = Math.floor(center+(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();
}
};