canvas繪製米字旗程式碼例項

admin發表於2018-02-09

本章節分享一段簡單的使用canvas繪製圖案的簡單程式碼例項。

需要的朋友可以做一下參考,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<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>

相關文章