自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一篇文章帶你了解SVG <clippath>剪切路徑

開發(fā) 前端
SVG剪切路徑(也稱為SVG剪切)用于根據(jù)特定路徑剪切SVG形狀。路徑內(nèi)部的形狀部分可見,外部的部分不可見。

[[357015]]

SVG剪切路徑(也稱為SVG剪切)用于根據(jù)特定路徑剪切SVG形狀。路徑內(nèi)部的形狀部分可見,外部的部分不可見。

一、剪輯路徑

這是一個簡單的剪輯路徑。

SVG代碼:

  1. <!DOCTYPE html> 
  2. <html> 
  3.     <head> 
  4.         <meta charset="utf-8"
  5.         <title>項目</title> 
  6.     </head> 
  7.     <body style="background-color: aqua;"
  8.         <svg width="200" height="100" style="border: 1px solid #cccccc;"
  9.             <defs> 
  10.                 <clippath id="clipPath"
  11.                     <rect x="15" y="15" width="40" height="40"></rect> 
  12.                 </clippath> 
  13.             </defs> 
  14.             <circle cx="25" cy="25" r="20" style="fill: #ff0000s; clip-path: url(#clipPath); "></circle> 
  15.         </svg> 
  16.         <svg width="200" height="100" style="border: 1px solid #cccccc;"
  17.             <defs> 
  18.                 <clippath id="clipPath2"
  19.                     <rect x="15" y="15" width="40" height="40"></rect> 
  20.                 </clippath> 
  21.             </defs> 
  22.             <circle cx="25" cy="25" r="20" style="fill: #ff0000; clip-path: url(#clipPath2); "></circle> 
  23.             <rect x="15" y="15" width="40" height="40" style="stroke: #000000; fill:none;"></rect> 
  24.         </svg> 
  25.     </body> 
  26. </html> 

這個實SVG代碼定義了一個形狀類似于矩形(元素中的形狀)的剪輯路徑。示SVG代碼末尾定義的圓通過CSS屬性 clip-path 引用了 id屬性。

運行效果:

左下方是生成的圖像。右邊是同一圖像,但也繪制了剪切路徑。

在剪切路徑內(nèi)只有圓的部分是可見的。其余部分將被剪切。

二、高級剪切路徑

可以使用矩形以外的其他形狀作為剪切路徑。可以使用圓形,橢圓形,多邊形或自定義路徑。任何SVG形狀都可以用作剪切路徑。

這是將元素用作剪切路徑的示SVG代碼,因為這些是可以使用的最高級的剪切路徑類型。剪輯路徑將應(yīng)用于元素。

SVG代碼:

  1. <svg width="200" height="100" style="border: 1px solid #cccccc;"
  2.     <rect x="5" y="5" width="190" height="90" style="stroke: none; fill:#00ff00; "></rect> 
  3. </svg> 
  4. <svg width="200" height="100" style="border: 1px solid #cccccc;"
  5.     <defs> 
  6.         <clippath id="clipPath3"
  7.             <path d="M10,10 q60,60 100,0 q50,50 50,50 l40,0 l-40,40 l-100,-20"></path> 
  8.         </clippath> 
  9.     </defs> 
  10.     <rect x="5" y="5" width="190" height="90" style="stroke: none; fill:#00ff00; clip-path: url(#clipPath3);"></rect> 
  11. </svg> 

運行效果:

這是生成的圖像-在右側(cè)。左側(cè)顯示沒有剪切路徑的圖像。

1. 在組上剪裁路徑

可以在一組SVG形狀上使用剪切路徑,而不是分別在每個形狀上使用。只需將形狀放在元素內(nèi),然后在元素上設(shè)置CSS屬性clip-path即可。這是一個實SVG代碼:

示例SVG代碼

  1. <svg width="200" height="100" style="border: 1px solid #cccccc;"
  2.     <rect x="5" y="5" width="190" height="90" style="stroke: none; fill:#00ff00; "></rect> 
  3.     <circle cx="20" cy="20" r="20" style="stroke: none; fill: #ff0000;"></circle> 
  4. </svg> 
  5. <svg width="200" height="100" style="border: 1px solid #cccccc;"
  6.     <defs> 
  7.         <clippath id="clipPath4"
  8.             <rect x="10" y="20" width="100" height="20"></rect> 
  9.         </clippath> 
  10.     </defs> 
  11.     <g style="clip-path: url(#clipPath4);"
  12.         <rect x="5" y="5" width="190" height="90" style="stroke: none; fill:#00ff00;"></rect> 
  13.         <circle cx="20" cy="20" r="20" style="stroke: none; fill: #ff0000;"></circle> 
  14.     </g> 
  15. </svg> 

運行效果:

下面是沒有剪切路徑的圖像,然后是應(yīng)用剪切路徑的圖像:

2. 文本作為剪切路徑

也可以將文本用作剪切路徑。這是一個實SVG代碼:

SVG代碼:

  1. <svg width="200" height="100" style="border: 1px solid #cccccc;"
  2.             <rect x="5" y="5" width="190" height="90" style="stroke: none; fill:#00ff00; "></rect> 
  3.             <circle cx="20" cy="20" r="20" style="stroke: none; fill: #ff0000;"></circle> 
  4.         </svg> 
  5.         <svg width="200" height="100" style="border: 1px solid #cccccc;"
  6.             <defs> 
  7.                 <clippath id="clipPath5"
  8.                     <text x="10" y="20" style="font-size: 20px; "
  9.                         This is a text 
  10.                     </text> 
  11.                 </clippath> 
  12.             </defs> 
  13.             <g style="clip-path: url(#clipPath5);"
  14.                 <rect x="5" y="5" width="190" height="90" style="stroke: none; fill:#00ff00;"></rect> 
  15.                 <circle cx="20" cy="20" r="20" style="stroke: none; fill: #ff0000;"></circle> 
  16.             </g> 
  17.         </svg> 

這是帶有和不帶有剪切路徑的結(jié)果圖像:

正如看到的,現(xiàn)在只顯示文本內(nèi)部形狀的一部分。

三、總結(jié)

本文基于SVG基礎(chǔ),介紹了如何剪切路徑,可以根據(jù)特定路徑剪切SVG形狀。還介紹了高級的剪切路徑(在組上剪裁路徑、文本作為剪切路徑)通過項目的分析,案例的效果圖的展示,能夠讓讀者更好的理解SVG路徑剪切的用法。

歡迎大家積極嘗試,有時候看到別人實現(xiàn)起來很簡單,但是到自己動手實現(xiàn)的時候,總會有各種各樣的問題,切勿眼高手低,勤動手,才可以理解的更加深刻。

希望能夠幫助你更好的學(xué)習(xí)。

本文轉(zhuǎn)載自微信公眾號「 前端進階學(xué)習(xí)交流」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系 前端進階學(xué)習(xí)交流公眾號。

 

 

責(zé)任編輯:武曉燕 來源: 前端進階學(xué)習(xí)交流
相關(guān)推薦

2020-12-15 08:15:34

SVG元素路徑

2020-12-08 08:09:49

SVG圖標(biāo)Web

2021-02-26 20:01:57

SVG濾鏡元素

2022-05-13 16:21:38

javascrip腳本SVG

2020-12-25 09:42:51

SVGtspanSVG基礎(chǔ)

2021-01-04 10:14:42

SVG標(biāo)簽元素

2021-03-26 09:57:51

SVGHtml基礎(chǔ)SVG圖像

2021-02-05 18:36:15

SVG形狀屬性

2020-12-23 08:12:08

javascriptSVG腳本SVG元素

2020-12-29 09:39:38

元素屬性定位

2021-02-23 06:51:16

SVGstrokeHtml基礎(chǔ)

2021-01-01 09:18:48

SVG圖像元素

2020-12-04 08:40:29

SVG動畫元素

2023-05-12 08:19:12

Netty程序框架

2021-06-30 00:20:12

Hangfire.NET平臺

2020-11-10 10:48:10

JavaScript屬性對象

2021-06-04 09:56:01

JavaScript 前端switch

2021-01-29 18:41:16

JavaScript函數(shù)語法

2021-02-02 18:39:05

JavaScript

2023-09-06 14:57:46

JavaScript編程語言
點贊
收藏

51CTO技術(shù)棧公眾號