如何在HTMLCanvas中指定背景和文字颜色?

How to specify background and text colors in HTML Canvas?

如何为背景和文本指定颜色?

const canvas = createCanvas(200, 200);
const ctx = canvas.getContext("2d");
ctx.font = "30px Impact";
ctx.fillText("Awesome!", 50, 100);

我只需要更改 fillStyle - 它设置颜色,然后用颜色绘制背景,然后更改颜色,然后绘制文本:

const canvas = createCanvas(200, 200);
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fillRect(0, 0, 200, 200);
ctx.font = "30px Impact";
ctx.fillStyle = "rgba(255,255,255,1)";
ctx.fillText("Awesome!", 50, 100);