为什么Canvas会留下幽灵般的光环?

发布于 2024-10-07 19:54:53 字数 1032 浏览 1 评论 0原文

我正在尝试使用 Canvas,将不同颜色的标记放置在网格上,并尝试删除它们。

我目前正在尝试通过在令牌上用白色绘制一个尺寸完全相同的圆圈来删除令牌。这会在原始圆圈所在的位置留下一个“幽灵环”(单像素轮廓),随着连续应用白色圆圈而消失。

Ring example

2, -1 中的圆是最初绘制的,根本没有过度绘制。 3, -1 中的圆已被覆盖一次,4, -1 中的圆已被覆盖两次,依此类推至 7, -1。

Chrome 和 Firefox 3.6 中都会出现此行为,

我的代码如下。

   function placeToken(e) {
        var click = getClick(e);

        var gridCord = getGridCord(click);

        var canvas = e.currentTarget;
        var ctx = canvas.getContext(CONTEXT_NAME);

        ctx.fillStyle = color;
        ctx.strokeStyle = color; //tried with and without this line, no effect

        x = (gridCord.x * spacing) + (spacing / 2);
        y = (gridCord.y * spacing) + (spacing / 2);


        ctx.beginPath();
        ctx.arc(x, y, (spacing - tokenEdge) / 2, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.fill();
        ctx.stroke();  //tried with and without this line.  Same result
    };

为什么画布上会留下这个幽灵环,我该如何去除它?

I'm experimenting with Canvas, placing tokens of different colors on a grid, and trying to remove them.

I'm currently trying to remove the token by drawing a circle of the exact same dimensions in white over the token. This is leaving a "ghostly ring" (single pixel outline) where the original circle was, that disappears with successive applications of the white circle.

Ring example

The circle in 2, -1 is originally drawn, and not at all overpainted. The circle in 3, -1 has been overpainted once, the circle in 4, -1 has been overpainted twice, and so on to 7, -1.

This behavior occurs in both Chrome and Firefox 3.6

My code follows.

   function placeToken(e) {
        var click = getClick(e);

        var gridCord = getGridCord(click);

        var canvas = e.currentTarget;
        var ctx = canvas.getContext(CONTEXT_NAME);

        ctx.fillStyle = color;
        ctx.strokeStyle = color; //tried with and without this line, no effect

        x = (gridCord.x * spacing) + (spacing / 2);
        y = (gridCord.y * spacing) + (spacing / 2);


        ctx.beginPath();
        ctx.arc(x, y, (spacing - tokenEdge) / 2, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.fill();
        ctx.stroke();  //tried with and without this line.  Same result
    };

Why does canvas leave this Ghostly ring, and how can I get rid of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜爱纠缠 2024-10-14 19:54:53

简而言之,抗锯齿。该圆圈边缘的像素以低于 100% 的不透明度绘制。这并不是画布独有的。过去,在 Windows 图形 API 执行抗锯齿功能之前编写和测试的 Windows 应用程序在执行抗锯齿功能的 Windows 版本上运行时会留下幽灵般的边界。

您只需在其上绘制一个完全不透明的白色矩形即可。由于矩形没有弯曲的边缘,因此每个像素要么完全漆成白色,要么不受影响——您不会获得抗锯齿效果。

In short, antialiasing. The pixels on the edge of that circle are being painted with less than 100% opacity. This isn't unique to canvas. Back in the day, Windows applications written and tested before Windows graphics APIs did antialiasing would leave ghostly boundaries when run on versions of Windows that did antialiasing.

You just have to paint a completely opaque white rectangle over it. Since a rectangle doesn't have curved edges, every pixel will be either completely painted white or unaffected -- you won't get antialiasing.

小女人ら 2024-10-14 19:54:53

它看起来像抗锯齿,一种在表示时最小化失真伪影(称为锯齿)的技术较低分辨率的高分辨率图像。由于圆圈在绘制之前都是高分辨率的,因此您将获得这些出血效果。

It looks like anti-aliasing, the technique of minimizing the distortion artifacts known as aliasing when representing a high-resolution image at a lower resolution. As a circle is high-resolution until drawn you will get these bleeding effects.

少年亿悲伤 2024-10-14 19:54:53

我没有太多地弄乱画布,但我认为问题发生的原因与 Photoshop 中的原因相同。

绘制圆形时,必须对其进行抗锯齿处理,以防止锯齿状边缘可见。这意味着您最终会在圆周围得到不同程度的半透明像素,以实现平滑度。

当您使用相同的位置和尺寸在圆的顶部绘制时,它本质上是在已经半透明的像素之上绘制半透明像素,留下一圈接近原始颜色(但稍浅)的像素。这就是为什么绘制白色圆圈的次数越多,它就越接近全白块。

I haven't messed with canvas much, but I figure the problem occurs for the same reason it does in Photoshop.

When you draw a circle, it has to be antialiased to prevent jagged edges from being visible. This means you end up with pixels all around the circle that are semi-transparent to varying degrees, in order to achieve smoothness.

When you paint over top of the circle using the same position and dimensions, it's essentially painting semi-transparent pixels on top of already semi-transparent pixels, leaving you with a ring of pixels that is close to the original color (but slightly lighter). That is why the more times you draw the white circle the closer it comes to a fully white block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文