什么是Canvas?
HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像。 画布是一个矩形区域,您可以控制其每一像素。 canvas 拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。
创建Canvas元素
向 HTML5 页面添加 canvas 元素。 规定元素的 id、宽度和高度(画布是白色的,默认是300*150,可以设置画布大小) :
<canvas id="myCanvas" width="200" height="100"></canvas>
Canvas坐标系
通过JavaScript来绘制:(默认宽度时1px,默认颜色是黑色 )
<script>
//获取元素
var myCanvas=document.querySelector('canvas');
//获取上下文,绘制工具
var ctx=myCanvas.getContext('2d');
//移动画笔
ctx.moveTo(100,100);
//绘制直线
ctx.lineTo(200,100);
//描边
ctx.stroke();
ctx.strokeStyle='blue';//设置线的颜色
ctx.lineWidth=10;//设置线的宽度
<script>
Canvas的基本使用
图形绘制
需要理解些概念:
-
路径的概念
-
路径的绘制
-
描边 stroke()
-
填充 fill()
-
在填充的时候遵循非零环绕规则:
- 看一个区域是否被填充
- 从这个区域拉一条直线
- 看和这条直线相交的轨迹
- 如果是顺时针就+1,如果是逆时针就-1
- 把所有的值加起来
- 若果是非0,就填充的
- 如果是0,就不填充
-
闭合路径
-
手动闭合
-
程序闭合 closePath()
-
-
开启新的路径,解决路径覆盖问题 beginPath()
设置样式
-
画笔的状态
-
lineWidth 线宽,默认1px
-
lineCap 线末端类型:(butt默认)、round(圆角) 、square(方形)
-
lineJoin 相交线的拐点 miter(默认)、round、bevel
-
strokeStyle 线的颜色(颜色渐变可以由for循环实现,将颜色的三个值通过for的每一次循环来变,关键就是rga的三个颜色值 )
-
fillStyle 填充颜色
-
setLineDash() 设置虚线,用数组描述排列方式,传一个数组进去
-
getLineDash() 获取虚线宽度集合,获取的是不重复的虚线排列方式
-
lineDashOffset 设置虚线偏移量(负值前面的往后偏移,正直后面的往偏移 )
-
参考文档
-
w3school(http://www.w3school.com.cn/tags/html_ref_canvas.asp)
-
Canvas_API(https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API/Tutorial)
实例:
绘制网格坐标轴折线图:
<canvas width="600" 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=20;
//坐标间距
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 () {
//x方向的线
this.ctx.strokeStyle = '#eee';
var xLineTotal=Math.floor(this.canvasHeight/this.gridSize);
for(let 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.stroke();
}
//x方向的线
var yLineTotal=Math.floor(this.canvasWidth/this.gridSize);
for(let 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.stroke();
}
};
//绘制坐标系
LineChart.prototype.drawAxis=function () {
// x轴
this.ctx.beginPath();
this.ctx.strokeStyle='#000';
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.stroke();
this.ctx.fill();
// y轴
this.ctx.beginPath();
this.ctx.strokeStyle = '#000';
this.ctx.moveTo(this.x0, this.y0);
this.ctx.lineTo(this.space, 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.stroke();
this.ctx.fill();
};
//绘制所有点
LineChart.prototype.drawDotted=function (data) {
//数据坐标需要转化为canvas坐标
var that=this;
//记录当前坐标
var prevCanvasX=0;
var prevCanvasY=0;
data.forEach(function (item,i) {
/* x = 原点的坐标 + 数据的坐标 */
/* y = 原点的坐标 - 数据的坐标 */
var canvasX = that.x0 + item.x;
var canvasY = that.y0 - item.y;
/*绘制点*/
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();
//把点连起来,当时第一个点的时候,起点是x0 y0,当不是第一个点的时候,起点是上一个点
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:120
},
{
x:500,
y:80
}
];
var lineChart=new LineChart();
lineChart.init(data);
</script>
结果如图所示:
Canvas图形绘制
矩形绘制
-
rect(x,y,w,h) 没有独立路径
-
strokeRect(x,y,w,h) 有独立路径,不影响别的绘制
-
fillRect(x,y,w,h) 有独立路径,不影响别的绘制
-
clearRect(x,y,w,h) 擦除矩形区域
颜色渐变的矩形:(颜色:方向,起始颜色,结束颜色)
css可以使用linear-gradident(方向,起始颜色,终止颜色)
canvas中的线性渐变:通过两点的坐标可以控制渐变方向
使用canvas封装函数,createLinearGradient(),添加颜色:addColorStop(起始位置,
圆弧绘制
-
弧度概念
-
arc()
-
x 圆心横坐标
-
y 圆心纵坐标
-
r 半径
-
startAngle 开始角度
-
endAngle 结束角度
-
anticlockwise 是否逆时针方向绘制(默认false表示顺时针;true表示逆时针)
-
绘制文本
-
ctx.font = '微软雅黑' 设置字体
-
strokeText()
-
fillText(text,x,y,maxWidth)
-
text 要绘制的文本
-
x,y 文本绘制的坐标(文本左下角)
-
maxWidth 设置文本最大宽度,可选参数
-
-
ctx.textAlign文本水平对齐方式,相对绘制坐标来说的
-
left
-
center
-
right
-
start 默认
-
end
-
-
ctx.direction属性css(rtl ltr) start和end于此相关
-
如果是ltr,start和left表现一致
-
如果是rtl,start和right表现一致
-
-
ctx.textBaseline 设置基线(垂直对齐方式 )
-
top 文本的基线处于文本的正上方,并且有一段距离
-
middle 文本的基线处于文本的正中间
-
bottom 文本的基线处于文本的证下方,并且有一段距离
-
hanging 文本的基线处于文本的正上方,并且和文本粘合
-
alphabetic 默认值,基线处于文本的下方,并且穿过文字
-
ideographic 和bottom相似,但是不一样
-
-
measureText() 获取文本宽度obj.width
做动画
绘制图片
-
drawImage()
-
三个参数drawImage(img,x,y)
-
img 图片对象、canvas对象、video对象
-
x,y 图片绘制的左上角
-
-
五个参数drawImage(img,x,y,w,h)
-
img 图片对象、canvas对象、video对象
-
x,y 图片绘制的左上角
-
w,h 图片绘制尺寸设置(图片缩放,不是截取)
-
-
九个参数drawImage(img,x,y,w,h,x1,y1,w1,h1)
-
img 图片对象、canvas对象、video对象
-
x,y,w,h 图片中的一个矩形区域
-
x1,y1,w1,h1 画布中的一个矩形区域
-
-
序列帧动画
-
绘制精灵图
-
动起来
-
控制边界
-
键盘控制
坐标变换
-
平移 移动画布的原点
-
translate(x,y) 参数表示移动目标点的坐标
-
-
缩放
-
scale(x,y) 参数表示宽高的缩放比例
-
-
旋转
-
rotate(angle) 参数表示旋转角度
-