我想,以纪念两个圆圈之间的重叠区域(如在维恩图)。 我想做到这一点的方式是通过绘制使用两个交叉点两个圆弧,也比使用填充路径fill()
我所知道的交叉点的坐标,但如何使用作为一个输入arc()
函数?
ctx.beginPath();
ctx.arc(circle1.x,circle1.y,circle1.r, ? , ? ,true);
ctx.fill();
ctx.closePath();
我想,以纪念两个圆圈之间的重叠区域(如在维恩图)。 我想做到这一点的方式是通过绘制使用两个交叉点两个圆弧,也比使用填充路径fill()
我所知道的交叉点的坐标,但如何使用作为一个输入arc()
函数?
ctx.beginPath();
ctx.arc(circle1.x,circle1.y,circle1.r, ? , ? ,true);
ctx.fill();
ctx.closePath();
您可以使用画布绘制globalCompositeOperation 2种形状的交集
掌握globalCompositeOperation允许您控制其显示在画布上新老图纸的部分。
:你可以在这里看到每个合成模式的例子http://www.html5canvastutorials.com/advanced/html5-canvas-global-composite-operations-tutorial/
我们使用这些合成模式2突出你的2圈的交集:
源之上
由于左边的圆圈已经得出,源之上将仅绘制正圆形的交叉部分 。
ctx.globalCompositeOperation="source-atop";
ctx.arc(circle2.x,circle2.y,circle2.r, 0, 2*Math.PI, false);
目的地在
由于左边的圆圈已经绘制,目的地在将利用现有的左边的圆圈下正圆形。
ctx.globalCompositeOperation="destination-over";
ctx.arc(circle2.x,circle2.y,circle2.r, 0, 2*Math.PI, false);
这是一个很大采取,所以你可能注释掉所有的绘制代码,然后取消它一调度研究-AT-A-时间看到每个操作都有什么样的影响。
这里的代码和一个小提琴: http://jsfiddle.net/m1erickson/JGSJ5/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.fillStyle="yellow";
ctx.strokeStyle="black";
ctx.lineWidth=3;
var circle1={x:100,y:100,r:50};
var circle2={x:140,y:100,r:50};
// draw circle1
ctx.save();
ctx.beginPath();
ctx.arc(circle1.x,circle1.y,circle1.r, 0, 2*Math.PI, false);
ctx.stroke();
ctx.fill();
// composite mode "source-atop" to draw the intersection
ctx.beginPath();
ctx.fillStyle="orange";
ctx.globalCompositeOperation="source-atop";
ctx.arc(circle2.x,circle2.y,circle2.r, 0, 2*Math.PI, false);
ctx.fill();
ctx.stroke();
ctx.restore();
// destination-over to draw fill for circle2 again
ctx.beginPath();
ctx.globalCompositeOperation="destination-over";
ctx.arc(circle2.x,circle2.y,circle2.r, 0, 2*Math.PI, false);
ctx.fill();
// back to normal composite mode (newest drawings on top)
ctx.globalCompositeOperation="source-over";
// draw the stroke for circle1 again
ctx.beginPath();
ctx.arc(circle1.x,circle1.y,circle1.r, 0, 2*Math.PI, false);
ctx.stroke();
// draw the stroke for circle2 again
ctx.beginPath();
ctx.arc(circle2.x,circle2.y,circle2.r, 0, 2*Math.PI, false);
ctx.stroke();
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>