简体   繁体   English

产生不同尺寸的电弧的相同呼吁

[英]Identical calls to arc producing different sizes

I'm trying to figure out why drawing a shape, then drawing over it in a new color (as though to highlight it), and then re-drawing the original (un-highlighting it) is leaving traces of the highlighted color. 我试图弄清楚为什么要绘制形状,然后以新的颜色对其进行绘制(好像要突出显示它),然后重新绘制原始形状(不突出显示它)会留下突出显示的颜色的痕迹。

I've reproduced the issue in this fiddle . 我在这个小提琴中转载了这个问题。 The wedge is drawn in a light-blue color. 楔形以浅蓝色绘制。 There's a red button that'll draw over it in red, then another button that re-draws the original shape. 有一个红色按钮将用红色绘制它,然后是另一个按钮以重新绘制原始形状。 All parameters are identical (except for the color), but yet after clicking the button to reset the color, there's a faint trace of red over the wedge. 所有参数都是相同的(颜色除外),但是在单击按钮以重置颜色之后,楔形上有淡淡的红色痕迹。

Before: 之前:

在此处输入图片说明

After: 后:

在此处输入图片说明

Here's the relevant code: 以下是相关代码:

drawWedge(250, 250, 200, 0, 18, "rgb(150, 254, 223)");

$("#red").click(function () {
    drawWedge(250, 250, 200, 0, 18, "rgb(255, 0, 0)");
});

$("#back").click(function () {
    drawWedge(250, 250, 200, 0, 18, "rgb(150, 254, 223)");
});

function d2r(degrees) {
    return degrees * (Math.PI / 180.0);
}

function drawWedge(centerX, centerY, r, start, end, color) {
    context.beginPath();
    context.moveTo(centerX, centerY);
    context.arc(centerX, centerY, r, d2r(start), d2r(end), false);
    context.closePath();
    context.fillStyle = color;
    context.fill();
}

This question was already answered, but I wanted to give a little more thorough explanation. 这个问题已经回答了,但是我想给出更彻底的解释。

在此处输入图片说明

When you draw at a diagonal, your passing through "parts" of pixels (show in my example). 当您在对角线处绘制时,您通过的像素的“部分”(在我的示例中显示)。 So what does the browser do to the part of the pixel outside of the shape? 那么浏览器如何处理形状外部的像素部分? It uses anti-aliasing (anti-aliasing is always on by default for browsers) to color the rest of the pixel (if you didnt have anti-aliasing the line would look jagged). 它使用抗锯齿功能(浏览器默认情况下始终启用抗锯齿功能)为其余像素着色(如果您没有抗锯齿功能,则该行会呈锯齿状)。 If you notice, the faint trace of red is not a bright red because its getting blended due to anti-aliasing. 如果您注意到,红色的微弱痕迹不是亮红色,因为由于抗锯齿而使其融合。 And the reason you see it is because when you draw your shape on the canvas, the faint trace of red is not part of your shape, its part of the pixel on the outside of your shape. 之所以会看到它,是因为在画布上绘制形状时,红色的淡淡痕迹不是形状的一部分,而是像素在形状外部的一部分。

Now as the answer mentioned, you can call clearRect to clear the canvas. 现在,正如提到的答案,您可以调用clearRect清除画布。 However, you should read this SO question as it explains things in more detail (the selected answer is not as good as the second answer). 但是,您应该阅读此SO问题,因为它会更详细地解释事物(所选答案不如第二个答案好)。 Also, ever wonder why they call it a "canvas"? 另外,有没有想过为什么他们称其为“画布”? Think of an actual art canvas used by artists, once they paint on the canvas there is no way to take it off unless you get a new canvas or paint over it! 想一想艺术家使用的实际艺术画布,一旦他们在画布上绘画,就无法脱掉它,除非您得到新的画布或在其上绘画!

When drawing on canvas, it just keeps stacking things on top of each other until you clear it . 在画布上绘画时,它会一直堆叠在一起直到清除为止 The easiest way to clear it is ctx.clearRect(0,0,width,height) 清除它的最简单方法是ctx.clearRect(0,0,width,height)

I put that in your drawWedge function here: 我把它放在你的drawWedge函数中:

http://jsfiddle.net/X7deh/1 http://jsfiddle.net/X7deh/1

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

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