【问题标题】:Why does filling yield a different result when reversing the path?为什么在反转路径时填充会产生不同的结果?
【发布时间】:2011-09-04 02:05:20
【问题描述】:

我想知道为什么下面会产生一个正方形相互重叠的白色区域:

http://jsfiddle.net/yNTTj/5/

// square 1
ctx.moveTo(  0,   0); // left top
ctx.lineTo(200,   0); // right top
ctx.lineTo(200, 200); // right bottom
ctx.lineTo(  0, 200); // left bottom
ctx.lineTo(  0,   0); // left top

// square 2
ctx.moveTo(100, 100); // left top
ctx.lineTo(100, 300); // left bottom
ctx.lineTo(300, 300); // right bottom
ctx.lineTo(300, 100); // right top
ctx.lineTo(100, 100); // left top

ctx.fill();

所以,第一个正方形是用顺时针定义的路径绘制的,而第二个正方形是用逆时针定义的路径绘制的。

我希望两者都涂成黑色,就像我们以相同的方式定义正方形 2 的顺序会发生什么:http://jsfiddle.net/yNTTj/6/。然而,显然重叠的空间变成了白色(一般来说,背景颜色)。

如果我反过来定义一条路径,它基本上就是它切断的相同区域,那么为什么它在填充时会产生不同的结果呢?

【问题讨论】:

    标签: html5-canvas


    【解决方案1】:

    这种类型的填充行为被称为“非零缠绕规则”。 Wikipedia has a page on it.

    规范定义了该行为。在this page of the specification 中搜索“绕组”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2018-09-01
      • 2017-02-21
      • 1970-01-01
      • 2015-06-30
      相关资源
      最近更新 更多