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的图片处理

如何用html5中的canvas绘制渐变矩形

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

如何使用canvas画一个微笑的表情(代码示例)

canvas实现九宫格心形拼图的方法(附代码)

canvas转存为图片实例教程

canvas画直角坐标系

canvas的绘图api使用详解

关于html5 canvas的事件处理

图文详解如何用canvas画实心圆和空心圆

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




打赏

取消

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

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

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

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

评论

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