English 中文(简体)
为什么在反转路径时填充会产生不同的结果?
原标题:Why does filling yield a different result when reversing the path?

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

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/。然而,令人着迷的是,重叠的空间变成了白色(一般来说,是背景色)。

如果我用另一种方式定义一条路径,它基本上与它所截断的区域相同,那么为什么在填充时会产生不同的结果呢?

最佳回答

这种填充行为被称为“非零缠绕规则”维基百科上有一个页面

规范定义了这种行为。搜索本页规范关于“绕组”。

问题回答

暂无回答




相关问题
How can I improve the performance of my HTML5 2D canvas?

I ve made a "biome gridmap playground" app which help to design biomes for a 2D grid map using simplex noise. The algorithm is roughly this: for each grid map tile, we look at the noises ...

Strange HTML5 Canvas drawImage behaviour

I am writing some code that uses HTML5 canvas. Generally it works well, but now I found a very strange behaviour. The weird thing is that it is consistent on different browsers, so must be that I ...

How to flip images horizontally with HTML5

In IE, I can use: <img src="http://example.com/image.png" style="filter:FlipH"> to implement an image flip horizontally. Is there any way to flip horizontally in HTML5? (maybe by using canvas?...

HTML5 Canvas: How to fake globalCompositeOperation="darker"

I ve googled and googled about this, and all I can find, including on StackOverflow, is "support was and is broken in most major browsers." Not an actual solution to my problem. This month s Playboy ...

Composite operations in Android Canvas

I m just starting with Android development and I m coming from JavaScript/HTML world so I m currently investigating the possibilities of the Android SDK. The HTML 5 canvas supports composite ...

What is the HTML5 canvas?

I m a little confused to what HTML5 canvas is. I ve been told it is JavaScript, but it seems to be a much bigger deal? What makes it different than javascript? Why is it so amazing? Does it do ...

How to clear the canvas for redrawing

After experimenting with composite operations and drawing images on the canvas I m now trying to remove images and compositing. How do I do this? I need to clear the canvas for redrawing other images;...

Web Workers and Canvas

Are web workers allowed to access a canvas object?

热门标签