canvas中使用clip()函数裁剪方法


当前第2页 返回上一页

也可以使用arc绘制圆形的剪裁区域


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.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实现只裁剪单个路径


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

29

30

31

32

33

34

<!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.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>

效果

相关推荐:

使用HTML5 Canvas API中的clip()方法裁剪区域图像代码实例

以上就是canvas中使用clip()函数裁剪方法的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

基于 html5 canvas 的 3d 渲染引擎界面以及吸附等效果的运用

canvas文字怎么换行?canvas文字换行的方法介绍

微信小程序-canvas生成图片并保存到本地

html5利用canvas绘画二级树形结构图

canvas中beginpath()和closepath()作用的实例解析

canvas实现二维码和图片合成的示例代码

html5 canvas标签是什么意思?canvas标签使用方法介绍

利用html5中的canvas绘制笑脸的代码

html5在canvas中实现自定义路径动画详解

html5 canvas的事件处理介绍

更多相关阅读请进入《canvas》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...