绘制直线
常用绘制路径方法:
beginPath()
closePath()
moveTo(x, y)
lineTo(x, y)
stroke()
fill()
绘制线段
window.onload = function(){
var cvs = document.getElementById("cvs");
var ctx = cvs.getContext("2d");
ctx.beginPath();
ctx.moveTo(250, 250);
ctx.lineTo(300, 300);
ctx.stroke();
};
绘制折线
window.onload = function(){
var cvs = document.getElementById("cvs");
var ctx = cvs.getContext("2d");
ctx.beginPath();
ctx.moveTo(250, 250);
ctx.lineTo(300, 300);
ctx.lineTo(100, 200);
ctx.stroke();
};
闭合路径
window.onload = function(){
var cvs = document.getElementById("cvs");
var ctx = cvs
ctx.beginPath();
ctx.moveTo(250, 250);
ctx.lineTo(300, 300);
ctx.lineTo(100, 200);
ctx.closePath();
ctx.stroke();
};
绘制扇形
window.onload = function(){
var cvs = document.getElementById("cvs");
var ctx = cvs.getContext("2d");
ctx.beginPath();
ctx.moveTo(250, 250);
ctx.arc(250, 250, 100, 0, Math.PI/4);
ctx.fill();
};
绘制曲线
window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
ctx.beginPath();
ctx.moveTo(30,96);
ctx.lineTo(70,66);
ctx.lineTo(103,76);
ctx.lineTo(170,15);
ctx.stroke();
};
绘制连接线
window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
var lastX = 200;
var lastY = 200;
ctx.onmousedown = function(ev){
paint(ev.offsetX, ev.offsetY);
}
function paint() {
ctx.beginPath();
ctx.moveTo(lastX,lastY);
ctx.lineTo(x,y);
ctx.stroke();
lastX = x;
lastY = y;
}
};
线条样式
属性 |
描述 |
lineCap |
设置或返回线条的结束端点样式 |
lineJoin |
设置或返回两条线相交时,所创建的拐角类型 |
lineWidth |
设置或返回当前的线条宽度 |
miterLimit |
设置或返回最大斜接长度 |