canvas getContext()方法
getContext方法可以獲取canvas畫布的繪圖環境。
接受一個引數,當前它的引數值只能是"2d",或許在未來還會有"3d"之類的。
此方法的呼叫物件是canvas元素物件,程式碼如下:
[JavaScript] 純文字檢視 複製程式碼var cvs = document.getElementById("canvas"); var ctx = cvs.getContext("2d");
返回值是CanvasRenderingContext2D類的物件例項。
我們也可以自行擴充套件CanvasRenderingContext2D類,具體可以參閱canvas繪製扇形程式碼例項一章節。
此物件例項中包含了大量的繪圖方法和繪圖屬性。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> window.onload = function () { var cvs = document.getElementById("canvas"); var ctx = cvs.getContext("2d"); ctx.fillStyle = "blue"; ctx.arc(80, 60, 20, 0, 2 * Math.PI); ctx.stroke(); ctx.fill(); } </script> </head> <body> <canvas id="canvas" width="300" height="150"></canvas> </body> </html>
相關文章
- canvas getContext()CanvasContext
- canvas setTransform()方法CanvasORM
- canvas isPointInPath()方法Canvas
- canvas arc()方法詳解Canvas
- canvas closePath()方法詳解Canvas
- View.getContext() 裡的小祕密ViewContext
- Canvas之translate、scale、rotate、skew方法講解!Canvas
- Android 開發中 getContext 和 getActivity的區別AndroidContext
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- 每日一問:View.getContext() 的返回一定是 Activity 麼?ViewContext
- 每日一問:View.getContext() 一定會返回 Activity 物件麼?ViewContext物件
- canvasCanvas
- flutter自定義View(CustomPainter) 之 canvas的方法總結FlutterViewAICanvas
- html5中的canvas繪製橢圓的方法HTMLCanvas
- 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