Canvas标签笔记 (1)
参考自https://developer.mozilla.org/en/Canvas_tutorial
<canvas>是HTML5新增加的标签,提供一些简单的方法来完成基本绘图和动画效果,由于标准问题,<canvas>的浏览器支持还不好,至少IE是不支持的(最新的IE9没了解过...),已知支持<canvas>的浏览器有Firefox 1.5+,Opera 9,Safari,Chrome.
一 基础知识
<canvas id="tutorial" width="150" height="150"></canvas>
<canvas id="stockGraph" width="150" height="150"> current stock price: $3.15 +0.15</canvas><canvas id="clock" width="150" height="150"> <img src="images/clock.png" width="150" height="150" alt="Canvas标签札记 (1)"/></canvas>
var canvas = document.getElementById('tutorial');var ctx = canvas.getContext('2d');
var canvas = document.getElementById('tutorial');if (canvas.getContext){ var ctx = canvas.getContext('2d'); //绘图代码} else { //不支持canvas时的代码}
<html> <head> <title>Canvas tutorial</title> <script type="text/javascript"> function draw(){ var canvas = document.getElementById('tutorial'); if (canvas.getContext){ var ctx = canvas.getContext('2d'); } } </script> <style type="text/css"> canvas { border: 1px solid black; } </style> </head> <body onload="draw();"> <canvas id="tutorial" width="150" height="150"></canvas> </body></html>
function draw() { var canvas = document.getElementById("canvas"); if (canvas.getContext) { var ctx = canvas.getContext("2d"); ctx.fillStyle = "rgb(200,0,0)"; ctx.fillRect (10, 10, 55, 50); ctx.fillStyle = "rgba(0, 0, 200, 0.5)"; ctx.fillRect (30, 30, 55, 50); }}