html5画布旋转效果示例

前端技术 2023/09/01 HTML

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=\"phpstudy.js\"></script>
</head>
<body>
<canvas id=\"phpstudy\"></canvas>
</body>
</html>

phpstudy.js的代码如下:

复制代码
代码如下:

/*
* 功能:画布旋转
*/
(function(){
var canvas=null,
context=null,
angle=0;
function resetCanvas(){
canvas=document.getElementById(\"phpstudy\");
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);
});
})();

本文地址:https://www.stayed.cn/item/2360

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。