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>
程式碼執行效果截圖如下:
上面程式碼對矩形路徑進行描邊和填充。
相關文章
- SVG <rect> 繪製矩形SVG
- appium tap 報錯:outside of element rectAPPIDE
- OpenCV(cv::Point、cv::Rect、cv::Mat)OpenCV
- 使用joinjs繪製流程圖(三)-Rect物件屬性JS流程圖物件
- Unity 之 UGUI Scroll Rect滾動矩形元件詳解UnityUGUI元件
- canvasCanvas
- 飛機大戰中rect屬性儲存小數值解釋
- WPF C# create canvas and draw ellipse in canvasC#Canvas
- canvas strokeStyleCanvas
- canvas lineTo()Canvas
- canvas strokeRect()Canvas
- canvas createRadialGradient()Canvas
- canvas createLinearGradient()Canvas
- canvas arc()Canvas
- canvas stroke()Canvas
- canvas fill()Canvas
- canvas fillStyleCanvas
- canvas setTransform()CanvasORM
- canvas transform()CanvasORM
- 初探canvasCanvas
- canvas closePath()Canvas
- canvas moveTo()Canvas
- canvas beginPath()Canvas
- canvas getContext()CanvasContext
- canvas getImageData()Canvas
- canvas isPointInPath()Canvas
- canvas putImageData()Canvas
- canvas drawImage()Canvas
- canvas clip()Canvas
- canvas arcTo()Canvas
- canvas createPattern()Canvas
- canvas createImageData()Canvas
- canvas restore()CanvasREST
- canvas translate()Canvas
- canvas rotate()Canvas
- canvas scale()Canvas
- canvas measureText()Canvas
- canvas fillText()Canvas