0
在以下示例中,我已將每個圖像像素的不透明度設置爲零。爲什麼灰色背景矩形不可見,我該如何實現?我是否在形狀與圖像數據的渲染過程中遺漏了一些東西?帆布圖形上的圖像數據不透明度
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<body>
<canvas id='myCanvas'></canvas>
</body>
<script type="text/javascript">
var width = 500;
var height = 500;
var canvas = document.getElementById('myCanvas')
context = canvas.getContext("2d"),
canvas.width = width;
canvas.height = height;
context.fillStyle = "grey";
context.fillRect(0,0,100,100);
var imageData = context.createImageData(width, height);
for (var i = 0, l = 0; i<height; ++i) {
for (j = 0; j<width; ++j, l += 4) {
imageData.data[l+0] = Math.round(Math.random() * 255);
imageData.data[l+1] = Math.round(Math.random() * 255);
imageData.data[l+2] = Math.round(Math.random() * 255);
imageData.data[l+3] = 0;
}
}
context.putImageData(imageData, 0, 0);
</script>
偉大的答案!非常感謝。 – spyrostheodoridis