<canvas> 除了全域屬性外,另有定義幾個屬性 (attribute)
- width
- height
此外, <canvas> 的文件物件模型 (DOM) 介面為 HTMLCanvasElement ,另有定義屬性與方法如下
- width
- height
- toDataURL()
- toBlob()
- getContext()
繪製圖形或製作動畫便是利用 getContext() 回傳的 CanvasRenderingContext2D 物件,然後以 JavaScript 程式設計繪製路徑或動畫進行模式。舉例如下
<!DOCTYPE html> <html> <head> <title>HTML 5 DEMO</title> <style> header, nav, section, article, footer { display: block; } header, nav, section, article, footer { color: white; } header, footer { text-align: center; width: 100%; } header { background-color: red; font-size: 36px; font-weight: bold; } nav { position: fixed; top: 40px; right: 25px; background-color: blue; width: 100px; } section { width: 86%; background-color: gray; padding: 20px; margin: 20px; } footer { background-color: green; font-size: 10px; } </style> <script> function run() { var d = document.getElementsByTagName("canvas"); var c = d[0].getContext("2d"); c.fillStyle = "rgb(100, 100, 100)"; c.fillRect(10, 10, 160, 160); c.fillStyle = "rgb(0, 0, 0)"; c.fillRect(30, 30, 120, 120); } </script> </head> <body onload="run();"> <header> header </header> <nav> <ul> <li>nav 1</li> <li>nav 2</li> </ul> </nav> <section> <article> <canvas id="demo" width="180" height="180"> </article> </section> <footer> <p> footer © 2011 </p> </footer> </body> </html> <!-- 《程式語言教學誌》的範例程式 http://pydoing.blogspot.com/ 檔名:canvastest.html 功能:示範 HTML 5 標記語言 作者:張凱慶 時間:西元 2011 年 12 月 -->
此例, <track> 放在 <video> 中
<section> <article> <canvas id="demo" width="180" height="180"> </article> </section>
利用 Firefox 開啟如下
中英文術語對照 | |
---|---|
元素 | element |
屬性 | attribute |
文件物件模型 | DOM |
您可以繼續參考
JavaScript 範例
- <canvas> (HTMLCanvasElement)
- CanvasRenderingContext2D
- CanvasRenderingContext2D.canvas
- CanvasRenderingContext2D.save()
- CanvasRenderingContext2D.restore()
- CanvasRenderingContext2D.scale()
- CanvasRenderingContext2D.rotate()
- CanvasRenderingContext2D.translate()
- CanvasRenderingContext2D.transform()
- CanvasRenderingContext2D.setTransform()
- CanvasRenderingContext2D.globalAlpha
- CanvasRenderingContext2D.globalCompositeOperation
- CanvasRenderingContext2D.strokeStyle
- CanvasRenderingContext2D.fillStyle
- CanvasRenderingContext2D.createLinearGradient()
- CanvasRenderingContext2D.createRadialGradient()
- CanvasGradient 物件
- CanvasRenderingContext2D.createPattern()
- CanvasRenderingContext2D.lineWidth
- CanvasRenderingContext2D.lineCap
- CanvasRenderingContext2D.lineJoin
- CanvasRenderingContext2D.miterLimit
- CanvasRenderingContext2D.shadowOffsetX
- CanvasRenderingContext2D.shadowOffsetY
- CanvasRenderingContext2D.shadowBlur
- CanvasRenderingContext2D.shadowColor
- CanvasRenderingContext2D.clearRect()
- CanvasRenderingContext2D.fillRect()
- CanvasRenderingContext2D.strokeRect()
- CanvasRenderingContext2D.beginPath()
- CanvasRenderingContext2D.closePath()
- CanvasRenderingContext2D.moveTo()
- CanvasRenderingContext2D.lineTo()
- CanvasRenderingContext2D.quadraticCurveTo()
- CanvasRenderingContext2D.bezierCurveTo()
- CanvasRenderingContext2D.arcTo()
- CanvasRenderingContext2D.rect()
- CanvasRenderingContext2D.arc()
- CanvasRenderingContext2D.fill()
- CanvasRenderingContext2D.stroke()
- CanvasRenderingContext2D.drawSystemFocusRing()
- CanvasRenderingContext2D.drawCustomFocusRing()
- CanvasRenderingContext2D.scrollPathIntoView()
- CanvasRenderingContext2D.clip()
- CanvasRenderingContext2D.isPointInPath()
- CanvasRenderingContext2D.font
- CanvasRenderingContext2D.textAlign
- CanvasRenderingContext2D.textBaseline
- CanvasRenderingContext2D.fillText()
- CanvasRenderingContext2D.strokeText()
- CanvasRenderingContext2D.measureText()
- CanvasRenderingContext2D.drawImage()
- CanvasRenderingContext2D.createImageData()
- CanvasRenderingContext2D.getImageData()
- CanvasRenderingContext2D.putImageData()
內嵌元素
相關目錄
HTML 5 快速導覽
HTML, CSS 教材
首頁
參考資料
http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#the-canvas-element
http://www.w3.org/TR/2011/WD-html5-20110525/the-canvas-element.html#the-canvas-element
1 則留言:
Well Written Beautiful Post!
here is my post related to html canvas.
you may like.
Convert HTML to Canvas
張貼留言