<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Canvas Canvas是HTML5新增的組件,它就像一塊幕布,可以用JavaScript在上面繪制各種圖表、動畫等。 沒有Canvas的年代,繪圖只能借助Flash插件實現,頁面不得不用JavaScript和Flash進行交互。有了Canvas,我們就再也不需要Flash了,直接使用JavaScript完成繪制。 一個Canvas定義了一個指定尺寸的矩形框,在這個范圍內我們可以隨意繪制: ``` <canvas id="test-canvas" width="300" height="200"></canvas> ``` 由于瀏覽器對HTML5標準支持不一致,所以,通常在`&lt;canvas&gt;`內部添加一些說明性HTML代碼,如果瀏覽器支持Canvas,它將忽略`&lt;canvas&gt;`內部的HTML,如果瀏覽器不支持Canvas,它將顯示`&lt;canvas&gt;`內部的HTML: ``` <canvas id="test-stock" width="300" height="200"> <p>Current Price: 25.51</p> </canvas> ``` 在使用Canvas前,用`canvas.getContext`來測試瀏覽器是否支持Canvas: ``` <!-- HTML代碼 --> <canvas id="test-canvas" width="200" heigth="100"> <p>你的瀏覽器不支持Canvas</p> </canvas> ``` ``` 'use strict'; var canvas = document.getElementById('test-canvas'); if (canvas.getContext) { alert('你的瀏覽器支持Canvas!'); } else { alert('你的瀏覽器不支持Canvas!'); } ``` `getContext('2d')`方法讓我們拿到一個`CanvasRenderingContext2D`對象,所有的繪圖操作都需要通過這個對象完成。 ``` var ctx = canvas.getContext('2d'); ``` 如果需要繪制3D怎么辦?HTML5還有一個WebGL規范,允許在Canvas中繪制3D圖形: ``` gl = canvas.getContext("webgl"); ``` 本節我們只專注于繪制2D圖形。 ## 繪制形狀 我們可以在Canvas上繪制各種形狀。在繪制前,我們需要先了解一下Canvas的坐標系統: ![canvas-xy](img/l10.png) Canvas的坐標以左上角為原點,水平向右為X軸,垂直向下為Y軸,以像素為單位,所以每個點都是非負整數。 `CanvasRenderingContext2D`對象有若干方法來繪制圖形: ``` 'use strict'; var canvas = document.getElementById('test-shape-canvas'), ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, 200, 200); // 擦除(0,0)位置大小為200x200的矩形,擦除的意思是把該區域變為透明 ctx.fillStyle = '#dddddd'; // 設置顏色 ctx.fillRect(10, 10, 130, 130); // 把(10,10)位置大小為130x130的矩形涂色 // 利用Path繪制復雜路徑: var path=new Path2D(); path.arc(75, 75, 50, 0, Math.PI*2, true); path.moveTo(110,75); path.arc(75, 75, 35, 0, Math.PI, false); path.moveTo(65, 65); path.arc(60, 65, 5, 0, Math.PI*2, true); path.moveTo(95, 65); path.arc(90, 65, 5, 0, Math.PI*2, true); ctx.strokeStyle = '#0000ff'; ctx.stroke(path); ``` ## 繪制文本 繪制文本就是在指定的位置輸出文本,可以設置文本的字體、樣式、陰影等,與CSS完全一致: ``` 'use strict'; var canvas = document.getElementById('test-text-canvas'), ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.shadowOffsetX = 2; ctx.shadowOffsetY = 2; ctx.shadowBlur = 2; ctx.shadowColor = '#666666'; ctx.font = '24px Arial'; ctx.fillStyle = '#333333'; ctx.fillText('帶陰影的文字', 20, 40); ``` Canvas除了能繪制基本的形狀和文本,還可以實現動畫、縮放、各種濾鏡和像素轉換等高級操作。如果要實現非常復雜的操作,考慮以下優化方案: * 通過創建一個不可見的Canvas來繪圖,然后將最終繪制結果復制到頁面的可見Canvas中; * 盡量使用整數坐標而不是浮點數; * 可以創建多個重疊的Canvas繪制不同的層,而不是在一個Canvas中繪制非常復雜的圖; * 背景圖片如果不變可以直接用`&lt;img&gt;`標簽并放到最底層。 ## 練習 請根據從163獲取的JSON數據繪制最近30個交易日的K線圖,數據已處理為包含一組對象的數組: <script>function downloadStockImage() { var canvas = document.getElementById('stock-canvas'), data = canvas.toDataURL(); window.open(data.replace('image/png', 'image/octet-stream')); }</script> ``` window.drawStock = function (data) { var canvas = document.getElementById('stock-canvas'), MAX_X = canvas.width, MAX_Y = canvas.height, ctx = canvas.getContext('2d'); var low = data.reduce(function (prev, x) { return x.low < prev.low ? x : prev; }); var high = data.reduce(function (prev, x) { return x.high > prev.high ? x : prev; }); var chg = high.high - low.low; // index range: var lowest = Math.floor(low.low - chg * 0.1); var highest = Math.floor(high.high + chg * 0.1 + 1); var calcY = function (idx) { return MAX_Y * (highest - idx) / (highest - lowest); }; var drawAtX = function (x, k) { var tmp, y1 = calcY(k.open), y2 = calcY(k.close); if (y1 > y2) { tmp = y1; y1 = y2; y2 = tmp; } ctx.fillStyle = (k.open > k.close) ? '#00ff00' : '#ff0000'; ctx.fillRect(x, calcY(k.high), 1, calcY(k.low) - calcY(k.high)); ctx.fillRect(x-2, y1, 5, y2 - y1); }; ctx.clearRect(0, 0, MAX_X, MAX_Y); ctx.font = '12px serif'; ctx.textAlign = 'right'; ctx.fillStyle = '#000000'; ctx.fillText(String(Math.floor(high.high)), 40, 15); ctx.fillText(String(Math.floor(low.low)), 40, MAX_Y - 20); var i, x; for (i=0; i<data.length; i++) { x = i * 8 + 50; drawAtX(x, data[i]); } }; ``` ``` 'use strict'; window.loadStockData = function (r) { var NUMS = 30, data = r.data; if (data.length > NUMS) { data = data.slice(data.length - NUMS); } data = data.map(function (x) { return { date: x[0], open: x[1], close: x[2], high: x[3], low: x[4], vol: x[5], change: x[6] }; }); window.drawStock(data); } window.drawStock = function (data) { var canvas = document.getElementById('stock-canvas'), width = canvas.width, height = canvas.height, ctx = canvas.getContext('2d'); console.log(JSON.stringify(data[0])); // {"date":"20150602","open":4844.7,"close":4910.53,"high":4911.57,"low":4797.55,"vol":62374809900,"change":1.69} ctx.clearRect(0, 0, width, height); ctx.fillText('Test Canvas', 10, 10); }; // 加載最近30個交易日的K線圖數據: var js = document.createElement('script'); js.src = 'http://img1.money.126.net/data/hs/kline/day/history/2015/0000001.json?callback=loadStockData&t=' + Date.now(); document.getElementsByTagName('head')[0].appendChild(js); ``` [下載為圖片](#0)
                  <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>

                              哎呀哎呀视频在线观看