canvas rect()
rect方法可以建立一個矩形路徑,不會真正繪製出圖案。
需要使用stroke或fill方法在畫布上實際地繪製矩形。
語法結構:
[JavaScript] 純文字檢視 複製程式碼context.rect(x,y,width,height);
引數解析:
(1).x:必需,矩形左上角的x軸座標。
(2).y:必需,矩形左上角的y軸座標。
(3).width:必需,矩形的寬度。
(4).height:必需,矩形的高度。
瀏覽器相容:
(1).IE9+瀏覽器支援此方法。
(2).edge瀏覽器支援此方法。
(3).谷歌瀏覽器支援此方法。
(4).火狐瀏覽器支援此方法。
(5).Opera瀏覽器支援此方法。
(6).Safari瀏覽器支援此方法。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> window.onload=function(){ let c=document.getElementById("ant"); let ctx=c.getContext("2d"); ctx.rect(20,20,150,70); ctx.stroke(); } </script> </head> <body> <canvas id="ant"></canvas> </body> </html>
程式碼執行效果截圖如下:
上述程式碼對矩形路徑進行描邊。
[HTML] 純文字檢視 複製程式碼執行程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> window.onload=function(){ let c=document.getElementById("ant"); let ctx=c.getContext("2d"); ctx.rect(20,20,150,70); ctx.strokeStyle="red"; ctx.fillStyle="blue"; ctx.stroke(); ctx.fill(); } </script> </head> <body> <canvas id="ant"></canvas> </body> </html>
程式碼執行效果截圖如下:
上面程式碼對矩形路徑進行描邊和填充。
相關文章
- HTML5 Canvas ( 矩形的繪製 ) rect, strokeRect, fillRectHTMLCanvas
- SVG <rect> 繪製矩形SVG
- Unity UGUI——Rect Transform包(Anchors)UnityUGUIORM
- OpenCV(cv::Point、cv::Rect、cv::Mat)OpenCV
- appium tap 報錯:outside of element rectAPPIDE
- area標籤circle/rect/poligon座標Go
- Unity 之 UGUI Scroll Rect滾動矩形元件詳解UnityUGUI元件
- UIScrollView滾動到並完全顯示特定的RectUIView
- 使用joinjs繪製流程圖(三)-Rect物件屬性JS流程圖物件
- canvasCanvas
- 飛機大戰中rect屬性儲存小數值解釋
- canvas clearRect()Canvas
- canvas toBlob()Canvas
- canvas toDataURL()Canvas
- canvas stroke()Canvas
- canvas fill()Canvas
- canvas fillRect()Canvas
- canvas translate()Canvas
- canvas rotate()Canvas
- canvas scale()Canvas
- canvas fillText()Canvas
- canvas createPattern()Canvas
- canvas createImageData()Canvas
- canvas restore()CanvasREST
- canvas save()Canvas
- canvas putImageData()Canvas
- canvas drawImage()Canvas
- canvas clip()Canvas
- canvas arcTo()Canvas
- canvas isPointInPath()Canvas
- canvas arc()Canvas
- canvas getImageData()Canvas
- canvas fillStyleCanvas
- canvas closePath()Canvas
- canvas beginPath()Canvas
- canvas strokeRect()Canvas
- canvas strokeStyleCanvas
- canvas setTransform()CanvasORM