【问题标题】:Polygon with a hole in the middle with HTML5's canvas中间有洞的多边形,带有 HTML5 的画布
【发布时间】:2017-09-29 02:32:57
【问题描述】:

使用<canvas> 标签我需要能够在多边形上画一个洞。

现在我有一些非常简单的方法,它使用 beginPath() 然后为每个点执行 lineTo()。然后用 fill() 填充。

我看不出有什么方法可以让一个填充的多边形中间没有填充,就像一个甜甜圈。 我不是在做甜甜圈,但它适合这个例子。

我有什么遗漏吗? 我宁愿不把它画满,然后不得不重画中间。

【问题讨论】:

标签: html geometry html5-canvas


【解决方案1】:

这就是我让它工作的原因:

var ctx = canvas.getContext("2d");     
ctx.beginPath();

//polygon1--- usually the outside polygon, must be clockwise
ctx.moveTo(0, 0);
ctx.lineTo(200, 0);
ctx.lineTo(200, 200);
ctx.lineTo(0, 200);
ctx.lineTo(0, 0);
ctx.closePath();

//polygon2 --- usually hole,must be counter-clockwise 
ctx.moveTo(10, 10);
ctx.lineTo(10,100);
ctx.lineTo(100, 100);
ctx.lineTo(100, 10);
ctx.lineTo(10, 10);
ctx.closePath();

//  add as many holes as you want
ctx.fillStyle = "#FF0000";
ctx.strokeStyle = "rgba(0.5,0.5,0.5,0.5)";
ctx.lineWidth = 1;
ctx.fill();
ctx.stroke();

这里的主要思想是你只能使用一次 beginPath;外部多边形必须是顺时针方向,孔必须是逆时针方向。

【讨论】:

  • 效果很好,但我现在想知道是否有机会按顺时针或逆时针顺序排列点。我找到了一些算法,但没有一个不适用于非凸多边形。
  • 在我看来,外部多边形的方向不必是顺时针,孔的方向不必是逆时针——它们只需相反。上下文假定第一个输入多边形的方向为 right 方向,至少在 Chrome 中是这样。
  • 如果有人渲染 GeoJSON 多边形,它可以很好地工作而无需重新排序点。我想,编辑们正确地重新排序了。
【解决方案2】:

您有 2 种选择来使用 HTML5 画布绘制孔。

选择 1:
绘制不同时钟方向的外部形状和内部形状。

外部形状顺时针,内部形状逆时针。
或者外部形状顺时针,内部形状逆时针。

ctx.beginPath();
//outer shape, clockwise
ctx.moveTo(100,20);
ctx.lineTo(200,200);
ctx.lineTo(20,200);
ctx.closePath();
//inner shape (hole), counter-clockwise
ctx.moveTo(100,100);
ctx.lineTo(70,170);
ctx.lineTo(140,170);
ctx.closePath();
//fill
ctx.fillStyle = "#FF0000";
ctx.fill();

我们在编码的时候检测形状绘制方向有点痛苦。

如果你需要检测一系列点是否顺时针,这里有一个很好的功能:

function isClockwise(dots){
    var sum = 0;
    for(var i=1, n=dots.length; i<n; i++){
        sum += (dots[i][0] - dots[i-1][0]) * (dots[i][1] + dots[i-1][1]);
    }
    sum += (dots[0][0] - dots[n-1][0]) * (dots[0][1] + dots[n-1][1]);
    return sum < 0;
}
console.log(isClockwise([[100,20], [200,200], [20,200]]));  //true
console.log(isClockwise([[100,20], [20,200], [200,200]]));  //false

如果你的点是顺时针的,但你需要逆时针,.reverse() 你的点数组。

var dots = [[100,20], [200,200], [20,200]];
dots.reverse();

选择 2:
使用“偶数”填充规则,向任何方向绘制形状。

这种方式比选择1简单多了。
fill() method API

   void ctx.fill();
   void ctx.fill(fillRule);
   void ctx.fill(path, fillRule);

fillRule 可以是 "nonzero""evenodd"
"nonzero":非零缠绕规则,默认规则。
“evenodd”:奇偶缠绕规则。

ctx.beginPath();
//outer shape, any direction, this sample is clockwise
ctx.moveTo(100,20);
ctx.lineTo(200,200);
ctx.lineTo(20,200);
ctx.closePath();
//inner shape (hole), any direction, this sample is clockwise
ctx.moveTo(100,100);
ctx.lineTo(140,170);
ctx.lineTo(70,170);
ctx.closePath();
//fill
ctx.fillStyle = "#FF0000";
ctx.mozFillRule = 'evenodd'; //for old firefox 1~30
ctx.fill('evenodd'); //for firefox 31+, IE 11+, chrome

【讨论】:

  • 你是最棒的。拯救了我的一天
【解决方案3】:

您可以使用evenodd 填充规则:fill('evenodd')

// properties
// - outer square
var outerLength = 200;
// - inner length
var innerLength = outerLength / 2;

// cnavas
var canvas = document.getElementById('canvas');
var width = canvas.width = document.body.clientWidth;
var height = canvas.height = document.body.clientHeight;
var context = canvas.getContext('2d');

// path
// - outer square
context.rect(
  (width - outerLength) / 2,
  (height - outerLength) / 2,
  outerLength,
  outerLength
);
// - inner square
var x0 = (width - innerLength) / 2;
var y0 = (height - innerLength) / 2;
context.moveTo(x0, y0);
context.rect(
  x0,
  y0,
  innerLength,
  innerLength
);

// draw
// - stroke
context.lineWidth = 10;
context.stroke();
// - fill
context.fillStyle = 'red';
context.fill('evenodd');
html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}
&lt;canvas id="canvas"&gt;&lt;canvas&gt;

【讨论】:

    【解决方案4】:

    按顺时针方向绘制形状(例如)。然后你可以增加或减少半径,然后逆时针方向走。

    【讨论】:

    • -1 不解释,只发布可能失效的链接是不行的
    • 解释是第一句话。
    • 感谢您发布您的答案!请务必仔细阅读FAQ on Self-Promotion。另请注意,每次链接到您自己的网站/产品时,都要求发布免责声明。
    猜你喜欢
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2011-06-17
    • 2013-07-22
    相关资源
    最近更新 更多