Html5使用canvas作图

时间:2022-09-04 20:43:27

以下例子是项目中实际用到的。不足之处请大家指正,设计到画线,写文字,填充,文字旋转。

 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Canvas绘图</title>
<script type="text/javascript" src="../../1_js_plugin/jquery/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../1_js_plugin/echarts/echarts.js"></script>
</head> <script type="text/javascript"> var oriData = [7533.00, 15508.00, 16780.00, 18737.00, 21155.00, 23769.00, 26302.00, 28504.00, 30178.00, 31197.00, 31513.00,
31153.00, 30201.00, 28781.00, 27034.00, 25100.00, 23102.00, 21137.00, 19272.00, 17548.00, 15983.00, 14582.00, 13341.00,
12254.00, 11313.00, 10515.00, 9856.00, 9335.00, 8948.00, 8691.00, 8561.00, 8553.00, 8667.00, 8906.00, 9281.00, 9807.00,
10503.00, 11389.00, 12478.00, 13778.00, 15283.00, 16977.00, 18837.00, 20830.00, 22919.00, 25064.00, 27211.00, 29298.00,
31244.00, 32955.00, 34330.00, 35272.00, 35704.00, 35588.00, 34929.00, 33781.00, 32242.00, 30431.00, 28473.00, 26477.00,
24523.00, 22657.00, 20892.00, 19222.00, 17631.00, 16109.00, 14656.00, 13286.00, 12022.00, 10889.00, 9908.00, 9088.00,
8426.00, 7905.00, 7499.00, 7177.00, 6910.00, 6671.00, 6442.00, 6214.00, 5981.00, 5745.00, 5510.00, 5278.00, 5055.00,
4841.00, 4637.00, 4446.00, 4269.00, 4108.00, 3967.00, 3848.00, 3749.00, 3666.00, 3594.00, 3522.00, 3442.00, 3346.00,
3229.00, 3093.00, 2942.00, 2785.00, 2634.00, 2500.00, 2394.00, 2322.00, 2284.00, 2279.00, 2297.00, 2329.00, 2364.00,
2392.00, 2406.00, 2404.00, 2388.00, 2362.00, 2332.00, 2303.00, 2281.00, 2266.00, 2256.00, 2249.00, 2239.00, 2223.00,
2198.00, 2162.00, 2114.00, 2058.00, 1996.00, 1930.00, 1865.00, 1802.00, 1743.00, 1688.00, 1637.00, 1590.00, 1545.00,
1501.00, 1459.00, 1420.00, 1383.00, 1350.00, 1321.00, 1296.00, 1275.00, 1254.00, 1233.00, 1207.00, 1177.00, 1140.00,
1097.00, 1049.00, 999.00, 948.00, 899.00, 851.00, 807.00, 766.00, 727.00, 691.00, 657.00, 626.00, 598.00, 574.00, 553.00,
536.00, 523.00, 513.00, 505.00, 500.00, 498.00, 497.00, 498.00, 501.00, 503.00, 504.00, 502.00, 497.00, 487.00, 472.00,
453.00, 429.00, 403.00, 375.00, 347.00, 321.00, 298.00, 279.00, 266.00, 257.00, 254.00, 253.00, 255.00, 258.00, 261.00,
263.00, 264.00, 263.00, 260.00, 255.00, 249.00, 242.00, 233.00, 223.00, 213.00, 202.00, 190.00, 180.00, 170.00, 162.00,
157.00, 155.00, 158.00, 164.00, 174.00, 186.00, 200.00, 213.00, 223.00, 231.00, 234.00, 233.00, 229.00, 221.00, 211.00,
201.00, 190.00, 180.00, 172.00, 165.00, 159.00, 155.00, 153.00, 151.00, 151.00, 151.00, 151.00, 151.00, 151.00, 150.00,
149.00, 148.00, 146.00, 145.00, 144.00, 142.00, 141.00, 140.00, 139.00, 138.00, 136.00, 135.00, 134.00, 133.00, 132.00,
130.00, 129.00, 127.00, 124.00, 122.00, 119.00, 117.00, 114.00, 113.00, 111.00, 110.00, 110.00, 110.00, 111.00, 113.00,
115.00, 117.00, 120.00, 122.00, 122.00, 122.00, 120.00, 117.00, 113.00, 109.00, 104.00, 100.00, 96.00, 93.00, 91.00, 91.00,
90.00, 90.00, 90.00, 90.00, 89.00, 88.00, 86.00, 84.00, 82.00, 79.00, 77.00, 74.00, 72.00, 70.00, 69.00, 69.00, 68.00, 68.00,
67.00, 66.00, 65.00, 63.00, 62.00, 60.00, 59.00, 58.00, 57.00, 56.00, 56.00, 55.00, 55.00, 53.00, 52.00, 50.00, 48.00, 46.00,
45.00, 43.00, 41.00, 40.00, 39.00, 37.00, 36.00, 35.00, 34.00, 33.00, 32.00, 32.00, 33.00, 33.00, 33.00, 32.00, 32.00, 31.00,
30.00, 30.00, 29.00, 29.00, 29.00, 29.00, 29.00, 28.00, 27.00, 26.00, 25.00, 24.00, 23.00, 21.00, 20.00, 20.00, 19.00, 19.00,
19.00, 18.00, 18.00, 18.00, 18.00, 18.00, 18.00, 18.00, 17.00, 17.00, 17.00, 17.00, 16.00, 16.00, 15.00, 14.00, 13.00, 12.00,
12.00, 11.00, 10.00, 9.00, 9.00, 9.00, 8.00, 8.00, 8.00, 8.00, 7.00, 7.00, 7.00, 6.00, 6.00, 6.00, 6.00, 5.00, 5.00, 5.00, 5.00,
5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 5.00, 4.00, 4.00, 4.00, 4.00, 4.00, 3.00, 3.00, 3.00, 3.00, 3.00, 2.00, 2.00, 2.00, 2.00,
2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 2.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00,
1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 0.00,
0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] // 原始数据
var max = {xMax: 0, yMax: 0}; // 横坐标与纵坐标的最值
var dataNum = 512; // 绘图点的个数
var data = {xData: [], yData: []}; // 坐标值
// 分割点的x坐标与索引
var segArr = [
{x: 0, index: Math.round(0 * dataNum)},
{x: 0.04, index: Math.round(0.04 * dataNum), color: "gray"},
{x: 0.15, index: Math.round(0.15 * dataNum), color: "red"},
{x: 0.4, index: Math.round(0.4 * dataNum), color: "green"},
{x: 1, index: Math.round(1 * dataNum)}
]; $(function () {
dealData();
draw();
}) /**
* @function:数据处理,获取x与y
* @author:
* @date:2018-05-29 07:48
*/
function dealData() {
var STEP = 1 / 512; var xMax = 0;
var yMax = 0;
var x_data = [];
var y_data = []; // x坐标及最值
x_data.push(0);
for (var i = 1; i <= 512; i++) {
x_data.push(Math.round((x_data[i - 1] + STEP) * 10000) / 10000);
xMax = xMax < x_data[i] ? x_data[i] : xMax;
}
max.xMax = xMax;
data.xData = x_data; // y坐标及最值
for (var i = 0; i < oriData.length; i++) {
y_data.push(oriData[i] / 1000 / 1000);
yMax = yMax < y_data[i] ? y_data[i].toFixed(3) : yMax;
}
max.yMax = yMax;
data.yData = y_data;
} /**
* @function:绘图
* @author:
* @date:2018-05-29 19:18
*/
function draw() {
// 坐标轴矩形的宽高
var retWidth = 680;
var retHeight = 460; // 坐标轴矩形的左上角坐标
var ret_leftTop_x = 80;
var ret_leftTop_y = 80; // 坐标轴矩形的左下角坐标
var ret_leftBottom_y = ret_leftTop_y + retHeight; // 坐标轴的分割数目
var xSeg = 4;
var ySeg = 5; // 坐标刻度短轴长度
var axis = 5; // 实际有效的绘图数目
var validDataNum = 205; var fdCanvas = document.getElementById("frequencyDomainChart");
var ctx = fdCanvas.getContext("2d");
var tip = fdCanvas.getContext("2d"); // canvas的宽高
var canvasWidth = fdCanvas.width; // title
tip.font = "18px Arial";
tip.fillText("RR Spectrum", 20, 30, 100); // fillText(text,x,y,maxWidth) // 坐标轴
ctx.lineWidth = 1;
ctx.rect(ret_leftTop_x, ret_leftTop_y, retWidth, retHeight); // rect(x,y,width,height)
ctx.stroke(); // x轴及坐标
ctx.beginPath();
for (var i = 0; i <= xSeg; i++) {
ctx.moveTo(ret_leftTop_x + retWidth / xSeg * i, ret_leftBottom_y - axis);
ctx.lineTo(ret_leftTop_x + retWidth / xSeg * i, ret_leftBottom_y);
ctx.stroke();
ctx.fillText(i/10, ret_leftTop_x - 20 / 2 + retWidth / xSeg * i, ret_leftBottom_y + 20, 20);
}
ctx.closePath();
tip.font = "15px Arial";
tip.fillText("Frequency(Hz)", canvasWidth / 2 - 40, ret_leftBottom_y + 50, 100); // fillText(text,x,y,maxWidth) // y轴及坐标
ctx.beginPath();
for (var i = 0; i <= ySeg; i++) {
ctx.moveTo(ret_leftTop_x, ret_leftTop_y + retHeight / ySeg * i);
ctx.lineTo(ret_leftTop_x + axis, ret_leftTop_y + retHeight / ySeg * i);
ctx.stroke();
ctx.fillText((max.yMax - max.yMax / ySeg * i).toFixed(3), ret_leftTop_x - 35, ret_leftTop_y + retHeight / ySeg * i + 5, 30);
}
ctx.closePath(); // 画曲线
for (var t = 0; t < segArr.length - 2; t++) {
ctx.beginPath();
ctx.lineWidth = 2;
ctx.moveTo(retWidth / validDataNum * segArr[t].index + ret_leftTop_x, retHeight - retHeight * data.yData[segArr[t].index] / max.yMax + ret_leftTop_y);
for (var i = segArr[t].index + 1; i <= segArr[t + 1].index; i++) {
ctx.lineTo(retWidth / validDataNum * i + ret_leftTop_x, retHeight - retHeight * data.yData[i] / max.yMax + ret_leftTop_y);
}
console.log(segArr[t + 1].x);
ctx.lineTo(ret_leftTop_x + segArr[t + 1].x * retWidth/(xSeg*1.0/10), ret_leftBottom_y);
ctx.lineTo(ret_leftTop_x + segArr[t].x * retWidth/(xSeg*1.0/10), ret_leftBottom_y);
ctx.closePath();
ctx.stroke();
if (t != segArr.length - 1) {
ctx.fillStyle = segArr[t + 1].color;
ctx.fill();
}
} // 纵轴单位
tip.font = "15px Arial";
tip.fillStyle = "black";
tip.translate(ret_leftTop_x - 50,ret_leftTop_y + retHeight/2); //重新映射画布上的 (0,0) 位置
tip.rotate( 270* Math.PI/180); //旋转当前的绘图,以弧度计
tip.fillText("PSD(s^2/Hz)", 0, 0, 100); } </script> <style type="text/css">
#frequencyDomainChart {
border: 1px solid #000000;
}
</style> <body>
<canvas id="frequencyDomainChart" height="600px" width="800px"></canvas>
</body>
</html>

最终效果(重点是文字旋转,文字旋转必选放到最后;还有画一段连续的线条时,只需第一次moveTo,然后剩下的lineTo即可):

Html5使用canvas作图