Canvas Test Wheel
Testing some stuff
by joquery
HTML
<canvas id="myCanvas" width="300" height="200"></canvas>
<div>
<button id="start">Start</button>
<button id="stop">Stop</button>
<button id="next">Next</button>
<button id="reset">Reset</button>
</div>
CSS
#myCanvas {
background: #fafaee;
}
JavaScript
var context;
var canvas;
var drawInterval = 1000.0 / 30.0;
var duration = 2;
var stepping = 1 / (duration * drawInterval);
var intervalId;
var progress = 0.0;
var draw = function() {
if (progress + stepping > 1) {
progress = 1;
} else {
progress += stepping;
}
context.save();
context.beginPath();
var ratio = canvas.width / canvas.height;
var w = canvas.width;
var h = canvas.height;
context.save();
context.beginPath();
//////////////////
var canvasMidW = w/2;
var canvasMidH = h/2;
// start point in the middle
context.moveTo(canvasMidW, canvasMidH);
// 4 arcs drawing 90°
var halfPI = Math.PI / 2;
var radius = Math.max(w, h);
for (var i=0; i<4; i++) {
var startAngle = i * halfPI;
var endAngle = startAngle + halfPI * progress;
context.arc(canvasMidW, canvasMidH, radius, startAngle, endAngle, false);
context.moveTo(canvasMidW, canvasMidH);
}
//////////////////
context.clip();
context.rect(0,0,canvas.width,canvas.height);
context.fillStyle="red";
context.fillRect(0,0,canvas.width,canvas.height);
if (progress === 1) {
clearInterval(intervalId);
}
context.restore();
};
var startAnimation = function() {
intervalId = setInterval(draw, drawInterval);
//draw();
};
var resetAnimation = function() {
clearInterval(intervalId);
progress = 0;
context.restore();
canvas.width = canvas.width;
canvas.height = canvas.height;
};
var stopAnimation = function() {
clearInterval(intervalId);
};
jQuery(document).ready(function(){
canvas = $("#myCanvas")[0];
context = canvas.getContext("2d");
$("#start").on("click", function(){
startAnimation();
});
$("#stop").on("click", function(){
stopAnimation();
});
...