HTML5 绘制简单的飞机模型

来源:互联网 发布:网络优化工程师面试题 编辑:程序博客网 时间:2024/04/28 06:32

<!DOCTYPE HTML>

<!--

   一.绘制一架模型飞机向上飞

      1.用fillText方法绘制简单的模型飞机

       2.用到定时器setInterval 目的是绘制出来的模型飞机能向上移动

      3.用到键盘事件加载,目的是绘制出来的模型飞机能消失

-->

代码:

drawingPlane.html

<html>
<head>
<title></title>
<script>
 var time;
heightTop=500;
function start(){
time=setInterval(function(){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.clearRect(0,0,600,500);//绘制前清除画布
  plane();
},100);
}
function plane(){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

//机翼
ctx.fillText("A", 260, heightTop-40);
ctx.fillText("A", 270, heightTop-40);
ctx.fillText("A", 280, heightTop-40);
ctx.fillText("A", 290, heightTop-40);
ctx.fillText("A", 300, heightTop-40);
ctx.fillText("A", 310, heightTop-40);
ctx.fillText("A", 320, heightTop-40);
ctx.fillText("A", 330, heightTop-40);
ctx.fillText("A", 340, heightTop-40);

//尾身

ctx.fillText("A", 300, heightTop);
ctx.fillText("A", 300, heightTop-10);
ctx.fillText("A", 300, heightTop-20);
ctx.fillText("A", 300, heightTop-30);
ctx.fillText("A", 300, heightTop-40);
ctx.fillText("A", 300, heightTop-50);
ctx.fillText("A", 300, heightTop-60);
//尾翼
ctx.fillText("H", 280, heightTop);
ctx.fillText("H", 290, heightTop);
ctx.fillText("H", 300, heightTop);
ctx.fillText("H", 310, heightTop);
ctx.fillText("H", 320, heightTop);
ctx.stroke();
heightTop-=1;
}
</script>
</head>
<body>
<input  type="button" onclick="start()" value="take off"/>
<canvas id="myCanvas" width="600" height="500" style="border:1px solid black;"></canvas>  //绘制画布
</body>

</html>


效果图:

按A键是绘制的plane会消失


原创粉丝点击