HTML5 Canvas ( 事件互動, 點擊事件為例 ) isPointInPath

來源:互聯網
上載者:User

標籤:[]   client   滑鼠   get   tle   title   etc   pre   random   

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>canvas</title>    <script type="text/javascript" src="../js/jQuery.js"></script>    <style type="text/css">        #canvas{            width: 7rem;            margin: .25rem 0 0 1.5rem;            border: 1px solid black;            display: block;        }    </style></head><body>     <canvas id="canvas" width="1000" height="600"></canvas></body></html><script type="text/javascript">    /**     * rem 布局初始化     */    $(‘html‘).css(‘font-size‘, $(window).width()/10);    /**     * 擷取 canvas 畫布     * 擷取 canvas 繪圖上下文環境     */    var canvas = $(‘#canvas‘)[0];    var cxt = canvas.getContext(‘2d‘);    var balls = [];        /**     * 事件互動, 點擊事件為例     * isPointInPath(橫座標, 縱座標)     */    for(var i = 0; i < 10; i++){        var ball = {            X: Math.random()*canvas.width,            Y: Math.random()*canvas.height,            R: Math.random()*50 + 20        }        balls[i] = ball;    }        draw();    $("#canvas").click(function(){        //標準的擷取滑鼠點擊相對於canvas畫布的座標公式        var x = event.pageX - canvas.getBoundingClientRect().left;        var y = event.pageY - canvas.getBoundingClientRect().top;        for(var i = 0; i < balls.length; i++){            cxt.beginPath();            cxt.arc(balls[i].X, balls[i].Y, balls[i].R, 0, Math.PI*2);            if(cxt.isPointInPath(x, y)){                cxt.fillStyle = "red";                cxt.fill();            }        }    });        function draw(){        cxt.fillStyle = "blue";        for(var i = 0; i < balls.length; i++){            cxt.beginPath();            cxt.arc(balls[i].X, balls[i].Y, balls[i].R, 0, Math.PI*2);            cxt.fill();        }    }</script>

 

HTML5 Canvas ( 事件互動, 點擊事件為例 ) isPointInPath

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.