canvas實現的七巧板效果程式碼例項

admin發表於2017-02-17
分享一段簡單的程式碼例項,它實現了簡單的七巧板效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script>
window.onload = function() {
  var canvas = document.getElementById("canvas");
  canvas.width = 500;
  canvas.height = 500;
  var context = canvas.getContext("2d");
  context.beginPath()
  context.moveTo(0, 0)
  context.lineTo(250, 250)
  context.lineTo(500, 0)
  context.closePath()
  context.fillStyle = "green"
  context.fill()
  context.stroke()
 
  context.beginPath()
  context.moveTo(0, 0)
  context.lineTo(250, 250)
  context.lineTo(0, 500)
  context.closePath()
  context.fillStyle = "pink"
  context.fill()
 
  context.beginPath()
  context.moveTo(0, 500)
  context.lineTo(125, 375)
  context.lineTo(250, 500)
  context.closePath()
  context.fillStyle = "red"
  context.fill()
  context.stroke()
 
  context.beginPath()
  context.moveTo(125, 375)
  context.lineTo(250, 250)
  context.lineTo(375, 375)
  context.lineTo(250, 500)
  context.closePath()
  context.fillStyle = "purple"
  context.fill()
 
  context.beginPath()
  context.moveTo(250, 250)
  context.lineTo(375, 125)
  context.lineTo(375, 375)
  context.closePath()
  context.fillStyle = "yellow"
  context.fill()
 
  context.beginPath()
  context.moveTo(500, 0)
  context.lineTo(500, 250)
  context.lineTo(375, 375)
  context.lineTo(375, 125)
  context.closePath()
  context.fillStyle = "blue"
  context.fill()
 
  context.beginPath()
  context.moveTo(500, 250)
  context.lineTo(250, 500)
  context.lineTo(500, 500)
  context.closePath()
  context.fillStyle = "black"
  context.fill()
}
</script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>

相關文章