gpt4 book ai didi

javascript - 如何剪切/剪辑形状并显示其背后的形状?

转载 作者:行者123 更新时间:2023-11-28 01:47:01 25 4
gpt4 key购买 nike

到目前为止我得到了这个:http://jsfiddle.net/Lt7VN/

enter image description here

但是它会剪切/剪辑红色和黑色矩形,而我希望它只剪切黑色矩形,我该怎么做?

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();

最佳答案

您可以使用合成在 1 个 Canvas 上执行此操作。

  • 绘制黑色矩形
  • 将合成设置为“删除”的目标输出
  • 绘制圆弧(删除黑色矩形的一部分)
  • 将合成设置为“绘制在后面”的目标
  • 绘制红色矩形(填充弧切矩形后面。

enter image description here

这是代码和 fiddle :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>

关于javascript - 如何剪切/剪辑形状并显示其背后的形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20148600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com