纸飞机模拟一个物体在规定设计轴线偏离方位。
//三角形
function DrawTriangle(canvas, A, B, C) {
//画个三角形,“A、B、C”是顶点
with (canvas) {
moveTo(A[0], A[1]);
lineTo(B[0], B[1]);
lineTo(C[0], C[1]);
lineTo(A[0], A[1]);
}
} // 画纸飞机
//headx,heady 纸飞机头坐标
//footx,footy 纸飞机尾巴坐标
function DrawDarts(canvas, headx, heady, footx, footy) {
var c = document.getElementById(canvas);
var cxt = c.getContext("2d"); //半径 R
var R = 150; //转换头坐标
var headx1 = R + headx;
var heady1 = R - heady;
//转换尾坐标
var footx1 = R + footx;
var footy1 = R - footy; //左尾
var footxl = R + (footx - 50);
var footyl = R - footy; //右尾
var footxr = R + (footx + 50);
var footyr = R - footy; //上尾
var footxu = R + footx;
var footyu = R - (footy - 50); //下尾
var footxd = R + footx;
var footyd = R - (footy + 50); //判断头x和尾x的大小
cxt.beginPath();
cxt.strokeStyle = "black";
cxt.fillStyle = "#6C8D9F";
var A = new Array(headx1, heady1);
var B = new Array(footx1, footy1);
// alert(headx1);
// alert(footx1);
if (parseInt(headx1) - parseInt(footx1) <= 0)
var C = new Array(footxr, footyr);
else
var C = new Array(footxl, footyl);
DrawTriangle(cxt, A, B, C); cxt.fill();
cxt.closePath();
cxt.stroke(); //画头、上、下尾巴 形成的三角形
cxt.beginPath();
cxt.strokeStyle = "black";
cxt.fillStyle = "#6C8D9F";
var A1 = new Array(headx1, heady1);
var B1 = new Array(footxu, footyu);
var C1 = new Array(footxd, footyd);
DrawTriangle(cxt, A1, B1, C1);
cxt.fill();
cxt.closePath();
cxt.stroke(); cxt.beginPath();
cxt.strokeStyle = "black";
cxt.fillStyle = "#6C8D9F";
var A2 = new Array(headx1, heady1);
var B2 = new Array(footx1, footy1);
if (parseInt(headx1) - parseInt(footx1) <= 0)
var C2 = new Array(footxl, footyl);
else
var C2 = new Array(footxr, footyr);
DrawTriangle(cxt, A2, B2, C2);
cxt.fill();
cxt.closePath();
cxt.stroke();
}
html 页面调用
<!DOCTYPE HTML>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HTML Canvas 画纸飞机</title>
<script type="text/javascript" src="Darts.js"></script>
</head>
<body>
<canvas id="can" width="300" height="300" style="border: 1px solid #00F">浏览器不支持HTML5!</canvas>
<script type="text/javascript" charset="utf-8">
DrawDarts("can", -10, 10, 30, -30)
</script>
</body>
</html>
预览效果图
效果图2: