keleyi.htm的代碼如下:
復制代碼代碼如下:
<!DOCTYPE Html>
<Html>
<head>
<title>Html旋轉畫布</title>
<script type="text/Javascript" src="/jquery-1.10.2.min.JS"></script>
<script type="text/Javascript" src="jb51.JS"></script>
</head>
<body>
<canvas id="jb51"></canvas>
</body>
</Html>
jb51.JS的代碼如下:
復制代碼代碼如下:
/*
* 功能:畫布旋轉
*/
(function(){
var canvas=null,
context=null,
angle=0;
function resetCanvas(){
canvas=document.getElementById("jb51");
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
context=canvas.getContext("2d");
}
function animate(){
context.save();
try{
//清除畫布
context.clearRect(0, 0, canvas.width, canvas.height);
//設置原點
context.translate(canvas.width * 0.5, canvas.height * 0.5);
//旋轉角度
context.rotate(angle);
//設置填充顏色
context.fillStyle = "#FF0000";
//繪制矩形
context.fillRect(-30, -30, 60, 60);
angle += 0.05 * Math.PI;
}
finally{
context.restore();
}
}
$(window).bind("resize",resetCanvas).bind("reorIEnt",resetCanvas);
$(document).ready(function(){
window.scrollTo(0,1);
resetCanvas();
setInterval(animate,40);
});
})();