<canvas id = "mycanvas" width = "100" height = "100"></canvas>
Id − ID代表在文件物件模型(DOM)canvas元素的識別符號。
Width − 表示canvas的寬度。
Height − 表示canvas的高度。
<html> <head> <style> #mycanvas{border:1px solid red;} </style> </head> <body> <canvas id = "mycanvas" width = "300" height = "200"></canvas> </body> </html>
<!DOCTYPE HTML> <html> <body> <canvas id = "mycanvas" width = "600" height = "300"></canvas> <script> var canvas = document.getElementById('mycanvas'); var context = canvas.getContext('2d'); context.font = '20pt Calibri'; context.fillStyle = 'green'; context.fillText('Welcome to Yiibai Tutorial', 100, 100); </script> </body> </html>
這將產生以下結果 -
<!DOCTYPE html> <html> <canvas id = 'my_canvas'></canvas> <script> var canvas = document.getElementById('my_canvas'); var gl = canvas.getContext('experimental-webgl'); gl.clearColor(0.9,0.9,0.8,1); gl.clear(gl.COLOR_BUFFER_BIT); </script> </html>
這將產生以下結果 -