设置fillStyle属性值为RGBA颜色值
<script>
window.onload = function () {
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
//设置矩形的透明度
ctx.fillStyle = "rgba(250, 0, 0, 0.5)";
ctx.fillRect(10, 10, 100, 50);
ctx.stroke();
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
</body>
</html>