canvas繪製圓角矩形程式碼例項
分享一段簡單的程式碼例項,它實現了繪製圓角矩形的功能。
非常適合初學者瞭解相關方法和屬性的使用,程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> * { padding: 0; margin: 0; } </style> </head> <body bgcolor="#00aeff"> <script type="text/javascript"> var canvas = document.createElement("canvas"); var canW = canH = 600; canvas.width = canW; canvas.height = canH; document.body.appendChild(canvas); var ctx = canvas.getContext("2d"); var w = h = 300; //矩形的寬高 var x = canW/2 - w/2; //開始的x軸座標 var y = canH/2 - h/2; //開始的y軸座標 var r = 15; //圓角半徑 var bdWidth = 8; //邊框寬度 var bdColor = "f00000"; //邊框的顏色 var bgcolor = "#ffffff"; //矩形背景的顏色 drawRoundedRect(x,y,w,h,r,bdWidth,bdColor,bgcolor); function drawRoundedRect(x,y,w,h,r,bdWidth,bdColor,bgcolor){ ctx.beginPath(); ctx.moveTo(x+r,y); ctx.lineWidth = bdWidth; ctx.strokeStyle = bdColor; ctx.fillStyle = bgcolor; ctx.arcTo(x+w,y,x+w,y+h,r); ctx.arcTo(x+w,y+h,x,y+h,r); ctx.arcTo(x,y+h,x,y,r); ctx.arcTo(x,y,x+w,y,r); ctx.stroke(); ctx.fill(); ctx.closePath(); } </script> </body> </html>
相關文章
- canvas 繪製圓角矩形Canvas
- 微信小程式 canvas圓角矩形的繪製微信小程式Canvas
- canvas繪製橢圓效果程式碼例項Canvas
- canvas繪製矩形並填充顏色程式碼例項Canvas
- HTML5利用canvas繪製矩形程式碼例項HTMLCanvas
- SVG 繪製圓角矩形SVG
- canvas 繪製矩形缺角Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- canvas繪製實心圓形程式碼例項Canvas
- canvas繪製網狀圓圈程式碼例項Canvas
- 原來 Canvas 也能直接繪製圓角矩形了Canvas
- canvas繪製五角星程式碼例項Canvas
- canvas繪製扇形程式碼例項Canvas
- canvas繪製星星程式碼例項Canvas
- canvas 繪製雞蛋程式碼例項Canvas
- SVG拖動繪製矩形程式碼例項SVG
- css繪製圓形程式碼例項CSS
- svg繪製半圓程式碼例項SVG
- canvas繪製網格程式碼例項Canvas
- canvas繪製笑臉程式碼例項Canvas
- canvas繪製圓環效果程式碼例項Canvas
- canvas 繪製矩形Canvas
- canvas繪製米字旗程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製夜空小屋效果程式碼例項Canvas
- canvas繪製鐘錶效果程式碼例項Canvas
- canvas繪製熊貓頭像程式碼例項Canvas
- iOS開發_繪製圓角矩形虛線環iOS
- canvas繪製魚吃豆效果程式碼例項Canvas
- canvas繪製雪花飄落效果程式碼例項Canvas
- canvas繪製機器貓頭像程式碼例項Canvas
- canvas繪製貝濟埃曲線程式碼例項Canvas線程
- canvas繪製憤怒小鳥形象程式碼例項Canvas
- canvas繪製矩形框Canvas
- canvas 繪製圓形Canvas
- iOS 繪製圓角iOS