Canvas Test BlindsTransition
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;
var duration = 4;
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 blindsCount = 6;
var maxBlindHeight = h / blindsCount;
for (var i = 0; i < blindsCount; i++) {
var centerY = (i + 0.5) * maxBlindHeight;
var factor = Math.max(0, 2 * progress - centerY / h);
factor = Math.min(factor, 1);
var blindHeight = maxBlindHeight * factor;
var blindPosY = centerY - (blindHeight / 2);
context.moveTo(0, blindPosY);
context.rect(0, blindPosY, w, blindHeight);
}
//////////////////
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();
});
...