一個給力的html5 畫多邊形的例子

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   div   sp   

只需改變參數就能畫出你想要的多邊形,代碼簡單!不得不驚歎canvas的強大!

代碼奉上

<!doctype html><html><head><meta charset="gb2312"><title>畫圖</title></head><body>  <canvas id = "myCanvas" width = ‘500‘ height = ‘500‘>Canvas畫線技巧</canvas><script>    var myCanvas = document.getElementById("myCanvas");    var context =  myCanvas.getContext("2d");        function drawPath(x, y, n, r)        {                var i,ang;                ang = Math.PI*2/n //旋轉的角度                context.save();//儲存狀態                context.fillStyle =‘rgba(255,0,0,.3)‘;//填充紅色,半透明                context.strokeStyle =‘hsl(120,50%,50%)‘;//填充綠色                context.lineWidth = 1;//設定線寬                context.translate(x, y);//原點移到x,y處,即要畫的多邊形中心                context.moveTo(0, -r);//據中心r距離處畫點                context.beginPath();                for(i = 0;i < n; i ++)                {                        context.rotate(ang)//旋轉                        context.lineTo(0, -r);//據中心r距離處連線                }                context.closePath();                context.stroke();                context.fill();                context.restore();//返回原始狀態        }        drawPath(100, 100, 3, 40)//在100,100處畫一個半徑為40的三邊形        drawPath(200, 100, 4, 40)//在200,100處畫一個半徑為40的四角形        drawPath(300, 100, 5, 40)//在300,100處畫一個半徑為40的五邊形        drawPath(100, 200, 6, 40)//在100,200處畫一個半徑為40的六邊形        drawPath(200, 200, 7, 40)//在100,200處畫一個半徑為40的七邊形        drawPath(300, 200, 7, 40)//在300,200處畫一個半徑為40的八邊形</script></body></html>

 

一個給力的html5 畫多邊形的例子

聯繫我們

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