使用fillrect我使用的是彼此的位置分隔2个画布元素,我试图使用fillRect两队战平他们每人使用fillRect不同的颜色。问题是只有一个画布被填充,使用fillRect(即在下面的代码示例中为“蓝色”)。我可以使用不同的颜色使用css style = background-color,但我更感兴趣的是为什么我fillRect在这种情况下不适用于我。多帆布
感谢您的帮助。
这里是我的HTML代码
<body style="position:relative">
<div id='c1' style="position:absolute; top:0px; left:0px; z-index:1">
<canvas id='canvas1' width='150' height='150'>
Your browser does not support HTML5 Canvas.
</canvas>
</div>
<div id='c2'style="position:absolute; top:0px; left:200px; z-index:2">
<canvas id='canvas2' width='150' height='150'>
Your browser does not support HTML5 Canvas.
</canvas>
</div>
</form>
这里是我的JavaScript代码的脚本标签中:
window.onload = function() {
// get all canvas elements
var canvasList = document.getElementsByTagName("canvas");
var ctx = canvasList[0].getContext("2d");
var ctx2 = canvasList[1].getContext("2d");
//canvas1
ctx.fillStyle = "blue";
ctx.fillRect(0,0,150,150);
//canvas2
ctx2.fillStyle = "red";
ctx2.fillRect(200,0,150,150);
}
尝试分配每个画布不同的ID,然后使用'的document.getElementById(“cavas1 “);'。 – Zak