本文摘自PHP中文网,作者小云云,侵删。
在canvas中,可以使用clip()函数裁剪区域,设定裁剪区域后,只有在区域内的图像才能显示,其余部分会被屏蔽掉。本文主要和大家介绍了canvas裁剪clip()函数的具体使用的相关资料,希望能帮助到大家。未使用裁剪绘制一个圆
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!DOCTYPE html>
<html lang= "en" >
<head>
<meta charset= "UTF-8" >
<title></title>
<style>
*{margin:0; padding:0;}
html, body{width:100%; height:100%; overflow:hidden; background-color:#AFAFAF;}
</style>
</head>
<body>
<canvas id= "canvas" ></canvas>
<script>
var canvas = 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()裁剪区域
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html>
<html lang= "en" >
<head>
<meta charset= "UTF-8" >
<title></title>
<style>
*{margin:0; padding:0;}
html, body{width:100%; height:100%; overflow:hidden; background-color:#AFAFAF;}
</style>
</head>
<body>
<canvas id= "canvas" ></canvas>
<script>
var canvas = 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>
|
效果

阅读剩余部分
相关阅读 >>
如何用html5中的canvas实现渐变文字的效果
canvas贝塞尔公式推导与物体跟随复杂曲线轨迹运动
canvas 动态图表
js html5 canvas绘制图片的方法
canvas中beginpath()和closepath()作用的实例解析
html5 canvas基本绘图之绘制线条
怎么用canvas压缩图片
基于 html5 canvas 实现的文字动画特效
h5 canvas中fill 与stroke文字效果实现实例
关于html5 canvas旋转动画的2个例子
更多相关阅读请进入《canvas》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » canvas中使用clip()函数裁剪方法