码迷,mamicode.com
首页 > Web开发 > 详细

canvas-a10isPointPath2.html

时间:2015-11-23 18:49:45      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <canvas id="canvas" style="margin:0 auto;border:1px #ddd solid">
        The current browser does not support Canvas, can replace the browser a try!
    </canvas>

    <script>
        var canvas = document.getElementById(canvas);
        var context = canvas.getContext(2d);
        var balls = [];

        window.onload = function(){
            // var canvas = document.getElementById(‘canvas‘);

            canvas.width = 1024;
            canvas.height = 768;

            if(canvas.getContext(2d)){
                // var context = canvas.getContext(‘2d‘);

                for(var i=0;i<10;i++){
                    var aBall = {
                        x : Math.random()*canvas.width,
                        y : Math.random()*canvas.height,
                        r : Math.random()*50+20
                    };
                    balls[i] = aBall;
                }

                draw();
                canvas.addEventListener("mousemove",detect);

            }else{
                alert(当前游览器不支持Canvas,请更换游览器后再试!);
            }
        }


        function draw(x,y){
            context.clearRect(0,0,canvas.width,canvas.height);

            for(var i = 0;i<balls.length;i++){
                context.beginPath();
                    context.arc(balls[i].x,balls[i].y,balls[i].r,0,Math.PI*2);
                
                if(context.isPointInPath(x,y))
                    context.fillStyle = "red";
                else
                    context.fillStyle = "#058";
                context.fill();
            }
        }

        function detect(event){
            var x = event.clientX - canvas.getBoundingClientRect().left;
            var y = event.clientY - canvas.getBoundingClientRect().top;

            draw(x,y)
        }

    </script>
</body>
<script>
    /*总结
        
        //判断事件是不是在canvas内
        context.isPointInPath(x,y)

    */
</script>
</html>

 

canvas-a10isPointPath2.html

标签:

原文地址:http://www.cnblogs.com/cynthia-wuqian/p/4989156.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!