简体   繁体   English

Javascript Canvas绘制混乱?

[英]Javascript Canvas drawing out of order?

I am attempting do draw some text over a rectangle, however the text is for some reason being drawn before the rectangle. 我试图在矩形上绘制一些文本,但是由于某种原因,该文本是在矩形之前绘制的。 This doesn't seem to happen when I use another shape such as an arc and happens in both Chromium and Firefox so I don't think it's a browser issue, this is my code: 当我使用另一种形状(例如弧形)并且在Chromium和Firefox中都发生时,似乎没有发生这种情况,因此我认为这不是浏览器问题,这是我的代码:

map_context.fillStyle = "#95609F";
map_context.rect(32,32,192,192);
map_context.fill();

map_context.fillStyle = "white";
map_context.fillText("+", 128, 128);

Try to replace these two lines: 尝试替换这两行:

map_context.rect(32,32,192,192);
map_context.fill();

with just: 只是:

map_context.fillRect(32,32,192,192);

You probably run this same code in a loop to draw several images but forget to clear the path each time. 您可能会在循环中运行相同的代码以绘制多个图像,但是每次都忘记清除路径。 For paths (such as rect() adds to) you need to use beginPath() , so an optional approach would be: 对于路径(例如rect()添加到),您需要使用beginPath() ,因此可选方法是:

map_context.beginPath();
map_context.rect(32,32,192,192);
map_context.fill();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM