我有兩個按鈕,可放大和縮小我的畫布動畫如何在動畫中縮放畫布?
<button id="zoomInButton">Zoom ++</button>
<button id="zoomOutButton">Zoom -- </button>
<canvas id="myCanvasmatchPage" >
Sorry, your browser doesn't support canvas technology
</canvas>
的我有使用的setInterval和帆布的動畫循環(略)。爲了不發生縮放,每個setInterval都必須超出draw函數循環。實際上,ctx.scale在按鈕單擊時重置。
$('#myCanvasmatchPage').attr({width:100,height:200}).css({width:'100px',height:'200px'});
var canvas = document.getElementById("myCanvasmatchPage");
ctx = canvas.getContext("2d");
ctx.translate(0.5,0.5);
var nextFrame =0;
var animationData = [];
var canvasZoom = 1; //initial is scale 100%
var incrementZoom = 0.05;
$("#zoomInButton").click(function()
{
canvasZoom += incrementZoom;
ctx.scale(canvasZoom, canvasZoom);
});
$("#zoomOutButton").click(function()
{
if (canvasZoom > 1){//i dont want to allow a less than 100% scale zoom
canvasZoom -= incrementZoom;
ctx.scale(canvasZoom, canvasZoom);
}
});
var draw = function(){
//cannot set scale within animation loop! keeps resetting!
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(images['background'], 0, 0, 622, 924, 0, 0, 311, 462);
if (nextFrame<=animationData.length){
//animation loop
}else{
clearTimeout(draw);//get out of the loop
return;
}
nextFrame++;
}
setInterval(draw,200);
我目前使用這段代碼的經驗是zoomIn'works',但是zoomOut doesn-t。控制檯日誌記錄顯示,canvasZoom一直上下0.05(有時也是0.05000001公頃),但畫布不會縮小。
它讓我覺得問題在於清除畫布而不是縮放。
你是否有意做其他的事情比縮放(旋轉/平移/歪斜)? – GameAlchemist 2014-09-12 15:43:41
@GameAlchemist,實際上是旋轉,也是一種移動動畫「中心點」的方式。 – Gamemorize 2014-09-12 15:46:20