到目前为止,我得到了这个:http://jsfiddle.net/Lt7VN/
但它切割/剪辑红色和黑色的部分,而我希望它只是切断黑色矩形,我将如何去做呢?
context.beginPath();
context.rect(20,20,160,200);
context.fillStyle = "red";
context.fill();
context.beginPath();
context.rect(20,20,150,100);
context.fillStyle = "black";
context.fill();
context.globalCompositeOperation = "destination-out";
context.beginPath();
context.arc(100, 100, 50, 0, 2*Math.PI);
context.fill();
答案 0 :(得分:1)
您可以使用合成在1个画布上执行此操作。
这是代码和小提琴:http://jsfiddle.net/m1erickson/F4dp3/
<!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 context=canvas.getContext("2d");
context.save();
context.beginPath();
context.rect(20,20,150,100);
context.fillStyle = "black";
context.fill();
context.globalCompositeOperation = "destination-out";
context.beginPath();
context.arc(100, 100, 50, 0, 2*Math.PI);
context.fill();
context.globalCompositeOperation = "destination-over";
context.beginPath();
context.rect(20,20,160,200);
context.fillStyle = "red";
context.fill();
context.restore();
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>
答案 1 :(得分:0)
两幅画布是我相信的唯一方式 http://jsfiddle.net/Lt7VN/2/
context1.globalCompositeOperation = 'destination-out';
context1.beginPath();
context1.arc(100, 100, 50, 0, 2*Math.PI);
context1.globalAlpha = 1.0;
context1.fill();