一、繪制線條
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尚未熟練。。。后續