Using the SVG evenodd fill-rule in the canvas elem

2019-03-30 07:26发布

Does anyone know if it's possible, either natively or mathematically with JavaScript, to implement the evenodd fill-rule from SVG in the HTML5 canvas element? Links to projects that do this would be helpful.

4条回答
Evening l夕情丶
2楼-- · 2019-03-30 08:05

I asked myself the same question and came across this Mozilla Bugreport.

Seems as it gets proposed to the WHATWG (canvas specs) by the bugreporters:

Chris Jones, 2011-06-10:

Let's wait for docs until this spec is looking solid on whatwg (I'll post today).

查看更多
放荡不羁爱自由
3楼-- · 2019-03-30 08:09

Yes, it should be possible with globalCompositeOperation. If I'm not mistaken, default "source-over" value should correspond to SVG's "evenodd" (otherwise, try few others one and see if resulting image looks identical).

查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-30 08:10

By mathematically do you mean an algorithm implementation ? It is certainly possible, see http://gpolo.awardspace.info/fill/main.html. It is more a demo than anything, but it solves this issue "mathematically".

查看更多
疯言疯语
5楼-- · 2019-03-30 08:20

That's a really old question, so things must have been different at the time, but since a long time ago, you can pass an fillrule parameter to the fill() method. This fillrule can be either "nonzero", the default, or "evenodd".

var ctx = c.getContext('2d');
drawPath();
ctx.fill();
ctx.translate(70, 0);
drawPath();
ctx.fill('evenodd');
ctx.translate(70, 0);
drawPath();
ctx.stroke();

function drawPath(){
  ctx.beginPath();
  ctx.arc(30,30,20,0,Math.PI*2);
  ctx.lineTo(60,60);
  ctx.lineTo(0,0);
  ctx.closePath();
}
<canvas id="c"></canvas>

查看更多
登录 后发表回答