html5 canvas學習

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   java   ar   2014   div   

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title></head><body>    <canvas width ="800" height="500" style=" background:yellow" id="canvas"></canvas>    <script type="text/javascript">        /*            1:擷取dom 對象            2:擷取繪圖對象            3:進行操作                如畫線 畫圓 方形 等        */        //擷取dom 對象        var canvas = document.getElementById(‘canvas‘);        //擷取繪圖對象        var cxt = canvas.getContext(‘2d‘);        //設定畫筆的寬度        //如畫一條線 moveTo(x,y)表示你要畫的開始位置        // lineTo(x,y) 表示你要畫到哪裡,也就是拉到哪裡        //stroke()表示執行 moveTo和lineTo命令        cxt.moveTo(20, 20);        cxt.lineTo(100, 20);        cxt.stroke();        //closePath 封閉路徑 表示畫的終點封閉        cxt.closePath();        //畫一個空心圓形    凡是路徑圖形必須有開起路徑,畫完了要封閉新路徑        //beginPath表示開啟一個新路徑        cxt.beginPath();        //arc(x,y,半徑,角度範圍1,角度範圍2,順或逆)是一個畫圓 曲線等方法        cxt.arc(200, 200, 50, 0, 360, false);        cxt.stroke();        cxt.closePath();        //畫一個實心圓形        cxt.beginPath();        //設定填充的顏色        cxt.fillStyle = "rgb(255,0,0)";        cxt.arc(200, 100, 50, 0, 360, false)        cxt.fill();        cxt.stroke();        cxt.closePath();        /*畫一個矩形 矩形方法不屬於路徑圖形所以不需          封閉路徑以及開啟路徑,但是由於有時候需要          填充顏色或者其它方法,所以還是畫一個圖形          開啟一個:               cxt.beginPath();                      cxt.closePath();                 */        //rect(x,y,寬,高)        cxt.beginPath();        cxt.rect(300, 20, 100, 100);        cxt.stroke();        cxt.closePath();        //直接畫        cxt.strokeRect(300, 150, 100, 100);        //實心矩形         cxt.beginPath();        cxt.rect(300, 350, 100, 100);        cxt.fill();        cxt.closePath();        //其他方法 這種方法最方便        cxt.fillRect(100, 400, 100, 100);        //設定文字        cxt.font = "80px 黑體";        cxt.fillText("無兄弟,不編程", 20, 230);        //空心字,沒有填充功能        cxt.strokeText("無兄弟,不籃球", 20, 350);        //畫圖 不同瀏覽器的支援不一樣        var img = new Image();        img.src = "bl.gif";        //drawImage(要畫的對象,x,y,寬,高)        cxt.drawImage(img, 20, 37, 230, 306);        //畫三角形        cxt.beginPath();        cxt.moveTo(300, 500);        cxt.lineTo(400, 350);        cxt.lineTo(350, 450);        cxt.closePath();        cxt.stroke();        //旋轉圖片        //設定旋轉環境 save表示把當前設定好的環境儲存好        cxt.save();        //圖片設計旋轉        //設定旋轉角度         cxt.rotate(30*Math.PI/180);        //旋轉一個線段        cxt.beginPath();        cxt.moveTo(20, 20);        cxt.lineTo(20, 100);        cxt.stroke();        cxt.closePath();        //建立旋轉空間        cxt.restore();    </script></body></html>


 

html5 canvas學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.