Nic*_*ick 7 html5 geometry html5-canvas
使用<canvas>标签我需要能够在多边形中绘制一个洞.
现在我有一些非常简单的东西,使用beginPath()然后为每个点执行lineTo().然后填充fill().
我看不到有任何方法可以将填充的多边形与未填充的中间区域相比,例如甜甜圈.我不是做甜甜圈,但它适合这个例子.
有什么我想念的吗?我宁愿不画它完全填充然后必须重绘中间.
Gis*_*way 24
这就是我的工作:
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();
Run Code Online (Sandbox Code Playgroud)
这里的主要思想是你只能使用一次; 外部多边形必须是顺时针方向,孔必须是逆时针方向.
cui*_*ing 18
你有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();
Run Code Online (Sandbox Code Playgroud)
在编码时检测形状绘制方向有点痛苦.
如果您需要检测一系列点是否顺时针,这是一个很好的功能:
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
Run Code Online (Sandbox Code Playgroud)
如果你的点是顺时针方向,但你需要逆时针方向,那么.reverse()你的点数组.
var dots = [[100,20], [200,200], [20,200]];
dots.reverse();
Run Code Online (Sandbox Code Playgroud)
选择2:
使用'evenodd'填充规则,在任何方向绘制形状.
这种方式比选择简单得多1.
请参阅fill()方法API:
void ctx.fill();
void ctx.fill(fillRule);
void ctx.fill(path, fillRule);
Run Code Online (Sandbox Code Playgroud)
fillRule可以是"非零"或"偶数"
"非零":非零缠绕规则,这是默认规则.
"偶数":奇怪的缠绕规则.
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
Run Code Online (Sandbox Code Playgroud)
您可以使用偶数填充规则: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');Run Code Online (Sandbox Code Playgroud)
html,
body {
margin: 0;
height: 100%;
overflow: hidden;
}Run Code Online (Sandbox Code Playgroud)
<canvas id="canvas"><canvas>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8102 次 |
| 最近记录: |