使用<canvas>
标签,我需要能够在多边形中画一个洞。
现在我有一些非常简单的东西,它使用beginPath()方法,然后对每个点做lineTo()。然后用fill()填充。
但是,我无法想到任何一种方法来填充多边形并留出未填充的中间区域,就像一个甜甜圈一样。 虽然我不是在制作甜甜圈,但这个例子很适合说明问题。
是否有什么我遗漏的地方? 我宁愿不完全填充然后重新绘制中间部分。
使用<canvas>
标签,我需要能够在多边形中画一个洞。
现在我有一些非常简单的东西,它使用beginPath()方法,然后对每个点做lineTo()。然后用fill()填充。
但是,我无法想到任何一种方法来填充多边形并留出未填充的中间区域,就像一个甜甜圈一样。 虽然我不是在制作甜甜圈,但这个例子很适合说明问题。
是否有什么我遗漏的地方? 我宁愿不完全填充然后重新绘制中间部分。
这是我让它工作的方法:
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;外部多边形必须是顺时针方向,孔洞必须是逆时针方向。
使用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: 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
你可以使用 奇偶规则 来进行填充: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;
}
<canvas id="canvas"><canvas>
以顺时针方向绘制您的形状(例如)。然后,您可以增加或减小半径并逆时针方向移动。
编辑:这里是一个非常简单的甜甜圈示例:
<canvas id="cvs" width="300" height="300">[No canvas support]</canvas>
<script>
// Get the context
context = document.getElementById('cvs').getContext('2d');
// Start a new path
context.beginPath();
// Draw a circle with a radius of 100 in the
// clockwise direction
context.arc(150,150,100,0, 2 * Math.PI, false);
// Now draw another circle with a smaller
// radius and in the anti-clockwise direction
context.arc(150,150,60,2 * Math.PI, 0, true);
// Fill the resultant shape
context.fill();
</script>
这个页面上有结果展示的演示:
https://www.rgraph.net/canvas/reference/arc.html#how-to-draw-a-donut-shape
为了更好地理解它,将context.fill()
更改为context.stroke()
,并将两个2 * Math.PI
都更改为1.5 * Math.PI
可能会有所帮助。这使示例更加清晰。