首页 > Web开发 > 详细

html Canvas动画旋转的小方块;

时间:2015-12-11 11:20:44      阅读:390      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Canvas动画旋转的小方块</title>
    <link rel="stylesheet" href="">
</head>
<body>
    <script>
        window.onload=function(){
            var canvas = document.querySelector("canvas");
            canvas.width = canvas.height = 500
            canvas.style.background = "red"
            if (canvas.getContext) {
                var ext = canvas.getContext("2d");
                var num = 0;
                ext.fillStyle = "#f90";
                ext.translate(canvas.width / 2, canvas.height / 2);
                function rotate() {
                    ext.clearRect(-canvas.width / 2, -canvas.height / 2, canvas.width, canvas.height)
                    num++;
                    ext.save();
                    ext.rotate(num * Math.PI / 180)
                    ext.translate(-50, -50)
                    ext.fillRect(0, 0, 100, 100);
                    ext.restore();
                }
                var t=setInterval(rotate,30);
                canvas.onmouseover=function(){
                    clearInterval(t);
                } 
                canvas.onmouseout=function(){
                    t=setInterval(rotate,30);
                } 
            }
        }
    </script>
    <canvas></canvas>
</body>
</html>

主要是学习了canvas的一些相关api,案例还是很简单的

html Canvas动画旋转的小方块;

原文:http://www.cnblogs.com/jone-chen/p/5038407.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!