Canvas Test 2MidGrowingRect
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 w = canvas.width;
var h = canvas.height;
context.save();
context.beginPath();
//////////////////
var canvasMidW = w/2;
var canvasMidH = h/2;
var rectWidth = w * progress;
var rectPosX = canvasMidW - (rectWidth / 2);
var secondRectHeight = h * progress;
var secondRectPosY = canvasMidH - (secondRectHeight / 2);
context.moveTo(rectPosX, 0);
context.rect(rectPosX, 0, rectWidth, h);
context.moveTo(0, secondRectPosY);
context.rect(0, secondRectPosY, w, secondRectHeight);
//////////////////
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();
});
$("#next").on("click", function(){
draw();
});
$("#reset").on("click", function(){
...