canvas徑向漸變程式碼例項

admin發表於2017-02-24
分享一段程式碼例項,它演示了徑向漸變效果。

程式碼如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
</head>
<body>
<canvas id="drawing"></canvas>
<script>
var drawing = document.getElementById("drawing");
if (drawing.getContext) {
  var context = drawing.getContext("2d");
  //建立漸變例項
  var gradient = context.createRadialGradient(55, 55, 10, 55, 55, 30);
  gradient.addColorStop(0, "white");
  gradient.addColorStop(1, "black");
  //繪製漸變矩形
  context.fillStyle = gradient;
  context.fillRect(30, 30, 50, 50);
}
</script>
</body>
</html>

相關文章