canvas立體效果的圓環

admin發表於2018-05-20
分享一段程式碼例項,它利用canvas實現了立體圓環效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
canvas {
  border:2px dotted #ccc;
}
</style>
</head>
<body>
<canvas id="canvas" width="550" height="450"></canvas>
<script type="text/javascript">
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
function drawTwoArcs() {
  context.beginPath();
  context.arc(300, 190, 150, 0, Math.PI * 2, false);
  context.arc(300, 190, 100, 0, Math.PI * 2, true);
 
  context.fill();
}
 
function draw() {
  context.clearRect(0, 0, context.canvas.width, context.canvas.height);
  context.save();
  context.shadowColor = "rgba(0,0,0,0.8)";
  context.shadowOffsetX = 12;
  context.shadowOffsetY = 12;
  context.shadowBlur = 15;
 
  drawTwoArcs();
  context.restore();
}
 
context.fillStyle = "rgba(100,140,230,0.5)";
draw();
</script>
</body>
</html>

相關文章