canvas線性漸變程式碼例項

admin發表於2017-02-21
分享一段程式碼例項,它演示了canvas的線性漸變效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
canvas {
  border: dashed 2px #CCC;
}
</style>
<script type="text/javascript">
function $$(id) {
  return document.getElementById(id);
}
function pageLoad() {
  var can = $$('can');
  var cans = can.getContext('2d');
  var gnt1 = cans.createLinearGradient(10, 0, 390, 0);
  gnt1.addColorStop(0, 'red');
  gnt1.addColorStop(0.5, 'green');
  gnt1.addColorStop(1, 'blue');
  cans.fillStyle = gnt1;
  cans.fillRect(10, 10, 380, 280);
}
window.onload = function () {
  pageLoad();
}
</script>
</head>
<body>
<canvas id="can" width="400" height="300"></canvas>
</body>
</html>

相關文章