当前位置:   article > 正文

微信小程序——关于同一个svg的颜色改变_小程序svg 修改颜色

小程序svg 修改颜色

目录

一、问题的产生

二、问题的解决

 三、总结


        相信小伙伴们在微信小程序开发之中使用svg的时候遇到过一个问题,同一个svg但是产品要求不同的状态有着不同的颜色,那么如果我们引入了太多的同一个类型的svg,对我们的内存又是一个极大的消耗,那么我们有没有什么办法将一个svg改变一下颜色呢?这样不就可以节省很多内存了,那么各位继续向下看吧。

一、问题的产生

此问题出于产品提出的一个需求

        在此处的建筑房子在浅色系的时候,颜色需要根据色卡的颜色来进行变换,但是由于浅色系的色卡过多,将所有颜色的建筑储存起来在调用会产生多余的很多代码,因为此处使用的是svg,确实可以显示颜色,但是并不能动态改变颜色呀!偶天哪。。。又但是,image的src支持base64,哎呀,这思路不就来了吗,既然你能base64,我还不能修改base64了?说干就干,那就来吧!

二、问题的解决

开始之前,我们要先明白,svg是如何改变颜色的。

svg中有一个属性fill,这里就是用来填写svg的填充颜色的,支持十六进制,例如#ff0000就是红色,也可以填写red,跟css差不多,明白这一点就可以进行接下来的操作了。

工厂函数代码: 实现思路就是将SVG的base64字符串解码之后得到svg代码,替换svg的fill属性来改变颜色,然后再Base64编码回去。

这里对转换之后的base64结果做一个说明,共分成三部分:

data:image/svg+xml;base64首部的这一串作为固定值,让image能够识别你给它的是什么东西,是一个图片、svg+xml格式的、经过了base64编码的,这个解释不是专业的哈,能看懂就行。

一个英文逗号,将前后隔离开。

逗号后面的就是svg代码base64编码之后得到的字符串,这就是我们可操作的字符串啦

由此这里我们需要四个东西进行辅助工作,两个工具类、一个组件以及一个js的静态文件(用来储存你需要改变颜色的base64的代码)

 在这里classSvg.js的作用是储存你需要改变颜色的svg的base64代码文件

  1. export default {
  2. attachment: 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTQiIGhlaWdodD0iMTUiIHZpZXdCb3g9IjAgMCAxNCAxNSIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDA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NS40ODEyOEM3Ljg5OTM0IDUuNDgxMzkgNy45OTk5MSA1LjUyMTQzIDguMDc1OTUgNS41OTMyMkM4LjE1MTk4IDUuNjY1IDguMTk3NzQgNS43NjMxIDguMjAzODcgNS44Njc0OUM4LjIxIDUuOTcxODggOC4xNzYwMyA2LjA3NDY2IDguMTA4OTIgNi4xNTQ4NUM4LjA0MTgxIDYuMjM1MDQgNy45NDY2MSA2LjI4NjU3IDcuODQyNzcgNi4yOTg5Mkw3Ljc5NDc3IDYuMzAxNzlINi4xNTM3NUM2LjA0OTE4IDYuMzAxNjcgNS45NDg2IDYuMjYxNjMgNS44NzI1NyA2LjE4OTg1QzUuNzk2NTMgNi4xMTgwNyA1Ljc1MDc4IDYuMDE5OTYgNS43NDQ2NSA1LjkxNTU4QzUuNzM4NTIgNS44MTExOSA1Ljc3MjQ4IDUuNzA4NCA1LjgzOTYgNS42MjgyMkM1LjkwNjcxIDUuNTQ4MDMgNi4wMDE5MSA1LjQ5NjUgNi4xMDU3NSA1LjQ4NDE1TDYuMTUzNzUgNS40ODEyOEg3Ljc5NDc3WiIgZmlsbD0id2hpdGUiLz4KPC9zdmc+Cg=='
  3. }

此处为Base64.js的代码文件(直接复制即可)

  1. /**
  2. * Created by SLICE_30_K on 2017/5/22.
  3. *
  4. * 支持一般Base64的编码和解码
  5. * 支持符合RFC_4648标准中"URL and Filename Safe Alphabet"的URL安全Base64编解码
  6. * 支持中文字符的编解码(Unicode编码)
  7. */
  8. var BASE64_MAPPING = [
  9. 'A',
  10. 'B',
  11. 'C',
  12. 'D',
  13. 'E',
  14. 'F',
  15. 'G',
  16. 'H',
  17. 'I',
  18. 'J',
  19. 'K',
  20. 'L',
  21. 'M',
  22. 'N',
  23. 'O',
  24. 'P',
  25. 'Q',
  26. 'R',
  27. 'S',
  28. 'T',
  29. 'U',
  30. 'V',
  31. 'W',
  32. 'X',
  33. 'Y',
  34. 'Z',
  35. 'a',
  36. 'b',
  37. 'c',
  38. 'd',
  39. 'e',
  40. 'f',
  41. 'g',
  42. 'h',
  43. 'i',
  44. 'j',
  45. 'k',
  46. 'l',
  47. 'm',
  48. 'n',
  49. 'o',
  50. 'p',
  51. 'q',
  52. 'r',
  53. 's',
  54. 't',
  55. 'u',
  56. 'v',
  57. 'w',
  58. 'x',
  59. 'y',
  60. 'z',
  61. '0',
  62. '1',
  63. '2',
  64. '3',
  65. '4',
  66. '5',
  67. '6',
  68. '7',
  69. '8',
  70. '9',
  71. '+',
  72. '/'
  73. ];
  74. var URLSAFE_BASE64_MAPPING = [
  75. 'A',
  76. 'B',
  77. 'C',
  78. 'D',
  79. 'E',
  80. 'F',
  81. 'G',
  82. 'H',
  83. 'I',
  84. 'J',
  85. 'K',
  86. 'L',
  87. 'M',
  88. 'N',
  89. 'O',
  90. 'P',
  91. 'Q',
  92. 'R',
  93. 'S',
  94. 'T',
  95. 'U',
  96. 'V',
  97. 'W',
  98. 'X',
  99. 'Y',
  100. 'Z',
  101. 'a',
  102. 'b',
  103. 'c',
  104. 'd',
  105. 'e',
  106. 'f',
  107. 'g',
  108. 'h',
  109. 'i',
  110. 'j',
  111. 'k',
  112. 'l',
  113. 'm',
  114. 'n',
  115. 'o',
  116. 'p',
  117. 'q',
  118. 'r',
  119. 's',
  120. 't',
  121. 'u',
  122. 'v',
  123. 'w',
  124. 'x',
  125. 'y',
  126. 'z',
  127. '0',
  128. '1',
  129. '2',
  130. '3',
  131. '4',
  132. '5',
  133. '6',
  134. '7',
  135. '8',
  136. '9',
  137. '-',
  138. '_'
  139. ];
  140. var _toBinary = function (ascii) {
  141. var binary = [];
  142. while (ascii > 0) {
  143. var b = ascii % 2;
  144. ascii = Math.floor(ascii / 2);
  145. binary.push(b);
  146. }
  147. binary.reverse();
  148. return binary;
  149. };
  150. var _toDecimal = function (binary) {
  151. var dec = 0;
  152. var p = 0;
  153. for (var i = binary.length - 1; i >= 0; --i) {
  154. var b = binary[i];
  155. if (b == 1) {
  156. dec += Math.pow(2, p);
  157. }
  158. ++p;
  159. }
  160. return dec;
  161. };
  162. var _toUTF8Binary = function (c, binaryArray) {
  163. var mustLen = 8 - (c + 1) + (c - 1) * 6;
  164. var fatLen = binaryArray.length;
  165. var diff = mustLen - fatLen;
  166. while (--diff >= 0) {
  167. binaryArray.unshift(0);
  168. }
  169. var binary = [];
  170. var _c = c;
  171. while (--_c >= 0) {
  172. binary.push(1);
  173. }
  174. binary.push(0);
  175. var i = 0,
  176. len = 8 - (c + 1);
  177. for (; i < len; ++i) {
  178. binary.push(binaryArray[i]);
  179. }
  180. for (var j = 0; j < c - 1; ++j) {
  181. binary.push(1);
  182. binary.push(0);
  183. var sum = 6;
  184. while (--sum >= 0) {
  185. binary.push(binaryArray[i++]);
  186. }
  187. }
  188. return binary;
  189. };
  190. var _toBinaryArray = function (str) {
  191. var binaryArray = [];
  192. for (var i = 0, len = str.length; i < len; ++i) {
  193. var unicode = str.charCodeAt(i);
  194. var _tmpBinary = _toBinary(unicode);
  195. if (unicode < 0x80) {
  196. var _tmpdiff = 8 - _tmpBinary.length;
  197. while (--_tmpdiff >= 0) {
  198. _tmpBinary.unshift(0);
  199. }
  200. binaryArray = binaryArray.concat(_tmpBinary);
  201. } else if (unicode >= 0x80 && unicode <= 0x7ff) {
  202. binaryArray = binaryArray.concat(_toUTF8Binary(2, _tmpBinary));
  203. } else if (unicode >= 0x800 && unicode <= 0xffff) {
  204. //UTF-8 3byte
  205. binaryArray = binaryArray.concat(_toUTF8Binary(3, _tmpBinary));
  206. } else if (unicode >= 0x10000 && unicode <= 0x1fffff) {
  207. //UTF-8 4byte
  208. binaryArray = binaryArray.concat(_toUTF8Binary(4, _tmpBinary));
  209. } else if (unicode >= 0x200000 && unicode <= 0x3ffffff) {
  210. //UTF-8 5byte
  211. binaryArray = binaryArray.concat(_toUTF8Binary(5, _tmpBinary));
  212. } else if (unicode >= 4000000 && unicode <= 0x7fffffff) {
  213. //UTF-8 6byte
  214. binaryArray = binaryArray.concat(_toUTF8Binary(6, _tmpBinary));
  215. }
  216. }
  217. return binaryArray;
  218. };
  219. var _toUnicodeStr = function (binaryArray) {
  220. var unicode;
  221. var unicodeBinary = [];
  222. var str = '';
  223. for (var i = 0, len = binaryArray.length; i < len;) {
  224. if (binaryArray[i] == 0) {
  225. unicode = _toDecimal(binaryArray.slice(i, i + 8));
  226. str += String.fromCharCode(unicode);
  227. i += 8;
  228. } else {
  229. var sum = 0;
  230. while (i < len) {
  231. if (binaryArray[i] == 1) {
  232. ++sum;
  233. } else {
  234. break;
  235. }
  236. ++i;
  237. }
  238. unicodeBinary = unicodeBinary.concat(binaryArray.slice(i + 1, i + 8 - sum));
  239. i += 8 - sum;
  240. while (sum > 1) {
  241. unicodeBinary = unicodeBinary.concat(binaryArray.slice(i + 2, i + 8));
  242. i += 8;
  243. --sum;
  244. }
  245. unicode = _toDecimal(unicodeBinary);
  246. str += String.fromCharCode(unicode);
  247. unicodeBinary = [];
  248. }
  249. }
  250. return str;
  251. };
  252. var _encode = function (str, url_safe) {
  253. var base64_Index = [];
  254. var binaryArray = _toBinaryArray(str);
  255. var dictionary = url_safe ? URLSAFE_BASE64_MAPPING : BASE64_MAPPING;
  256. var extra_Zero_Count = 0;
  257. for (var i = 0, len = binaryArray.length; i < len; i += 6) {
  258. var diff = i + 6 - len;
  259. if (diff == 2) {
  260. extra_Zero_Count = 2;
  261. } else if (diff == 4) {
  262. extra_Zero_Count = 4;
  263. }
  264. var _tmpExtra_Zero_Count = extra_Zero_Count;
  265. while (--_tmpExtra_Zero_Count >= 0) {
  266. binaryArray.push(0);
  267. }
  268. base64_Index.push(_toDecimal(binaryArray.slice(i, i + 6)));
  269. }
  270. var base64 = '';
  271. for (var i = 0, len = base64_Index.length; i < len; ++i) {
  272. base64 += dictionary[base64_Index[i]];
  273. }
  274. for (var i = 0, len = extra_Zero_Count / 2; i < len; ++i) {
  275. base64 += '=';
  276. }
  277. return base64;
  278. };
  279. var _decode = function (_base64Str, url_safe) {
  280. var _len = _base64Str.length;
  281. var extra_Zero_Count = 0;
  282. var dictionary = url_safe ? URLSAFE_BASE64_MAPPING : BASE64_MAPPING;
  283. if (_base64Str.charAt(_len - 1) == '=') {
  284. if (_base64Str.charAt(_len - 2) == '=') {
  285. //两个等号说明补了4个0
  286. extra_Zero_Count = 4;
  287. _base64Str = _base64Str.substring(0, _len - 2);
  288. } else {
  289. //一个等号说明补了2个0
  290. extra_Zero_Count = 2;
  291. _base64Str = _base64Str.substring(0, _len - 1);
  292. }
  293. }
  294. var binaryArray = [];
  295. for (var i = 0, len = _base64Str.length; i < len; ++i) {
  296. var c = _base64Str.charAt(i);
  297. for (var j = 0, size = dictionary.length; j < size; ++j) {
  298. if (c == dictionary[j]) {
  299. var _tmp = _toBinary(j);
  300. /*不足6位的补0*/
  301. var _tmpLen = _tmp.length;
  302. if (6 - _tmpLen > 0) {
  303. for (var k = 6 - _tmpLen; k > 0; --k) {
  304. _tmp.unshift(0);
  305. }
  306. }
  307. binaryArray = binaryArray.concat(_tmp);
  308. break;
  309. }
  310. }
  311. }
  312. if (extra_Zero_Count > 0) {
  313. binaryArray = binaryArray.slice(0, binaryArray.length - extra_Zero_Count);
  314. }
  315. var str = _toUnicodeStr(binaryArray);
  316. return str;
  317. };
  318. export const Base64 = {
  319. encode: function (str) {
  320. return _encode(str, false);
  321. },
  322. decode: function (base64Str) {
  323. return _decode(base64Str, false);
  324. },
  325. urlsafe_encode: function (str) {
  326. return _encode(str, true);
  327. },
  328. urlsafe_decode: function (base64Str) {
  329. return _decode(base64Str, true);
  330. }
  331. };

tools.js: 实现思路就是将SVG的base64字符串解码之后得到svg代码,替换svg的fill属性来改变颜色,然后再Base64编码回去。(直接复制即可)

  1. import { Base64 } from "./Base64";
  2. export const getColorSVG = (svgBase64, color) => {
  3. try {
  4. svgBase64 = svgBase64.substring(svgBase64.indexOf(',') + 1, svgBase64.length);
  5. const svg = Base64.decode(svgBase64);// 解码得到svg代码
  6. if (/<svg /.test(svg)) {// 先简单判断是一下否是一个svg
  7. let newSvg;
  8. if (/fill=".*?"/.test(svg)) {
  9. newSvg = svg.replace(/fill=".*?"/g, `fill="${color}" `); // SVG有默认色,注意结尾跟一个空格
  10. } else {
  11. newSvg = svg.replace(/<svg /, `<svg fill="${color}" `); // 无默认色,注意结尾跟一个空格
  12. }
  13. return 'data:image/svg+xml;base64,' + Base64.encode(newSvg); // 替换完之后再组合回去
  14. }
  15. } catch { }
  16. return '';
  17. };

组件svg.js

  1. import SvgManager from "../../pages/widgets/classSchedule/classSchedulePage/classSvg"
  2. import { getColorSVG } from "../../utils/tools"
  3. Component({
  4. options: {
  5. styleIsolation: 'apply-shared',
  6. virtualHost: true
  7. },
  8. properties: {
  9. /** 图标名称 */
  10. name: {
  11. type: String,
  12. value: ''
  13. },
  14. /** 图标颜色 */
  15. color: {
  16. type: String,
  17. value: '#000000'
  18. },
  19. size: {
  20. type: String,
  21. value: '28rpx'
  22. }
  23. },
  24. observers: {
  25. // 监听名称和颜色变化
  26. 'name,color': function (name, color) {
  27. this.changeIcon(name, color);
  28. }
  29. },
  30. data: {
  31. svgData: ''
  32. },
  33. methods: {
  34. changeIcon(name, color) {
  35. let svgBase64 = SvgManager[name]; // 从svg管理器中取出对应svg
  36. svgBase64 = getColorSVG(svgBase64, color); // 替换它的颜色
  37. this.setData({
  38. svgData: svgBase64 // 渲染
  39. });
  40. }
  41. }
  42. })

在此处呢,就是我们所需要的所有的代码。

之后呢,在实际的代码引用的过程中我们只需要(此处的m-icon是我自己引用组件设置的名字,各位可以自己设置名字,name为你classSvg.js里面保存的base64的名字,color为你想改变的颜色,size为你需要的图片的大小)

<m-icon name="attachment" color="#689658" size="30rpx" />

就可以在我们的页面之上对我们需要的svg进行一个变色处理了

以下就是我们颜色变换的一个效果图啦!!!

 三、总结

        当时小编遇到这个问题的时候头疼了好久的,最后在团队的一位学长的帮助下搞出来了,很感谢呀,这个位置呢,其实也就是去更改了base64里面控制颜色的一部分代码,这个就是最基础的逻辑思路了,okok,那今天的分享就到这里了,期待和各位一起变强呀! 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号