preamble
Recently when doing the project is always because the component library icon can not meet the demand and annoyed, and every time you need a new icon and have to go to find the font icon, generate new css code and agonizing.
So thought of using svg instead, the volume is also small, the main thing is a little more convenient, do not have to every time also re-generate the code. But one of the most important problems also appeared, the small program does not support svg code .... But there is also a way to save the country, image can display svg, fortunately did not drive to the end.
But then there is a new problem, although it can display svg, but it can't change the color dynamically! I can't change the color dynamically! However, the src of image supports base64. Oops, this idea is coming, since you can base64, I still can't modify base64? I can't change base64? Just do it, let's do it!
1. create a wechat applet project, prepare SVG material
Once you've created your project, go to iconfont and look for the svg of the two icons! So how to manage the svg? First we need to base64 encode the svg code.
Example:
<svg t="1656728349217" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http:///2000/svg" p- width="200" height="200"><path d="M874.666667 467.2c-10.666667-10.666667-29.866667-12.8-42.666667 0l-343.466667 341.333333c-74.666667 74.666667-198.4 74.666667-275.2 0-36.266667-36.266667-57.6-85.333333-57.6-136.533333s19.2-100.266667 57.6-136.533333L556.8 192c46.933333-46.933333 121.6-46.933333 168.533333 0 23.466667 23.466667 34.133333 53.333333 34.133334 83.2 0 32-12.8 61.866667-34.133334 83.2L384 704c-17.066667 17.066667-44.8 17.066667-64 0-8.533333-8.533333-12.8-19.2-12.8-32s4.266667-23.466667 12.8-32l317.866667-315.733333c10.666667-10.666667 12.8-29.866667 0-42.666667-10.666667-12.8-29.866667-12.8-42.666667 0L277.333333 597.333333c-19.2 19.2-29.866667 46.933333-29.866666 74.666667S258.133333 725.333333 277.333333 746.666667c40.533333 40.533333 106.666667 40.533333 147.2 0L768 403.2c34.133333-34.133333 53.333333-78.933333 53.333333-125.866667s-19.2-93.866667-53.333333-125.866666a178.986667 178.986667 0 0 0-253.866667 0l-341.333333 341.333333c-46.933333 46.933333-74.666667 110.933333-74.666667 179.2s25.6 132.266667 74.666667 179.2c49.066667 49.066667 115.2 74.666667 179.2 74.666667s130.133333-25.6 179.2-74.666667l343.466667-341.333333c10.666667-12.8 10.666667-32 0-42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OTcuMzMzMzMzYy0xOS4yIDE5LjItMjkuODY2NjY3IDQ2LjkzMzMzMy0yOS44NjY2NjYgNzQuNjY2NjY3UzI1OC4xMzMzMzMgNzI1LjMzMzMzMyAyNzcuMzMzMzMzIDc0Ni42NjY2NjdjNDAuNTMzMzMzIDQwLjUzMzMzMyAxMDYuNjY2NjY3IDQwLjUzMzMzMyAxNDcuMiAwTDc2OCA0MDMuMmMzNC4xMzMzMzMtMzQuMTMzMzMzIDUzLjMzMzMzMy03OC45MzMzMzMgNTMuMzMzMzMzLTEyNS44NjY2NjdzLTE5LjItOTMuODY2NjY3LTUzLjMzMzMzMy0xMjUuODY2NjY2YTE3OC45ODY2NjcgMTc4Ljk4NjY2NyAwIDAgMC0yNTMuODY2NjY3IDBsLTM0MS4zMzMzMzMgMzQxLjMzMzMzM2MtNDYuOTMzMzMzIDQ2LjkzMzMzMy03NC42NjY2NjcgMTEwLjkzMzMzMy03NC42NjY2NjcgMTc5LjJzMjUuNiAxMzIuMjY2NjY3IDc0LjY2NjY2NyAxNzkuMmM0OS4wNjY2NjcgNDkuMDY2NjY3IDExNS4yIDc0LjY2NjY2NyAxNzkuMiA3NC42NjY2NjdzMTMwLjEzMzMzMy0yNS42IDE3OS4yLTc0LjY2NjY2N2wzNDMuNDY2NjY3LTM0MS4zMzMzMzNjMTAuNjY2NjY3LTEyLjggMTAuNjY2NjY3LTMyIDAtNDIuNjY2NjY3eiIgcC1pZD0iMjU5MCI+PC9wYXRoPjwvc3ZnPg==
Here is an explanation of the result after the conversion, which is divided into three parts:
-
data:image/svg+xml;base64
The first string is used as a fixed value so that image can recognize what you are giving it, an image, svg+xml format, encoded with base64, this explanation is not professional haha, just be able to read it. - An English comma separates the front and back.
- The string after the comma is the string obtained after base64 encoding of the svg code, which is the string we can manipulate.
Next, create a new file in the applet/asstes/
Used to manage all of our svg code, because we want to manipulate the base64 of the svg, it is best to store it directly in the project, will be converted to a base64 string svg exported through export
export default { attachment: 'data:image/svg+xml;base64,PH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2LjkzMzMzMy0yOS44NjY2NjYgNzQuNjY2NjY3UzI1OC4xMzMzMzMgNzI1LjMzMzMzMyAyNzcuMzMzMzMzIDc0Ni42NjY2NjdjNDAuNTMzMzMzIDQwLjUzMzMzMyAxMDYuNjY2NjY3IDQwLjUzMzMzMyAxNDcuMiAwTDc2OCA0MDMuMmMzNC4xMzMzMzMtMzQuMTMzMzMzIDUzLjMzMzMzMy03OC45MzMzMzMgNTMuMzMzMzMzLTEyNS44NjY2NjdzLTE5LjItOTMuODY2NjY3LTUzLjMzMzMzMy0xMjUuODY2NjY2YTE3OC45ODY2NjcgMTc4Ljk4NjY2NyAwIDAgMC0yNTMuODY2NjY3IDBsLTM0MS4zMzMzMzMgMzQxLjMzMzMzM2MtNDYuOTMzMzMzIDQ2LjkzMzMzMy03NC42NjY2NjcgMTEwLjkzMzMzMy03NC42NjY2NjcgMTc5LjJzMjUuNiAxMzIuMjY2NjY3IDc0LjY2NjY2NyAxNzkuMmM0OS4wNjY2NjcgNDkuMDY2NjY3IDExNS4yIDc0LjY2NjY2NyAxNzkuMiA3NC42NjY2NjdzMTMwLjEzMzMzMy0yNS42IDE3OS4yLTc0LjY2NjY2N2wzNDMuNDY2NjY3LTM0MS4zMzMzMzNjMTAuNjY2NjY3LTEyLjggMTAuNjY2NjY3LTMyIDAtNDIuNjY2NjY3eiIgcC1pZD0iMjU5MCI+PC9wYXRoPjwvc3ZnPg==' }
2. Encapsulate the factory function that modifies the color of the svg.
Before we start, we need to understand how svg changes colors.
There is a property in svgfill
Here is where you fill in the fill color of the svg, which supports hexadecimal, e.g.#ff0000
It's just red. You can fill it in too.red
, pretty much the same as css, understand this and you can proceed to the next step.
Factory function code: The idea is to decode the SVG base64 string to get the svg code and replace the svg code.fill
attribute to change the color, and then Base64-encoded back. Base64 code can be found on the Internet at random, the end of the article will be posted on the code snippet, containing Base64 code
import { Base64 } from "./Base64"; export const getColorSVG = (svgBase64, color) => { try { svgBase64 = ((',') + 1, ); // Take out the third part const svg = (svgBase64); // Decode the svg code if (/<svg /.test(svg)) { // Simply determine if it's an svg or not. let newSvg; if (/fill=".*?"/.test(svg)) { newSvg = (/fill=".*?"/, `fill="${color}"`); // SVGs have default colors } else { newSvg = (/<svg /, `<svg fill="${color}"`); // No default color } return 'data:image/svg+xml;base64,' + (newSvg); // Replace it and put it back together } } catch { } return ''; };
3. Wrap a custom component to facilitate the use of svg icons
Create a new component and save it in the/components/m-icon/index
- JS
import SvgManager from "../../asstes/SvgManager" import { getColorSVG } from "../../utils/tools" Component({ options: { styleIsolation: 'apply-shared', virtualHost: true }, properties: { /** Icon name */ name: { type: String, value: '' }, /** Icon color */ color: { type: String, value: '#000000' }, size: { type: String, value: '28rpx' } }, observers: { // Listen for name and color changes 'name,color': function (name, color) { (name, color); } }, data: { svgData: '' }, methods: { changeIcon(name, color) { let svgBase64 = SvgManager[name]; // Retrieve the corresponding svg from the svg manager svgBase64 = getColorSVG(svgBase64, color); // Replace its color ({ svgData: svgBase64 // Rendering }); } } })
- WXML
<view class="m-icon" style="width: {{ size }};"> <image src="{{ svgData }}" mode="widthFix" ></image> </view>
- WXSS
.m-icon image { width: 100%; }
4. Registering components, using
- Register, either on the page you want to use or globally, which is what I've done here, on the
Add the following configuration
{ ...setting, "usingComponents": { "m-icon": "/components/m-icon/index" }, }
Then you can happily use our own icon component on any page!
<m-icon name="attachment" color="red" size="30rpx" /> <m-icon name="attachment" color="green" size="60rpx" /> <m-icon name="attachment" color="blue" size="90rpx" />
concluding remarks
Dynamically changing svg is just a thought, image supports base64, svg hasfill
attribute can change the color, we can manipulate the string to replace thefill
I'm not sure if I'm going to be able to do this, but I'm sure I'm going to be able to do it! There are better ideas welcome everyone to put forward . This article project code:/daofeng-cod…
summarize
To this article on WeChat applet how to achieve dynamic change SVG color and size of the article is introduced to this, more related to the applet dynamically change the size of the SVG color content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!