更改画布宽度/高度时,将重置画布上下文

时间:2018-03-04 09:59:06

标签: javascript html5-canvas

如果我设置ctx.textBaseline = "top";,然后调整画布大小,则ctx.textBaseline会更改回"alphabetic"。这是预期的行为吗?这个问题让我感到难过,所以我只是好奇。发生在Chrome和Firefox中,所以我最初的猜测是它的预期行为。

Here's a demo of the problem

// textBaseline not working:
let ctx1 = canvas1.getContext("2d");
ctx1.textBaseline = "top";
canvas1.width = 31;
canvas1.height = 31;
ctx1.fillText("hi", 10, 10);

// fixed:
let ctx2 = canvas2.getContext("2d");
canvas2.width = 31;
canvas2.height = 31;
ctx2.textBaseline = "top"; // <-- need to set it *after* resize to get it to work
ctx2.fillText("hi", 10, 10);

谢谢!

0 个答案:

没有答案