動態更改svg圖片的顏色
因為要做uni-app換膚,所以有一些icon使用svg,然後根據主題色進行著色,所以有這個需求下面我說一下解決方法
補充說明一下,這種方式處理起來感覺有點麻煩,最後我還是使用的iconfont去解決的,這裡提供一下改變svg顏色的方法
首先準備一張svg圖
如下
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="44px" height="44px" viewBox="0 0 44 44" enable-background="new 0 0 44 44" xml:space="preserve">
<g display="none">
<path display="inline" d="M28.902,26.002C32.561,23.702,35,19.641,35,15c0-7.18-5.82-13-13-13C14.82,2,9,7.82,9,15
c0,4.641,2.439,8.702,6.098,11.003C7.466,28.259,2,34.12,2,41c0,1.657,1.343,3,3,3h34c1.657,0,3-1.343,3-3
C42,34.12,36.534,28.258,28.902,26.002z M12,15c0-5.514,4.486-10,10-10c5.514,0,10,4.486,10,10s-4.486,10-10,10
C16.486,25,12,20.514,12,15z M5,41c0-7.18,7.611-13,17-13c9.389,0,17,5.82,17,13H5z"/>
<path display="inline" d="M26.232,16.438c-0.807-0.21-1.619,0.274-1.826,1.077C24.121,18.619,23.131,19.39,22,19.39
s-2.121-0.771-2.406-1.876c-0.207-0.802-1.025-1.285-1.828-1.076c-0.802,0.208-1.284,1.026-1.076,1.828
c0.628,2.428,2.812,4.124,5.311,4.124s4.683-1.696,5.312-4.125C27.518,17.464,27.036,16.646,26.232,16.438z"/>
</g>
<g>
<path fill="#999999" d="M22,14c-4.418,0-8,3.582-8,8c0,4.418,3.582,8,8,8c4.418,0,8-3.582,8-8C30,17.582,26.418,14,22,14z
M22,28.77c-3.732,0-6.769-3.037-6.769-6.77s3.037-6.769,6.769-6.769s6.77,3.037,6.77,6.769S25.732,28.77,22,28.77z"/>
<path fill="#999999" d="M26.556,21.385h-3.94v-3.941c0-0.316-0.275-0.572-0.615-0.572c-0.34,0-0.615,0.256-0.615,0.572v4.6
c0,0.147,0.061,0.279,0.159,0.38c0.005,0.005,0.01,0.012,0.015,0.017c0.006,0.006,0.012,0.011,0.019,0.017
c0.101,0.097,0.233,0.158,0.379,0.158h4.6c0.316,0,0.572-0.276,0.572-0.616S26.873,21.385,26.556,21.385z"/>
</g>
</svg>
第二把圖片轉成data:image,可以利用如下網站https://codepen.io/jakob-e/pen/doMoML轉
得到結果如下:
let svgClock = "data:image/svg+xml,%3C?xml version='1.0' encoding='utf-8'?%3E %3C!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --%3E %3C!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3E %3Csvg version='1.1' id='圖層_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' width='40px' height='40px' viewBox='10 12 20 20' xml:space='preserve'%3E %3Cg display='none'%3E %3Cpath display='inline' d='M28.902,26.002C32.561,23.702,35,19.641,35,15c0-7.18-5.82-13-13-13C14.82,2,9,7.82,9,15 c0,4.641,2.439,8.702,6.098,11.003C7.466,28.259,2,34.12,2,41c0,1.657,1.343,3,3,3h34c1.657,0,3-1.343,3-3 C42,34.12,36.534,28.258,28.902,26.002z M12,15c0-5.514,4.486-10,10-10c5.514,0,10,4.486,10,10s-4.486,10-10,10 C16.486,25,12,20.514,12,15z M5,41c0-7.18,7.611-13,17-13c9.389,0,17,5.82,17,13H5z'/%3E %3Cpath display='inline' d='M26.232,16.438c-0.807-0.21-1.619,0.274-1.826,1.077C24.121,18.619,23.131,19.39,22,19.39 s-2.121-0.771-2.406-1.876c-0.207-0.802-1.025-1.285-1.828-1.076c-0.802,0.208-1.284,1.026-1.076,1.828 c0.628,2.428,2.812,4.124,5.311,4.124s4.683-1.696,5.312-4.125C27.518,17.464,27.036,16.646,26.232,16.438z'/%3E %3C/g%3E %3Cg%3E %3Cpath fill='%23999999' d='M22,14c-4.418,0-8,3.582-8,8c0,4.418,3.582,8,8,8c4.418,0,8-3.582,8-8C30,17.582,26.418,14,22,14z M22,28.77c-3.732,0-6.769-3.037-6.769-6.77s3.037-6.769,6.769-6.769s6.77,3.037,6.77,6.769S25.732,28.77,22,28.77z'/%3E %3Cpath fill='%23999999' d='M26.556,21.385h-3.94v-3.941c0-0.316-0.275-0.572-0.615-0.572c-0.34,0-0.615,0.256-0.615,0.572v4.6 c0,0.147,0.061,0.279,0.159,0.38c0.005,0.005,0.01,0.012,0.015,0.017c0.006,0.006,0.012,0.011,0.019,0.017 c0.101,0.097,0.233,0.158,0.379,0.158h4.6c0.316,0,0.572-0.276,0.572-0.616S26.873,21.385,26.556,21.385z'/%3E %3C/g%3E %3C/svg%3E"
第三使用一個函式替換顏色
如下
const changeSvgColor = function(url, color){
let svgUrl = url.replace(/%23[a-zA-Z0-9]{6}/g, color.replace("#", "%23"));//這時候需要注意有的顏色是三個長度
return svgUrl;
}
第四使用
data() {
return {
svgData: '"' + svgClock + '"',
}
}
<view :style="{ backgroundImage: 'url('+svgData+')' }" class="icon" ></view>
js
this.svgData = changeSvgColor(this.svgData, this.appColorStyle.themeColor);
效果如下:
相關文章
- 使用CSS混合模式和SVG來動態更改產品圖片的顏色CSS模式SVG
- 用SVG的圖片格式如何劃入更改圖片的顏色?SVG
- svg圖片 填充顏色SVG
- css 更改svg顏色CSSSVG
- 修改SVG圖片的大小和顏色SVG
- 如何在 CSS 背景圖片中對 SVG 圖片進行著色 ,修改svg圖片顏色CSSSVG
- 改變SVG圖的顏色SVG
- web前端 改變SVG圖的顏色Web前端SVG
- svg 圖示設定背景顏色SVG
- [SVG]修改固定顏色為填充顏色SVG
- 更改placeholder顏色
- XMind怎麼更改線條顏色?XMind更改線條顏色的方法
- VueJs中動態更改svg的相關屬性VueJSSVG
- 一個提取圖片顏色的React元件React元件
- 搭建自己的直播平臺,更改狀態列的字型顏色
- 利用bitmap將圖片部分顏色透明
- 直播原始碼網站,任意更改底部圖示顏色原始碼網站
- 如何更改 Mac 上的游標顏色Mac
- Android通過程式碼修改圖片顏色Android
- Chrome 獲取網頁顏色(文字、圖片)Chrome網頁
- 利用CSS改變圖片顏色的100種方法!CSS
- 利用CSS改變圖片顏色的多種方法!CSS
- 短視訊直播系統,簡單實現個人主頁背景圖片和文字顏色的更改
- win10 如何更改檔案筐顏色_win10更改檔案筐顏色方法Win10
- Mac更改資料夾圖示顏色軟體:Color FolderMac
- android更改EditText下劃線顏色Android
- win10 更改控制檯預設顏色方法 win10怎麼更改控制檯顏色Win10
- 使用canvas給banner圖片加個顏色條Canvas
- Android的標題欄,狀態列圖示文字顏色及背景動態變化Android
- 如何更改 Linux 終端顏色主題Linux
- 技巧分享:如何利用CSS屬性修改圖片顏色?CSS
- canvas實現動態替換人物的背景顏色Canvas
- 兩行 CSS 程式碼實現圖片任意顏色賦色技術CSS
- SVG動畫應用-酷炫的圖片展示效果SVG動畫
- 高階切圖技巧!基於單張圖片的任意顏色轉換
- Flutter改變狀態列字型、狀態列背景顏色、Appbar背景顏色的方式FlutterAPP
- Win10如何更改游標指標顏色樣式 win10更改滑鼠指標顏色樣式的步驟Win10指標
- Mac電腦游標顏色怎麼更改Mac