这篇文章主要为大家展示了“canvas中如何使用clip()函数裁剪方法”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“canvas中如何使用clip()函数裁剪方法”这篇文章吧。
未使用裁剪绘制一个圆
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title></title>
<style>
*{margin:0;padding:0;}
html,body{width:100%;height:100%;overflow:hidden;background-color:#AFAFAF;}
</style>
</head>
<body>
<canvasid="canvas"></canvas>
<script>
varcanvas=document.getElementById('canvas'),
context=canvas.getContext('2d');
canvas.width=document.body.clientWidth;
canvas.height=document.body.clientHeight;
context.lineWidth=3;
context.strokeStyle='red';
context.beginPath();
context.arc(200,200,100,(Math.PI/180)*0,(Math.PI/180)*360,false);
context.stroke();
context.closePath();
</script>
</body>
</html>
效果
使用clip()裁剪区域
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title></title>
<style>
*{margin:0;padding:0;}
html,body{width:100%;height:100%;overflow:hidden;background-color:#AFAFAF;}
</style>
</head>
<body>
<canvasid="canvas"></canvas>
<script>
varcanvas=document.getElementById('canvas'),
context=canvas.getContext('2d');
canvas.width=document.body.clientWidth;
canvas.height=document.body.clientHeight;
context.lineWidth=3;
context.strokeStyle='red';
context.rect(0,0,200,200);
context.clip();
context.beginPath();
context.arc(200,200,100,(Math.PI/180)*0,(Math.PI/180)*360,false);
context.stroke();
context.closePath();
</script>
</body>
</html>
效果
也可以使用arc绘制圆形的剪裁区域
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title></title>
<style>
*{margin:0;padding:0;}
html,body{width:100%;height:100%;overflow:hidden;background-color:#AFAFAF;}
</style>
</head>
<body>
<canvasid="canvas"></canvas>
<script>
varcanvas=document.getElementById('canvas'),
context=canvas.getContext('2d');
canvas.width=document.body.clientWidth;
canvas.height=document.body.clientHeight;
context.lineWidth=3;
context.strokeStyle='red';
context.arc(100,100,150,(Math.PI/180)*0,(Math.PI/180)*360,false);
context.clip();
context.beginPath();
context.arc(200,200,100,(Math.PI/180)*0,(Math.PI/180)*360,false);
context.stroke();
context.closePath();
</script>
</body>
</html>
效果
使用save和restore实现只裁剪单个路径
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title></title>
<style>
*{margin:0;padding:0;}
html,body{width:100%;height:100%;overflow:hidden;background-color:#AFAFAF;}
</style>
</head>
<body>
<canvasid="canvas"></canvas>
<script>
varcanvas=document.getElementById('canvas'),
context=canvas.getContext('2d');
canvas.width=document.body.clientWidth;
canvas.height=document.body.clientHeight;
context.lineWidth=3;
context.strokeStyle='red';
context.save();
context.rect(0,0,200,200);
context.clip();
context.beginPath();
context.arc(200,200,100,(Math.PI/180)*0,(Math.PI/180)*360,false);
context.stroke();
context.closePath();
context.restore();
context.beginPath();
context.arc(250,250,100,(Math.PI/180)*0,(Math.PI/180)*360,false);
context.stroke();
context.closePath();
</script>
</body>
</html>
以上是“canvas中如何使用clip()函数裁剪方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!