背景颜色必须随我所画的颜色而改变。
因此,如果我在画布上使用红色绘图,我想单击clear按钮,画布div将清除所有绘图,并将背景颜色更改为红色。
这是我目前所拥有的,但我不知道如何将颜色更改为我所选择的颜色
function ClearCanvas(sColour) {
DebugMessage("Clear Canvas");
const context = oCanvas.getContext('2d');
context.clearRect(0, 0, oCanvas.width, oCanvas.height);
document.getElementById("1Canvas").style.background = "____";
}发布于 2018-11-12 02:57:50
在画布上绘制颜色时,需要将fillStyle更改为所需的颜色。更改画布的背景色将不起作用。
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const colors = ["blue", "green", "red", "pink", "purple", "grey", "black"];
function draw(color) {
//This line is actually not even needed...
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.rect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = color;
ctx.fill();
}
let i = 0;
setInterval(() => {
draw(colors[i]);
i = (i + 1) % colors.length;
}, 1000);<canvas id="canvas">
https://stackoverflow.com/questions/53251958
复制相似问题