canvas的线形基本用法+ 绘制折线图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>canvas</title>
<style>
canvas{
border:1px solid #eee;
}
</style>
</head>
<body>
<canvas width="1000" height="1000">
</canvas>
<script>
var mycanvas = document.querySelector('canvas')
var ctx =mycanvas.getContext('2d');
ctx.beginPath();
ctx.moveTo(100,100.5);
ctx.lineTo(300,20);
ctx.lineTo(300,100.5);
ctx.strokeStyle="red";
ctx.lineWidth =20;
ctx.lineCap = 'round';
ctx.lineJoin= 'round'
ctx.stroke();
ctx.beginPath();
ctx.moveTo(100,200.5);
ctx.lineTo(200,120);
ctx.lineTo(300,200.5);
ctx.strokeStyle="blue";
ctx.lineWidth =10;
ctx.lineCap = 'square';
ctx.lineJoin = 'bevel'
ctx.stroke();
ctx.beginPath();
ctx.moveTo(100,300.5);
ctx.lineTo(300,300.5);
ctx.strokeStyle="green";
ctx.lineWidth =5;
ctx.lineCap = 'butt';
ctx.lineJoin = ''
ctx.stroke();
ctx.moveTo(100,400);
ctx.lineTo(200,400);
ctx.lineTo(200,500)
ctx.closePath();
ctx.stroke();
ctx.fillStyle ='pink'
ctx.fill();
ctx.beginPath();
ctx.strokeStyle="red";
ctx.moveTo(400,100);
ctx.lineTo(400,200);
ctx.lineTo(500,200);
ctx.lineTo(500,100);
ctx.closePath();
ctx.fill();
ctx.fillStyle ='red'
ctx.stroke();
ctx.beginPath();
ctx.moveTo(400,300.5);
ctx.lineTo(600,300.5);
ctx.strokeStyle="#aaa";
ctx.setLineDash([5,20,15]);
console.log(ctx.getLineDash());
ctx.lineDashOffset =20;
ctx.stroke();
var mycanvas = document.querySelector('canvas')
var ctx =mycanvas.getContext('2d');
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>canvas-折线图</title>
<style>
canvas{
border:1px solid #aaa;
}
</style>
</head>
<body>
<canvas width='800' height='400'>
</canvas>
<script>
var LineChart = function(ctx){
this.ctx = ctx || document.querySelector('canvas').getContext('2d');
this.canvasWidth = this.ctx.canvas.width;
this.canvasHeight = this.ctx.canvas.height;
this.gridSize = 10;
this.space = 20;
this.x0 = this.space;
this.y0 = this.canvasHeight-this.space;
this.arrowSize = 10;
this.dottedSize = 6;
}
LineChart.prototype.init =function(data) {
this.drawGrid();
this.drawAxis();
this.drawDotted(data);
};
LineChart.prototype.drawGrid =function() {
var xLineTotal = Math.floor(this.canvasHeight/this.gridSize)
for(var i= 0;i<=xLineTotal;i++){
this.ctx.beginPath();
this.ctx.moveTo(0,i*this.gridSize-0.5)
this.ctx.lineTo(this.canvasWidth,i*this.gridSize-0.5)
this.ctx.strokeStyle ='red';
this.ctx.stroke()
}
var yLineTotal = Math.floor(this.canvasWidth/this.gridSize);
for(var i =0; i<=yLineTotal;i++){
this.ctx.beginPath();
this.ctx.moveTo(i*this.gridSize-0.5,0)
this.ctx.lineTo(i*this.gridSize-0.5,this.canvasHeight);
this.ctx.strokeStyle ='red';
this.ctx.stroke()
}
};
LineChart.prototype.drawAxis =function() {
this.ctx.beginPath();
this.ctx.strokeStyle ='green';
this.ctx.moveTo(this.x0,this.y0);
this.ctx.lineTo(this.canvasWidth-this.space,this.y0)
this.ctx.lineTo(this.canvasWidth-this.space-this.arrowSize,this.y0+this.arrowSize/2)
this.ctx.lineTo(this.canvasWidth-this.space-this.arrowSize,this.y0-this.arrowSize/2)
this.ctx.lineTo(this.canvasWidth-this.space,this.y0)
this.ctx.fill();
this.ctx.stroke();
this.ctx.beginPath();
this.ctx.strokeStyle ='green';
this.ctx.moveTo(this.x0,this.y0)
this.ctx.lineTo(this.x0,this.space)
this.ctx.lineTo(this.space+this.arrowSize/2,this.space+this.arrowSize)
this.ctx.lineTo(this.space-this.arrowSize/2,this.space+this.arrowSize)
this.ctx.lineTo(this.space,this.space)
this.ctx.fill()
this.ctx.stroke()
} ;
LineChart.prototype.drawDotted =function(data) {
var that = this;
var prevCanvasX = 0;
var prevCanvasY = 0;
data.map((item,i)=>{
var canvasX = that.x0+item.x;
var canvasY = that.y0-item.y;
console.log(canvasX-that.dottedSize/2,canvasY-that.dottedSize/2)
that.ctx.beginPath();
that.ctx.moveTo(canvasX-that.dottedSize/2,canvasY-that.dottedSize/2);
that.ctx.lineTo(canvasX+that.dottedSize/2,canvasY-that.dottedSize/2);
that.ctx.lineTo(canvasX+that.dottedSize/2,canvasY+that.dottedSize/2);
that.ctx.lineTo(canvasX-that.dottedSize/2,canvasY+that.dottedSize/2);
that.ctx.closePath();
that.ctx.fill();
if(i==0){
that.ctx.beginPath();
that.ctx.moveTo(that.x0,that.y0)
that.ctx.lineTo(canvasX,canvasY)
that.ctx.stroke();
}else{
that.ctx.beginPath();
that.ctx.moveTo(prevCanvasX,prevCanvasY)
that.ctx.lineTo(canvasX,canvasY)
that.ctx.stroke();
}
prevCanvasX = canvasX;
prevCanvasY = canvasY;
})
} ;
var data = [
{
x:100,
y:120
},
{
x:200,
y:160
},
{
x:300,
y:240
},
{
x:400,
y:320
},
{
x:500,
y:80
},
];
var LineChart= new LineChart();
LineChart.init(data);
</script>
</body>
</html>