当前位置:编程学习 > html/css >>

画布旋转——HTML5之特效

translate.html内容如下:


[html] 
<!DOCTYPE HTML>  
<html> 
<head> 
<script type="text/javascript" src="jquery-1.7.2.min.js"></script> 
<script type="text/javascript" src="translate.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
 
}); 
</script> 
</head> 
<body> 
<canvas id="易做图"></canvas> 
</body> 
</html>  

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="translate.js"></script>
<script type="text/javascript">
$(document).ready(function(){

});
</script>
</head>
<body>
<canvas id="易做图"></canvas>
</body>
</html>

translate.js代码如下:


[javascript]
(function(){ 
    var canvas=null, 
    context=null, 
    angle=0; 
    function resetCanvas(){ 
        canvas=document.getElementById("易做图"); 
        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); 
    }); 
})(); 

(function(){
 var canvas=null,
 context=null,
 angle=0;
 function resetCanvas(){
  canvas=document.getElementById("易做图");
  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);
 });
})();
用支持HMTL5的浏览器打开translate.html就可以看到一个旋转的红色方块

补充:web前端 , HTML 5 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,