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()函数裁剪方法的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

canvas与h5如何实现视频截图功能

html5 canvas如何绘制动态径向渐变

如何解决canvas绘图时遇到的跨域问题

canvas制作旋转太极的动画

分享利用canvas实现知乎登录页的实例代码

如何用html5中的canvas实现渐变文字的效果

html5 canvas api中drawimage()方法的使用代码实例分享(图)

如何在canvas里面基于随机点绘制一个多边形

html5 canvas标签的作用以及canvas标签的历史由来介绍

canvas怎么使用

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




打赏

取消

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

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

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

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

评论

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