详解HTML5 Canvas绘制时指定颜色与透明度的方法

2019-01-28 15:00:10王旭
  •     </style>    </head>   
  • <body>    <div id="canvas-warp">   
  •     <canvas id="canvas">            你的浏览器居然不支持Canvas?!赶快换一个吧!!   
  •     </canvas>    </div>   
  •    <script>   
  •     window.onload = function(){            var canvas = document.getElementById("canvas");   
  •         canvas.width = 800;            canvas.height = 600;   
  •         var context = canvas.getContext("2d");            context.fillStyle = "#FFF";   
  •         context.fillRect(0,0,800,600);      
  •         context.globalAlpha = 0.5;      
  •         for(var i=0; i<=50; i++){                var R = Math.floor(Math.random() * 255);   
  •             var G = Math.floor(Math.random() * 255);                var B = Math.floor(Math.random() * 255);   
  •                context.fillStyle = "rgb(" + R + "," + G + "," + B + ")";   
  •                context.beginPath();   
  •             context.arc(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 100, 0, Math.PI * 2);                context.fill();   
  •         }        };