这是jsfiddle中的一个示例。我首先画了一个红色背景,然后在这张图片上画了绿色条纹。绿色条纹之间的间隙设置为 0 的 alpha。在除 IE 之外的所有浏览器中,生成的图像如下所示:
然而,在 IE 中,它看起来像这样:
即使在 alpha=0 的简单情况下,似乎也不支持混合。有什么我想念的吗?我是否需要自己混合颜色才能在 IE 中进行这项工作?谁能解释这里发生了什么?这是来自jsfiddle 示例的代码...
HTML:
<canvas id="myCanvas" width="100" height="100"></canvas>
JS:
var finalCanvas, finalCanvasCtx, size, drawCanvas, drawCanvasCtx, drawImageData, ctrV, ctrH, ctrVStep, ctrHStep, index;
// final canvas
finalCanvas = document.getElementById("myCanvas");
finalCanvasCtx = finalCanvas.getContext("2d");
size = finalCanvas.width;
// drawing canvas
drawCanvas = document.createElement("canvas");
drawCanvas.width = size;
drawCanvas.height = size;
drawCanvasCtx = drawCanvas.getContext("2d");
drawImageData = drawCanvasCtx.createImageData(size, size);
// draw red background
for (ctrV = 0; ctrV < size; ctrV++) {
for (ctrH = 0; ctrH < size; ctrH++) {
var index = ((ctrV * size) + ctrH) * 4;
drawImageData.data[index] = 255;
drawImageData.data[index+1] = 0;
drawImageData.data[index+2] = 0;
drawImageData.data[index+3] = 255;
}
}
// paint red background to final canvas
drawCanvasCtx.putImageData(drawImageData, 0, 0);
finalCanvasCtx.drawImage(drawCanvas, 0, 0);
// draw green overlay
for (ctrV = 0; ctrV < size; ctrV++) {
for (ctrH = 0; ctrH < size; ctrH++) {
ctrVStep = Math.round(ctrV / 10);
ctrHStep = Math.round(ctrH / 10);
index = ((ctrV * size) + ctrH) * 4;
drawImageData.data[index] = 0;
drawImageData.data[index+1] = 255;
drawImageData.data[index+2] = 0;
if ((ctrVStep % 2) == 0) {
drawImageData.data[index+3] = 255;
} else {
drawImageData.data[index+3] = 0;
}
}
}
// draw green overlay to final canvas
drawCanvasCtx.putImageData(drawImageData, 0, 0);
finalCanvasCtx.drawImage(drawCanvas, 0, 0);