如果在画布2d上下文连续绘制多次相同的图形,比如
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>画布简单实践</title>
</head>
<body>
<div>
<canvas id="canvas"></canvas>
</div>
<script>
window.onload = function() {
var canvas = document.getElementById('canvas');
canvas.width = 500;
canvas.height = 500;
var ctx = canvas.getContext('2d');
// function loop() {
// ctx.fillStyle = "rgb(0, 0, 0, 0.05)";//画布黑设定为色;
// ctx.fillRect(0, 0, canvas.width, canvas.height);//画布设定为矩形;
// requestAnimationFrame(loop);
// }
// loop();
for(var i = 0; i < 5; i ++) {
ctx.fillStyle = "rgb(0, 0, 0, 0.5)";//画布设定为黑色,透明度0.5;
ctx.fillRect(0, 0, canvas.width, canvas.height);//画布设定为矩形;
}
}
</script>
</body>
</html>
像这样连续绘制5个半透明的黑色矩形,叠加的效果最终就是一个接近黑色的矩形;