<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## :-: [HTML 5 Canvas 參考手冊](http://www.w3school.com.cn/tags/html_ref_canvas.asp) > ## :-: canvas - 繪制線形 / 填充 ``` <!-- canvas畫布 --> <canvas id="demo" width="600" height="400"></canvas> <script> // 畫筆 - 獲取畫布上下文對象、2D 目前沒有3D var ctx = demo.getContext('2d'); // 想要畫的圖形 ctx.moveTo(50, 50); // 開始 - 起始點、 ctx.lineTo(150, 50); // 到某一點 ctx.lineTo(150, 150); ctx.closePath(); // 閉合路徑、 // 設置畫筆寬度、 ctx.lineWidth = 5; // 設置畫筆顏色、 ctx.strokeStyle = '#f40'; // 開始畫 ctx.stroke(); // 填充 ctx.fill(); // 抬起畫筆 ctx.beginPath(); </script> ``` ![](https://box.kancloud.cn/d70423c591dae71f92b402356d226646_237x177.png) > ## :-: canvas - 繪制矩形 / [圓](http://www.w3school.com.cn/tags/canvas_arc.asp) / 圓角矩形 ``` // 畫筆 - 獲取畫布上下文對象、2D 目前沒有3D var ctx = demo.getContext('2d'); // 畫矩形 - ctx.rect(x, y, dx, dy); ctx.rect(200, 50, 100, 100); ctx.stroke(); ctx.beginPath(); // 直接繪制矩形、 ctx.strokeRect(310, 50, 100, 100); // 直接繪制帶填充的矩形、 ctx.fillRect(310, 160, 100, 100); // 橡皮擦 - ctx.clearRect(x, y, dx, dy) ctx.clearRect(180, 30, 50, 50); // 畫弧形 - 圓 ctx.arc(x,y,r,sAngle,eAngle,counterclockwise); ctx.arc(100, 100, 50, 0, Math.PI * 2, 0); ctx.stroke(); ctx.beginPath(); // Demo ctx.moveTo(100, 250); ctx.arc(100, 250, 50, 0, Math.PI * 1.5, 0); ctx.lineTo(100, 250); ctx.stroke(); ctx.beginPath(); ``` ![](https://box.kancloud.cn/4c96dcd820d4c0943c3239ce002017c7_204x210.png) ![](https://box.kancloud.cn/922e2b5e18fe0444b6aa6af0c9c8f7f7_827x393.png) ![](https://box.kancloud.cn/7639a48e5a03495d7d807f03b70a95a4_620x422.png) ``` // 畫筆 - 獲取畫布上下文對象、2D 目前沒有3D var ctx = demo.getContext('2d'); // 繪制圓角矩形 - ctx.arcTo(x1, y1, x2, y2, r); ctx.moveTo(100, 120); ctx.arcTo(100, 200, 200, 200, 20); // 圓角 ctx.arcTo(200, 200, 200, 100, 20); ctx.arcTo(200, 100, 100, 100, 20); ctx.arcTo(100, 100, 100, 200, 20); ctx.stroke(); ctx.beginPath(); ``` ![](https://box.kancloud.cn/6bd260619415e37b4134fef2dbe8512d_130x123.png) > ## :-: canvas - 貝塞爾曲線 ``` // 2次 貝塞爾曲線 - ctx.quadraticCurveTo(x1, y1, ex, ey); // x1,y1 == 控制點、ex,ey == 結束點 ctx.moveTo(100, 100); ctx.quadraticCurveTo(100, 200, 200, 200); ctx.stroke(); ctx.beginPath(); // 3次 貝塞爾曲線 - ctx.bezierCurveTo(x1, y1, x2, y2, ex, ey); // x1,y1,x2,y2 == 控制點、ex,ey == 結束點 ctx.moveTo(300, 100); ctx.bezierCurveTo(400, 300, 500, 100, 600, 300); ctx.stroke(); ctx.beginPath(); ``` ![](https://box.kancloud.cn/78785980cfc7983a575353e7c3a855e0_586x269.png) > ## :-: canvas - 變換 / 圖案 / 其他 ``` // 畫筆 - 獲取畫布上下文對象、2D 目前沒有3D var ctx = demo.getContext('2d'); // 平移 - ctx.translate(dx, dy); (放在繪圖之前) ctx.translate(-50, -50); // 平移 // 縮放 - ctx.scale(sx, sy); ctx.scale(2, 2); // 縮放 // 旋轉 - ctx.rotate(Math.PI); ctx.rotate(Math.PI / 4); ctx.save(); // 保存當前繪圖狀態、1 ctx.restore(); // 恢復保存的繪圖狀態、2 // 先重置再變換 - ctx.setTransform(水平縮放, 水平傾斜, 垂直傾斜, 垂直縮放, 水平移動, 垂直移動); ctx.setTransform(1, 1, 1, 0.5, 123, 123); // 在之前的基礎上變換 - ctx.transform(水平縮放, 水平傾斜, 垂直傾斜, 垂直縮放, 水平移動, 垂直移動); ctx.transform(1, 2, 3, 4, 5, 6); ctx.strokeRect(100, 100, 200, 200); ctx.stroke(); ctx.beginPath(); // 填充圖案 - ctx.createPattern(img, repeat); var img = new Image(); img.onload = function() { // 圖片加載是異步的過程、需要在加載完成事件里使用、 var fill = ctx.createPattern(this, 'no-repeat'); // 不重復 ctx.fillStyle = fill; // 設置顏色為圖案 ctx.fillRect(50, 50, 100, 100); // 繪制帶填充的矩形 ctx.stroke(); ctx.beginPath(); } img.src = './src/wx.jpg'; // 線性漸變 - ctx.createLinearGradient(x1, y1, x1, y); var bg = ctx.createLinearGradient(0, 0, 200, 200); bg.addColorStop(0, 'red'); bg.addColorStop(0.25, '#139'); bg.addColorStop(0.5, '#168'); bg.addColorStop(0.75, '#654'); bg.addColorStop(1.0, '#000'); ctx.fillStyle = bg; ctx.fillRect(50, 50, 200, 200); ctx.stroke(); ctx.beginPath(); // 徑向漸變 - ctx.createRadialGradient(x1, y1, r1, x2, y2, r2); var bg = ctx.createRadialGradient(200, 200, 0, 200, 200, 100); bg.addColorStop(0, '#fff'); bg.addColorStop(1.0, '#000'); ctx.fillStyle = bg; ctx.arc(200, 200, 100, 0, Math.PI * 2, false); // 畫圓 ctx.fill(); // 填充 ctx.stroke(); ctx.beginPath(); // 陰影 ctx.shadowColor = 'red'; // 陰影顏色 ctx.shadowOffsetX = 10; // X - 偏移量 ctx.shadowOffsetY = 10; // Y - 偏移量 ctx.shadowBlur = 10; // 模糊程度 // ---------------------------------- ctx.fillRect(100, 100, 100, 100); ctx.stroke(); ctx.beginPath(); // 文字陰影 ctx.shadowColor = 'red'; // 陰影顏色 ctx.shadowOffsetX = 10; // X - 偏移量 ctx.shadowOffsetY = 10; // Y - 偏移量 ctx.shadowBlur = 10; // 模糊程度 // ---------------------------------- ctx.fillStyle = '#aaa'; // 填充顏色 ctx.font = '50px sans-serif'; ctx.fillText('Hello World~', 100, 100); ctx.stroke(); ctx.beginPath(); // 線段樣式、.lineCap .lineJoin ctx.lineCap = 'round'; // 圓形 ctx.lineJoin = 'bevel'; // 方形 // 更多樣式自行百度 ``` ![](https://box.kancloud.cn/e7c177b82707e3131bf965c2e9175064_218x215.png)![](https://box.kancloud.cn/c4ff9ca783af5dbc82ff9bfbe5d091dc_224x216.png)![](https://box.kancloud.cn/747d6b955cf6f6c314cc6ab6f72e1a0c_277x221.png)![](https://box.kancloud.cn/4ba1c1bc45eaa9973a1c8929c7838f11_214x212.png)![](https://box.kancloud.cn/d11330b0f0858f95265a33d460ace119_236x131.png)![](https://box.kancloud.cn/d3bcbba542fa07362a7a40a3d04eeb40_330x132.png)![](https://box.kancloud.cn/597b7c27612d66f4e9fa5dfb0af0c711_343x145.png) > ## :-: canvas - 裁剪 ``` ctx.arc(200, 200, 50, 0, Math.PI * 2, true); // 圓 ctx.stroke(); ctx.clip(); // 裁剪 ctx.fillRect(200, 200, 50, 50); // 帶填充矩形 ctx.beginPath(); ``` ![](https://box.kancloud.cn/dd8b1fa038241f49c87343ded480dfdc_123x119.png) > ## :-: canvas - 繪制圖片 ``` // 繪制圖片 - var img = new Image(); img.onload = function() { // 圖片加載是異步的過程、需要在加載完成事件里使用、 // 3個參數的用法、ctx.drawImage(img, x, y); ctx.drawImage(img, 10, 10); // 5個參數的用法、ctx.drawImage(img, x, y, dx.dy); ctx.drawImage(img, 220, 10, 50, 50); // 9個參數的用法、ctx.drawImage(img, x1, y1, dx1, dy1, x2, y2, w2, h2); // 截取圖片0,0 100,100的區域,放到220,70的位置,改變寬高為200*200 ctx.drawImage(img, 0, 0, 100, 100, 220, 70, 200, 200); ctx.beginPath(); } img.src = './src/wx.jpg'; ``` ![](https://box.kancloud.cn/08fc5176703d476d5586ef40b34653a7_819x320.png) > ## :-: canvas - 其他 ``` // 將畫板導出為圖片編碼 (受同源策略影響) data:image/png;base64 var base64 = demo.toDataURL(); console.log(base64); // 創建新的空白ImageData對象、一般不用 ctx.createImageData(100, 100); // 獲取指定區域的像素信息(object) ctx.getImageData(x, y, dx, dy); var data = ctx.getImageData(100, 100, 50, 50); // 放回指定區域的像素、ctx.putImageData(data, x, y); ctx.putImageData(data, 100, 300); // 命中檢查 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看