一、绘制线条
context.lineTo(x,y)
context.moveTo(x,y)
起点moveTo的点到lineTo的点之间画一条之间
若没有moveTo那么第一次lineTo的效果是和moveTo一样的
每次lineTo后如果没有moveTo,哪啊么下次lineTo的开始点为前一次lineTo的终点
切记最后要加上stroke()
<script type="text/javascript">
var tangram=[
{p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"#caff67"},
{p:[{x:0,y:0},{x:400,y:400},{x:0,y:800}],color:"#67becf"},
{p:[{x:800,y:0},{x:800,y:400},{x:600,y:600},{x:600,y:200}],color:"#ef3d61"},
{p:[{x:600,y:200},{x:600,y:600},{x:400,y:400}],color:"#f9f51a"},
{p:[{x:400,y:400},{x:600,y:600},{x:400,y:800},{x:200,y:600}],color:"#a594c0"},
{p:[{x:200,y:600},{x:400,y:800},{x:0,y:800}],color:"#fa8ecc"},
{p:[{x:800,y:400},{x:800,y:800},{x:400,y:800}],color:"#f6ca29"}
]
window.onload=function () {
var canvas=document.getElementById('canvas');
canvas.width=800;
canvas.height=800;
var context=canvas.getContext('2d');
for(var i=0;i<tangram.length;i++){
draw(tangram[i])}
}
function draw(piece) {
var context=canvas.getContext('2d');
context.beginPath();
context.moveTo(piece.p[0].x,piece.p[0].y);
console.log(piece.p[0].x+":"+piece.p[0].y); 测试专用
for(var i=1;i<piece.p.length;i++){
context.lineTo(piece.p[i].x,piece.p[i].y);
}
context.closePath();
context.fillStyle=piece.color;
context.fill();
context.lineWidth=3;
context.strokeStyle="black";
context.stroke();
}
二、绘图
context.drawImage(img,x,y )
img:对象var img=new Image()
x:图像左上角的x坐标
y:图像左上角的y坐标
context.drawImage(img ,x,y,w,h)
w:是绘图的宽度
h:是绘图的高度
context.drawImage(img,x,y,xx,yy,dx,dy,dxx,dyy)
xx:是矩形区域的宽度
yy:是矩形区域的高度
dx:画在canvas的x坐标
dy:画在canvas的y坐标
dxx:是画出来的宽度
dyy:是画出来的高度
图像的平铺context.createPattern(img,type)
type:no-repeat/repeat-x/repeat-y/repeat
图像的裁剪:context.clip()
只绘制封闭路径区域的图像
canvas变形:
平移,缩放(按照比例坐标缩放),旋转
chart.js尚未熟练。。。后续