欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!

未使用裁剪绘制一个圆

 

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>

效果

也可以使用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>

效果








本文转载自励志网

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h57994.shtml