canvas绘制米字旗代码实例

快乐打工仔 分类:实例代码

canvas绘制米字旗代码实例属于前端实例代码,有关更多实例代码大家可以查看

本章节分享一段简单的使用canvas绘制图案的简单代码实例。

需要的朋友可以做一下参考,代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<title>前端教程网</title>
</head>
<body>
<div style="margin:100px auto;width:400px;height:200px;">
  <canvas id="myCanvas" style="width:400px;height:200px;"></canvas>
</div>
<script type="text/javascript">
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
 
//绘制蓝色背景
ctx.fillStyle = "#0065BD";
ctx.fillRect(0, 0, 400, 200);
 
//绘制白色X
ctx.beginPath();
ctx.lineWidth = "30";
ctx.strokeStyle = "white";
ctx.moveTo(0, 0);
ctx.lineTo(400, 200);
ctx.moveTo(400, -50);
ctx.lineTo(-100, 200);
ctx.stroke();
 
//绘制红色X
ctx.beginPath();
ctx.lineWidth = "10";
ctx.strokeStyle = "red";
ctx.moveTo(0, 0);
ctx.lineTo(400, 200);
ctx.moveTo(400, -50);
ctx.lineTo(-100, 200);
ctx.stroke();
 
//绘制白色+
ctx.beginPath();
ctx.lineWidth = "45";
ctx.strokeStyle = "white";
ctx.moveTo(150, 0);
ctx.lineTo(150, 200);
ctx.moveTo(0, 75);
ctx.lineTo(400, 75);
ctx.stroke();
 
//绘制红色+
ctx.beginPath();
ctx.lineWidth = "30";
ctx.strokeStyle = "red";
ctx.moveTo(150, 0);
ctx.lineTo(150, 200);
ctx.moveTo(0, 75);
ctx.lineTo(400, 75);
ctx.stroke();
</script>
</body>
</html>

canvas绘制米字旗代码实例,这样的场景在实际项目中还是用的比较多的,关于canvas绘制米字旗代码实例就介绍到这了。

回复

我来回复
  • 暂无回复内容