国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > HTML5 > 正文

canvas裁剪clip()函數(shù)的具體使用

2024-09-05 07:22:23
字體:
供稿:網(wǎng)友

在canvas中,可以使用clip()函數(shù)裁剪區(qū)域,設(shè)定裁剪區(qū)域后,只有在區(qū)域內(nèi)的圖像才能顯示,其余部分會(huì)被屏蔽掉

未使用裁剪繪制一個(gè)圓

<!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()裁剪區(qū)域

<!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繪制圓形的剪裁區(qū)域

<!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實(shí)現(xiàn)只裁剪單個(gè)路徑

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

效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 准格尔旗| 萝北县| 双柏县| 苗栗市| 双江| 临沭县| 肥东县| 新竹市| 郸城县| 东台市| 泸溪县| 五台县| 岑巩县| 黔江区| 四川省| 晋宁县| 滁州市| 泸溪县| 横峰县| 嵊泗县| 迁安市| 丽江市| 晋宁县| 库车县| 拉萨市| 西宁市| 仙居县| 太和县| 吉木萨尔县| 鸡东县| 堆龙德庆县| 若尔盖县| 新丰县| 林甸县| 东乌| 桃源县| 五大连池市| 峨山| 岑溪市| 大庆市| 长白|