mpvue微信小程序中使用svg图标,并通过代码动态改变图标颜色

偏执的太偏执、 2021-12-14 09:07 752阅读 0赞

微信小程序,mpvue中使用svg图标,并通过代码改变图标颜色

本文主要是mpvue开发小程序的代码,不过微信小程序原生开发应该也是一样的,思路都是通用的,按照这个思路微信小程序原生开发一样可以实现同样的功能。

首先先说下svg格式(给出官方说明):

  1. SVG 指可伸缩矢量图形 (Scalable Vector Graphics)
  2. SVG 用来定义用于网络的基于矢量的图形
  3. SVG 使用 XML 格式定义图形
  4. SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失
  5. SVG 是万维网联盟的标准
  6. SVG 与诸如 DOM 和 XSL 之类的 W3C 标准是一个整体

在mpvue中我们可以直接这样使用svg图标:

  1. <img style="width:45rpx;height:45rpx;" src="/static/icon/index/zaixianzhanting.svg"/>

svg图标的路径:
在这里插入图片描述
我们知道svg是xml格式定义的一种图标,是通过xml代码中的fill属性来修改图标的颜色的,UI给的svg默认都是黑色,当我们需要其他颜色的图标时,我们可以直接修改源码中的fill属性值达到修改颜色的目的。

(注意:svg中可能有多个fill,替换时根据需要替换即可)

但是如果我们需要在用户执行的时候动态修改图标的颜色呢?难道每种颜色都新建一个svg图标再引用?答案肯定是否定的。

直接给思路:
既然svg是通过xml定义图像的,那么为什么我们不能先获取svg图标的源码,再将其中fill的属性值替换成我们需要的颜色呢?
然后再将替换后的源码成base64格式,是不是就可以实现动态的改变图标的颜色了?

小程序api中为我们提供了读取文件的方法:wx.getFileSystemManager().readFile()
通过这个方法我们可以获取到svg文件的源码。

mpvue 框架中我们直接可以封装一个vue组件,方便各个页面使用,组件已经给大家写好了。直接复制到项目就可以使用。

组件代码:
将字符串源码转化为base64需要引入 JS-base64 这个js
npm直接安装:
$ npm install —save js-base64

icons组件:

  1. <template>
  2. <img :style="iconstyle?iconstyle:('width:' + size + 'rpx;height:' + size + 'rpx;')" :src="imgurl" :model="model"/>
  3. </template>
  4. <script>
  5. import { Base64 } from 'js-base64'
  6. export default {
  7. props: {
  8. size: {
  9. type: [Number, String],
  10. default: 45
  11. },
  12. src: [String],
  13. iconstyle: [String],
  14. color: [String],
  15. stroke: [String],
  16. model: [String]
  17. },
  18. data () {
  19. return {
  20. imgurl: ''
  21. }
  22. },
  23. watch: {
  24. color: function (obj) {
  25. this.initImage()
  26. },
  27. stroke: function (obj) {
  28. this.initImage()
  29. }
  30. },
  31. methods: {
  32. initImage () {
  33. if (this.color || this.stroke) {
  34. // 先获取svg源码字符串,替换 file="#ffff" stroke='#FFFFFF' 颜色这个属性,再将这个字符串转化为base64,达到修改颜色的目的
  35. wx.getFileSystemManager().readFile({
  36. filePath: this.src,
  37. encoding: 'binary',
  38. success: res => {
  39. let basestr = res.data
  40. if (this.color) {
  41. const strArr = basestr.split(/fill="#?[a-zA-Z0-9]{0,6}"/g)
  42. const oldcolorArr = basestr.match(/fill="#?[a-zA-Z0-9]{0,6}"/g)
  43. const newcolorArr = this.color.split(',')
  44. for (let i = 0; i < newcolorArr.length; i++) {
  45. const color = newcolorArr[i]
  46. if (color) {
  47. oldcolorArr[i] = `fill="${ color}"`
  48. }
  49. }
  50. let str = ''
  51. for (let i = 0; i < strArr.length; i++) {
  52. str += (strArr[i] + (oldcolorArr[i] ? oldcolorArr[i] : ''))
  53. }
  54. basestr = str
  55. }
  56. if (this.stroke) {
  57. const strArr = basestr.split(/stroke="#?[a-zA-Z0-9]{0,6}"/g)
  58. const oldcolorArr = basestr.match(/stroke="#?[a-zA-Z0-9]{0,6}"/g)
  59. const newcolorArr = this.stroke.split(',')
  60. for (let i = 0; i < newcolorArr.length; i++) {
  61. const color = newcolorArr[i]
  62. if (color) {
  63. oldcolorArr[i] = `stroke="${ color}"`
  64. }
  65. }
  66. let str = ''
  67. for (let i = 0; i < strArr.length; i++) {
  68. str += (strArr[i] + (oldcolorArr[i] ? oldcolorArr[i] : ''))
  69. }
  70. basestr = str
  71. }
  72. const base64 = Base64.encode(basestr)
  73. this.imgurl = 'data:image/svg+xml;base64,' + base64
  74. }
  75. })
  76. } else {
  77. this.imgurl = this.src
  78. }
  79. }
  80. },
  81. created () {
  82. this.initImage()
  83. }
  84. }
  85. </script>
  86. <style scoped>
  87. img{
  88. display: block;
  89. }
  90. </style>

组件使用:vue组件的使用不用我多说了吧~ 先在页面注册然后就能使用了。

考虑到svg还有stroke属性也可以改变颜色。并且一个文件有多个fill,stroke属性(color对应svg的fill属性,stroke对应svg的stroke属性)
我们来通过逗号”,“来分割颜色属性,逗号分隔后的数组来确定我们要替换哪个fill属性或者stroke属性。

组件使用范例:

  1. <!-- 第一个fill属性值替换成#cccccc-->
  2. <icons src="/static/icon/index/zaixianzhanting.svg" color="#cccccc" size="40"/>
  3. <!-- 第三个fill属性替值换成#cccccc,前两个不替换-->
  4. <icons src="/static/icon/index/zaixianzhanting.svg" color=",,#cccccc" size="40"/>
  5. <!-- 第二个fill属性值替换成#cccccc,第三个fill属性换成#fff,第三个stroke属性值替换成#cccccc-->
  6. <icons src="/static/icon/index/zaixianzhanting.svg" color=",#cccccc,#fff" stroke=",,#cccccc" size="40"/>

好了,大功告成!

原创不易,转载请注明出处!!

发表评论

表情:
评论列表 (有 0 条评论,752人围观)

还没有评论,来说两句吧...

相关阅读