Canvas Test FADE
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 / 60.0; // 60Hz
var duration = 5;
var stepping = 1 / (duration * drawInterval);
var intervalId;
var alpha = 0.0;
var draw = function() {
if (alpha + stepping > 1) {
alpha = 1;
} else {
alpha += stepping;
}
context.globalAlpha = alpha;
context.clearRect(0,0,canvas.width,canvas.height);
context.rect(0,0,canvas.width,canvas.height);
context.fillStyle="red";
context.fillRect(0,0,canvas.width,canvas.height);
if (alpha === 1) {
clearInterval(intervalId);
}
};
var startAnimation = function() {
alpha = 0;
context.save();
intervalId = setInterval(draw, drawInterval);
//draw();
};
var resetAnimation = function() {
clearInterval(intervalId);
alpha = 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();
});
$("#next").on("click", function(){
draw();
});
$("#reset").on("click", function(){
resetAnimation();
});
});