canvas實現的多邊形程式碼例項
分享一段簡單的程式碼例項,它利用canvas實現了多邊形效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> var colBlock = [{ p: [{ x: 50, y: 50 }, { x: 200, y: 50 }, { x: 230, y: 100 }, { x: 50, y: 200 }], color: 'red' }, { p: [{ x: 200, y: 50 }, { x: 230, y: 100 }, { x: 400, y: 200 }], color: 'cadetblue' }, { p: [{ x: 200, y: 50 }, { x: 400, y: 200 }, { x: 400, y: 50 }], color: 'red' }, { p: [{ x: 231, y: 100 }, { x: 50, y: 200 }, { x: 250, y: 110 }], color: 'cadetblue' }, { p: [{ x: 50, y: 200 }, { x: 50, y: 350 }, { x: 200, y: 350 }, { x: 230, y: 300 }], color: 'red' }, { p: [{ x: 200, y: 350 }, { x: 400, y: 350 }, { x: 400, y: 200 }], color: 'red' }, { p: [{ x: 200, y: 350 }, { x: 230, y: 300 }, { x: 400, y: 200 }], color: 'cadetblue' }, { p: [{ x: 50, y: 200 }, { x: 230, y: 300 }, { x: 250, y: 290 }], color: 'cadetblue' }] window.onload = function() { var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); for (var i = 0; i < colBlock.length; i++) { render(colBlock<i>, cxt) } } function render(col, cxt) { cxt.beginPath(); cxt.moveTo(col.p[0].x, col.p[0].y); for (var i = 1; i < col.p.length; i++) { cxt.lineTo(col.p<i>.x, col.p<i>.y); } cxt.closePath(); cxt.fillStyle = col.color; cxt.fill(); } </script> </head> <body> <canvas id="canvas" width="650" height="550"></canvas> </body> </html>
相關文章
- html5利用canvas圓形和多邊形程式碼例項HTMLCanvas
- canvas實現的動態心形效果程式碼例項Canvas
- canvas實現的圓形鐘錶效果程式碼例項Canvas
- CSS3實現的平行四邊形程式碼例項CSSS3
- css3實現平行四邊形程式碼例項CSSS3
- canvas實現的刮刮樂程式碼例項Canvas
- canvas繪製實心圓形程式碼例項Canvas
- canvas實現的美猴王程式碼例項Canvas
- css六邊形效果程式碼例項CSS
- canvas實現的驗證碼效果程式碼例項Canvas
- canvas實現的變幻線程式碼例項Canvas線程
- canvas實現的千輪眼程式碼例項Canvas
- canvas實現的鋸齒效果程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- 利用canvas實現的驗證碼效果程式碼例項Canvas
- canvas實現的貪吃蛇遊戲程式碼例項Canvas遊戲
- canvas實現的雪花飄落效果程式碼例項Canvas
- canvas實現的藍色雨滴效果程式碼例項Canvas
- canvas實現的圖片縮放程式碼例項Canvas
- canvas實現的彈力小球效果程式碼例項Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- canvas實現煙花燃放效果程式碼例項Canvas
- css實現半透明邊框程式碼例項CSS
- canvas實現的隨機生成漂浮小球程式碼例項Canvas隨機
- canvas實現的簡單餅狀圖程式碼例項Canvas
- canvas實現的七巧板效果程式碼例項Canvas
- canvas實現的簡單塗鴉板程式碼例項Canvas
- canvas實現的五子棋程式碼例項Canvas
- canvas實現的七色板程式碼例項Canvas
- canvas實現的簡單畫板效果程式碼例項Canvas
- css3實現的多重邊框程式碼例項CSSS3
- angularJS結合canvas實現的畫圖程式碼例項AngularJSCanvas
- canvas實現的賽貝爾曲線效果程式碼例項Canvas
- canvas實現的小球四壁碰撞效果程式碼例項Canvas
- canvas刮刮樂程式碼例項Canvas
- canvas實現的旋轉的太極圖效果程式碼例項Canvas
- canvas實現文字線性漸變效果程式碼例項Canvas
- CSS3繪製平行四邊形程式碼例項CSSS3