基础用法
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.strokeStyle = '#ff0000';
ctx.lineWidth = 5;
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(0, 50);
ctx.stroke();
封装调用
drawLine(x1, y1, x2, y2, color, lineWidth) {
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
this.drawLine(0, 0, 0, 50, 'red', 3)
循环调用
for (let i = 0; i < serverData.length; i++) {
this.drawLine(100 * i, 0, 100 * i, 50, 'red', 3)
}