HTML 5深入淺出教學(xué)篇之八
介紹
HTML 5: 畫布(canvas)之繪制圖形
畫布 Demo - 畫布的基本概念及 Demo,canvas.getContext(), CanvasRenderingContext2D, canvas.width, canvas.height, canvas.toDataURL()
在畫布上繪制矩形 - canvas.getContext(), fillRect(), fillStyle, lineWidth, strokeStyle, strokeRect(), clearRect()
在畫布上繪制弧線(以路徑的方式)- beginPath(), arc(), fill(), stroke(), moveTo(), arcTo(), isPointInPath()
在畫布上繪制曲線(以路徑的方式)- quadraticCurveTo(), bezierCurveTo()
在畫布上繪制直線(以路徑的方式)- lineWidth, beginPath(), stroke(), moveTo(), lineTo(), lineCap, lineJoin, miterLimit, closePath()
在畫布上繪制矩形(以路徑的方式)- rect()
示例
1、畫布 Demo | canvas.getContext(), CanvasRenderingContext2D, canvas.width, canvas.height, canvas.toDataURL()
canvas/demo.html
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>畫布 Demo</title>
- </head>
- <body>
- <canvas id="canvas" width="320" height="240" style="background-color: rgb(222, 222, 222)">
- 您的瀏覽器不支持 canvas 標(biāo)簽
- </canvas>
- <br/>
- <button type="button" onclick="demo();">Demo</button>
- <br />
- <img id="img" alt="" src="" />
- <script type="text/javascript">
- var canvas = document.getElementById('canvas')
- if (canvas.getContext) {
- alert("您的瀏覽器支持 canvas 標(biāo)簽");
- } else {
- alert("您的瀏覽器不支持 canvas 標(biāo)簽");
- }
- /*
- * canvas 標(biāo)簽 - 畫布標(biāo)簽
- * getContext("2d") - 獲取畫布標(biāo)簽上的 2D 上下文對象(CanvasRenderingContext2D 對象)
- * width - 畫布的寬
- * height - 畫布的高
- * canvas.toDataURL(type) - 返回畫布數(shù)據(jù),默認類型為 image/png
- * type - 指定返回畫布數(shù)據(jù)的類型,比如可以指定為 image/jpeg,默認類型為 image/png
- *
- * CanvasRenderingContext2D - 畫布的 2D 上下文對象,其擁有多種繪制圖像的方法
- * canvas - 上下文所對應(yīng)的畫布
- */
- var ctx = canvas.getContext('2d');
- alert(ctx.canvas.id);
- function demo() {
- ctx.fillRect(20, 20, 100, 100);
- alert("width: " + canvas.width.toString());
- alert("height: " + canvas.height.toString());
- alert(canvas.toDataURL("image/jpeg"));
- alert(canvas.toDataURL()); // image/png
- document.getElementById("img").src = canvas.toDataURL();
- }
- </script>
- </body>
- </html>
2、繪制矩形 | canvas.getContext(), fillRect(), fillStyle, lineWidth, strokeStyle, strokeRect(), clearRect()
canvas/shape/rectangle.html
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>在 canvas 上繪制矩形的 demo</title>
- </head>
- <body>
- <canvas id="canvas" width="300" height="360" style="background-color: rgb(222, 222, 222)">
- 您的瀏覽器不支持 canvas 標(biāo)簽
- </canvas>
- <br/>
- <button type="button" onclick="drawIt();">在畫布上繪制一些矩形</button>
- <button type="button" onclick="clearIt();">清除畫布</button>
- <script type="text/javascript">
- var canvas = document.getElementById('canvas')
- if (canvas.getContext) {
- alert("您的瀏覽器支持 canvas 標(biāo)簽");
- } else {
- alert("您的瀏覽器不支持 canvas 標(biāo)簽");
- }
- /*
- * canvas.getContext("2d") - 獲取畫布標(biāo)簽上的 2D 上下文對象(HTML DOM CanvasRenderingContext2D 對象),其擁有多種繪制圖像的方法。
- */
- var ctx = canvas.getContext('2d');
- function drawIt() {
- clearIt();
- /*
- * context.fillRect(x, y, w, h) - 繪制一個有填充色的矩形,默認填充色為 0x000000
- * x - 矩形左上角的 x 坐標(biāo)
- * y - 矩形左上角的 y 坐標(biāo)
- * w - 矩形的寬
- * h - 矩形的高
- */
- ctx.fillRect(0, 0, 100, 100);
- /*
- * context.fillStyle - 指定填充色的顏色值
- *
- * 顏色值示例如下:
- * Color Name - "green"
- * #rgb - "#0f0"
- * #rrggbb = "#00ff00"
- * rgb(0-255, 0-255, 0-255) - rgb(0, 255, 0)
- * rgb(0.0%-100.0%, 0.0%-100.0%, 0.0%-100.0%) - rgb(0%, 100%, 0%)
- * rgba(0-255, 0-255, 0-255, 0.0-1.0) - rgb(0, 255, 0, 1)
- * rgba(0.0%-100.0%, 0.0%-100.0%, 0.0%-100.0%, 0.0-1.0) - rgb(0%, 100%, 0%, 1)
- */
- ctx.fillStyle = "#0f0";
- ctx.fillRect(120, 0, 100, 50);
- /*
- * context.lineWidth - 筆劃的寬度,默認值是 1.0。
- * 注意看本 Demo,筆劃的寬度為 10,可以明顯的看出其中心線為筆劃的路徑,畫布外的圖像不予顯示
- * context.strokeStyle - 指定筆劃的顏色值
- * context.strokeRect(x, y, w, h) - 繪制一個不填充的矩形
- * x - 矩形左上角的 x 坐標(biāo)
- * y - 矩形左上角的 y 坐標(biāo)
- * w - 矩形的寬
- * h - 矩形的高
- */
- ctx.lineWidth = 10;
- ctx.strokeStyle = "rgb(0, 0, 0)";
- ctx.strokeRect(0, 120, 100, 100);
- // 繪制一個填充色半透明的矩形
- ctx.fillStyle = "rgba(0, 255, 0, 0.3)";
- ctx.fillRect(0, 240, 100, 100);
- }
- function clearIt() {
- /*
- * context.clearRect(x, y, w, h) - 將指定的矩形區(qū)域上的圖像全部清除
- */
- ctx.clearRect(0, 0, 300, 360);
- ctx.fillStyle = "Black";
- ctx.strokeStyle = "Black";
- ctx.lineWidth = 1;
- }
- </script>
- </body>
- </html>
#p#
3、路徑方式繪制 - 弧線 | beginPath(), arc(), fill(), stroke(), moveTo(), arcTo(), isPointInPath()
canvas/shape/path/arc.html
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>以路徑的方式在 canvas 上繪制圓和弧的 demo</title>
- </head>
- <body>
- <img alt="" src="arcTo.png" />
- <br/>
- <canvas id="canvas" width="260" height="360" style="background-color: rgb(222, 222, 222)">
- 您的瀏覽器不支持 canvas 標(biāo)簽
- </canvas>
- <br />
- <button type="button" onclick="drawIt();">在畫布上繪制一些圓和弧</button>
- <button type="button" onclick="clearIt();">清除畫布</button>
- <script type="text/javascript">
- var ctx = document.getElementById('canvas').getContext('2d');
- function drawIt() {
- clearIt();
- /*
- * context.beginPath() - 準(zhǔn)備繪制一條路徑
- *
- * context.arc(x, y, radius, startRadian, endRadian, anticlockwise) - 根據(jù)指定的參數(shù)繪制一條弧線
- * x - 弧線的中心點的 x 坐標(biāo)
- * y - 弧線的中心點的 x 坐標(biāo)
- * radius - 弧線的半徑
- * startRadian - 弧線起始點的弧度(以 X 軸正半軸的三點鐘方向為弧度 0)
- * endRadian - 弧線結(jié)束點的弧度(以 X 軸正半軸的三點鐘方向為弧度 0)
- * anticlockwise - 是否以逆時針方向繪制路徑
- *
- * context.fill() - 使用當(dāng)前的顏色或漸變色等來填充當(dāng)前路徑的內(nèi)部
- *
- * context.stroke() - 繪制當(dāng)前路徑
- *
- * context.isPointInPath(x, y) - 判斷指定的點是否在當(dāng)前路徑內(nèi)
- */
- // 繪制一個以黑色為填充色的圓形
- ctx.beginPath();
- ctx.arc(50, 50, 50, 0, 2 * Math.PI, true);
- ctx.fill();
- alert(ctx.isPointInPath(50, 50));
- // 繪制一個以半透明藍色為填充色的圓形
- ctx.beginPath();
- ctx.fillStyle = "rgba(0, 0, 255, 0.5)";
- ctx.arc(150, 50, 50, 0, 2 * Math.PI, true);
- ctx.fill();
- ctx.lineWidth = 10;
- // 演示按順時針方向繪制弧線(以 X 軸正半軸的三點鐘方向為弧度 0)
- ctx.beginPath();
- ctx.strokeStyle = "rgb(0, 255, 0)";
- ctx.arc(50, 150, 50, 0, 1.5 * Math.PI, false);
- ctx.stroke();
- // 演示按逆時針方向繪制弧線(以 X 軸正半軸的三點鐘方向為弧度 0)
- ctx.beginPath();
- ctx.strokeStyle = "rgb(0, 255, 0)";
- ctx.arc(150, 150, 50, 0, 1.5 * Math.PI, true);
- ctx.stroke();
- /*
- * context.moveTo(x, y) - 新開一個路徑,并指定路徑的起點
- *
- * context.arcTo(x1, y1, x2, y2, radius) - 通過指定切點和半徑的方式繪制弧線。
- * x1, y1 - 路徑當(dāng)前點與 (x1, y1) 的連接線為弧線起點的切線。詳見圖片 arcTo.png
- * x2, y2 - (x1, y1) 與 (x2, y2) 的連接線為弧線終點的切線,此切點即為弧線的終點。詳見圖片 arcTo.png
- * radius - 弧線半徑
- */
- ctx.beginPath();
- ctx.strokeStyle = "rgb(0, 0, 255)";
- ctx.moveTo(50, 250);
- ctx.arcTo(150, 250, 150, 1000, 50);
- ctx.stroke();
- }
- function clearIt() {
- ctx.clearRect(0, 0, 260, 360);
- ctx.fillStyle = "Black";
- ctx.strokeStyle = "Black";
- ctx.lineWidth = 1;
- }
- </script>
- </body>
- </html>
4、路徑方式繪制 - 曲線 | quadraticCurveTo(), bezierCurveTo()
canvas/shape/path/curve.html
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>以路徑的方式在 canvas 上繪制曲線的 demo</title>
- </head>
- <body>
- <img alt="" src="curve.png" />
- <br/>
- <img alt="" src="curve_quadratic.gif" />
- <br/>
- <img alt="" src="curve_bezier.gif" />
- <br/>
- <canvas id="canvas" width="260" height="300" style="background-color: rgb(222, 222, 222)">
- 您的瀏覽器不支持 canvas 標(biāo)簽
- </canvas>
- <br/>
- <button type="button" onclick="drawIt();">在畫布上繪制一些曲線</button>
- <button type="button" onclick="clearIt();">清除畫布</button>
- <script type="text/javascript">
- var ctx = document.getElementById('canvas').getContext('2d');
- function drawIt() {
- clearIt();
- /*
- * context.quadraticCurveTo(cpX, cpY, x, y) - 以當(dāng)前點為曲線起點,按指定的參數(shù)繪制二次方貝塞爾曲線。見圖 curve.png, curve_bezier.gif
- * cpX - 控制點的 x 軸坐標(biāo)
- * cpY - 控制點的 y 軸坐標(biāo)
- * x - 曲線終點的 x 軸坐標(biāo)
- * y - 曲線終點的 y 軸坐標(biāo)
- */
- ctx.beginPath();
- ctx.moveTo(20, 100);
- ctx.quadraticCurveTo(40, 20, 180, 100);
- ctx.stroke();
- /*
- * context.bezierCurveTo(cpX1, cpY1, cpX2, cpY2, x, y) - 以當(dāng)前點為曲線起點,按指定的參數(shù)繪制三次方貝塞爾曲線。見圖 curve.png, curve_quadratic.gif
- * cpX1 - 和曲線起點相關(guān)連的控制點的 x 軸坐標(biāo)
- * cpY1 - 和曲線起點相關(guān)連的控制點的 y 軸坐標(biāo)
- * cpX2 - 和曲線終點相關(guān)連的控制點的 x 軸坐標(biāo)
- * cpY2 - 和曲線終點相關(guān)連的控制點的 y 軸坐標(biāo)
- * x - 曲線終點的 x 軸坐標(biāo)
- * y - 曲線終點的 y 軸坐標(biāo)
- */
- ctx.beginPath();
- ctx.moveTo(20, 200);
- ctx.bezierCurveTo(0, 160, 160, 120, 180, 200);
- ctx.stroke();
- }
- function clearIt() {
- ctx.clearRect(0, 0, 260, 300);
- }
- </script>
- </body>
- </html>
#p#
5、路徑方式繪制 - 直線 | lineWidth, beginPath(), stroke(), moveTo(), lineTo(), lineCap, lineJoin, miterLimit, closePath()
canvas/shape/path/line.html
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>以路徑的方式在 canvas 上繪制直線的 demo</title>
- </head>
- <body>
- <canvas id="canvas" width="340" height="300" style="background-color: rgb(222, 222, 222)">
- 您的瀏覽器不支持 canvas 標(biāo)簽
- </canvas>
- <br/>
- <button type="button" onclick="drawIt();">在畫布上繪制一些直線</button>
- <button type="button" onclick="clearIt();">清除畫布</button>
- <script type="text/javascript">
- var ctx = document.getElementById('canvas').getContext('2d');
- function drawIt() {
- clearIt();
- ctx.strokeStyle = 'Green';
- /*
- * context.lineWidth - 筆劃的寬度,默認值是 1.0
- */
- ctx.lineWidth = 10;
- /*
- * context.beginPath() - 準(zhǔn)備繪制一條路徑
- * context.stroke() - 繪制當(dāng)前路徑
- * context.moveTo(x, y) - 新開一個路徑,并指定路徑的起點
- * context.lineTo(x, y) - 將當(dāng)前點與指定的點用一條筆直的路徑連接起來
- */
- ctx.beginPath();
- ctx.moveTo(20, 20);
- ctx.lineTo(200, 20);
- ctx.stroke();
- /*
- * context.lineCap - 指定線條末端的繪制方式
- * round - 線條末端有一個半圓形線帽
- * square - 線條末端有一個矩形線帽
- * butt - 線條末端無任何特殊處理,此值為默認值
- */
- ctx.beginPath();
- ctx.lineCap = "round";
- ctx.moveTo(20, 40);
- ctx.lineTo(200, 40);
- ctx.stroke();
- ctx.beginPath();
- ctx.lineCap = "square";
- ctx.moveTo(20, 60);
- ctx.lineTo(200, 60);
- ctx.stroke();
- ctx.beginPath();
- ctx.lineCap = "butt";
- ctx.moveTo(20, 80);
- ctx.lineTo(200, 80);
- ctx.stroke();
- ctx.lineWidth = 20;
- /*
- * context.lineJoin - 指定兩條線段的連接方式
- * bevel - 兩條線段的連接點用一個三角形填充
- * round - 兩條線段的連接點用一個弧形填充
- * miter - 兩條線段以斜接的方式連接,默認值
- */
- ctx.beginPath();
- ctx.lineJoin = "bevel";
- ctx.moveTo(20, 120);
- ctx.lineTo(60, 120);
- ctx.lineTo(20, 160);
- ctx.stroke();
- ctx.beginPath();
- ctx.lineJoin = "round";
- ctx.moveTo(100, 120);
- ctx.lineTo(140, 120);
- ctx.lineTo(100, 160);
- ctx.stroke();
- ctx.beginPath();
- ctx.lineJoin = "miter";
- ctx.moveTo(180, 120);
- ctx.lineTo(220, 120);
- ctx.lineTo(180, 160);
- ctx.stroke();
- /*
- * context.miterLimit - 當(dāng) lineJoin 為 miter 方式時,此值表示斜接長度與筆劃寬度之間的所允許的最大比值,默認值為 10
- */
- ctx.miterLimit = 2;
- ctx.beginPath();
- ctx.lineJoin = "miter";
- ctx.moveTo(260, 120);
- ctx.lineTo(300, 120);
- ctx.lineTo(260, 160);
- ctx.stroke();
- ctx.lineWidth = 2;
- /*
- * context.closePath() - 如果當(dāng)前路徑是打開的則關(guān)閉它
- */
- ctx.beginPath();
- ctx.moveTo(20, 180);
- ctx.lineTo(180, 180);
- ctx.lineTo(180, 280);
- ctx.closePath(); // 關(guān)閉打開的路徑
- ctx.stroke();
- }
- function clearIt() {
- ctx.clearRect(0, 0, 340, 300);
- ctx.fillStyle = "Black";
- ctx.strokeStyle = "Black";
- ctx.lineWidth = 1;
- }
- </script>
- </body>
- </html>
6、路徑方式繪制 - 矩形 | rect()
canvas/shape/path/rect.html
- <!DOCTYPE HTML>
- <html>
- <head>
- <title>以路徑的方式在 canvas 上繪制矩形的 demo</title>
- </head>
- <body>
- <canvas id="canvas" width="300" height="360" style="background-color: rgb(222, 222, 222)">
- 您的瀏覽器不支持 canvas 標(biāo)簽
- </canvas>
- <br/>
- <button type="button" onclick="drawIt();">在畫布上繪制矩形</button>
- <button type="button" onclick="clearIt();">清除畫布</button>
- <script type="text/javascript">
- var ctx = document.getElementById('canvas').getContext('2d');
- function drawIt() {
- clearIt();
- ctx.strokeStyle = "Black";
- /*
- * context.rect(x, y, w, h) - 以路徑的方式繪制一個矩形
- * x - 矩形左上角的 x 坐標(biāo)
- * y - 矩形左上角的 y 坐標(biāo)
- * w - 矩形的寬
- * h - 矩形的高
- */
- ctx.beginPath();
- ctx.rect(20, 20, 260, 320);
- ctx.stroke();
- }
- function clearIt() {
- ctx.clearRect(0, 0, 300, 360);
- }
- </script>
- </body>
- </html>
原文鏈接:http://www.cnblogs.com/webabcd/archive/2012/02/13/2348813.html