2014-09-30 127 views

回答

3

您可以輕鬆地在rgba,其中最後一個參數將是字母或不透明度達到中用圓圈的填充樣式此屬性。語法將是這樣的

circle.fillStyle = "rgba(255, 255, 255, 0.5)"; 
//can be used to fill as red object with opacity of 0.5 

達到預期效果的完整代碼將是這樣的。

// JavaScript Code 
 

 
var canvas = document.getElementById('myCanvas'); 
 
var context = canvas.getContext('2d'); 
 
var centerleftX = canvas.width/4; 
 
var centerRightX = 3 * canvas.width/4; 
 
var centerY = canvas.height/2; 
 
var radius = 70; 
 

 
context.beginPath(); 
 
context.rect(0, 0, 400, 200); 
 
context.fillStyle = 'rgb(200,200,200)'; 
 
context.fill(); 
 
context.lineWidth = 7; 
 
context.strokeStyle = 'black'; 
 
context.stroke(); 
 

 

 
context.beginPath(); 
 
context.arc(centerleftX + 50, centerY, radius, 0, 2 * Math.PI, false); 
 
context.fillStyle = "rgba(0, 0,255,0.7)"; 
 
context.fill(); 
 

 

 
context.beginPath(); 
 
context.arc(centerRightX - 50, centerY, radius, 0, 2 * Math.PI, false); 
 
context.fillStyle = "rgba(255, 0,0,0.7)"; 
 
context.fill();

 
<!-- HTML Code --> 
 
<canvas id="myCanvas" width="400" height="200">

+1

完美的解決方案!非常感謝! – 2014-09-30 10:52:40