当前位置: 首页 > news >正文

原生微信小程序如何动态修改svg图片颜色及尺寸、宽高(封装svgIcon组件)

最终效果

在这里插入图片描述

前言

动态设置Svg图片颜色就是修改Svg源码的path中的fill属性,

通过wx.getFileSystemManager().readFile读取.xlsx文件

把文件转成base64

封装svg-icon组件

1、在项目的components下新建svg-icon文件夹,新增base64.js文件

class Base64 {constructor() {}_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";encode(input) {var output = "";var chr1, chr2, chr3, enc1, enc2, enc3, enc4;var i = 0;input = this._utf8_encode(input);while (i < input.length) {chr1 = input.charCodeAt(i++);chr2 = input.charCodeAt(i++);chr3 = input.charCodeAt(i++);enc1 = chr1 >> 2;enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);enc4 = chr3 & 63;if (isNaN(chr2)) {enc3 = enc4 = 64;} else if (isNaN(chr3)) {enc4 = 64;}output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);}return output;};// public method for decodingdecode(input) {var output = "";var chr1, chr2, chr3;var enc1, enc2, enc3, enc4;var i = 0;input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");while (i < input.length) {enc1 = this._keyStr.indexOf(input.charAt(i++));enc2 = this._keyStr.indexOf(input.charAt(i++));enc3 = this._keyStr.indexOf(input.charAt(i++));enc4 = this._keyStr.indexOf(input.charAt(i++));chr1 = (enc1 << 2) | (enc2 >> 4);chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);chr3 = ((enc3 & 3) << 6) | enc4;output = output + String.fromCharCode(chr1);if (enc3 != 64) {output = output + String.fromCharCode(chr2);}if (enc4 != 64) {output = output + String.fromCharCode(chr3);}}output = this._utf8_decode(output);return output;};// private method for UTF-8 encoding_utf8_encode(string) {string = string.replace(/\r\n/g, "\n");var utftext = "";for (var n = 0; n < string.length; n++) {var c = string.charCodeAt(n);if (c < 128) {utftext += String.fromCharCode(c);} else if ((c > 127) && (c < 2048)) {utftext += String.fromCharCode((c >> 6) | 192);utftext += String.fromCharCode((c & 63) | 128);} else {utftext += String.fromCharCode((c >> 12) | 224);utftext += String.fromCharCode(((c >> 6) & 63) | 128);utftext += String.fromCharCode((c & 63) | 128);}}return utftext;};// private method for UTF-8 decoding_utf8_decode(utftext) {var string = "";var i = 0;var c = 0;var c1 = 0;var c2 = 0;while (i < utftext.length) {c = utftext.charCodeAt(i);if (c < 128) {string += String.fromCharCode(c);i++;} else if ((c > 191) && (c < 224)) {c2 = utftext.charCodeAt(i + 1);string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));i += 2;} else {c2 = utftext.charCodeAt(i + 1);c3 = utftext.charCodeAt(i + 2);string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));i += 3;}}return string;}
}export {Base64
}

2、在项目的components下新建svg-icon文件夹,新增index.js文件

// component/svg.js
const fs = wx.getFileSystemManager()import { Base64 } from './base64.js';
const base64 = new Base64()Component({properties: {// svg图片路径src: {type: String,value: ''},// svg颜色color: {type: String,value: ''},// svg宽度width: {type: String,value: '60rpx'},// svg高度height: {type: String,value: '60rpx'}},observers: {'src,color': function (src, color) {this.getSvgFile(src, color)}},data: {svgData: ''},methods: {getSvgFile(src, color) {let that = this;fs.readFile({filePath: src,encoding: 'UTF-8',position: 0,success(res) {let sourceFile = res.data;let newFile = that.changeColor(sourceFile, color);let svgBase64File = base64.encode(newFile);that.setData({svgData: 'data:image/svg+xml;base64,' + svgBase64File})},fail(res) {console.error(res)}})},changeColor(sourceFile, color) {let newSvg;if (/fill=".*?"/.test(sourceFile)) {newSvg = sourceFile.replace(/fill=".*?"/g, `fill="${color}"`);  // SVG有默认色} else {newSvg = sourceFile.replace(/<svg /g, `<svg fill="${color}" `); // 无默认色}return newSvg}}
})

3、在项目的components下新建svg-icon文件夹,新增index.json文件

{"component": true,"usingComponents": {}
}

4、在项目的components下新建svg-icon文件夹,新增index.wxml文件

<block wx:if="{{svgData}}"><image style="width: {{width}};height: {{height}};" src="{{svgData}}"></image>
</block>

使用svg-icon组件

1、在使用的页面引入组件(即在json文件中引入)

{"usingComponents": {"svg-icon": "/components/svg-icon/index"}
}

2、在wxml文件中如下使用即可

<svg-icon src="/assets/imgs/userCenter/wocwin.svg" color="#3fb65f" />

相关文章

基于ElementUi再次封装基础组件文档


基于ant-design-vue再次封装基础组件文档


vue3+ts基于Element-plus再次封装基础组件文档

http://www.lryc.cn/news/273772.html

相关文章:

  • Python从入门到网络爬虫(面向对象详解)
  • NPDP产品经理含金量高吗?难考吗?
  • 目标检测 YOLOv5 - 推理时的数据增强
  • 篇二:springboot2.7 OAuth2 server使用jdbc存储RegisteredClient
  • 卷积神经网络|导入图片
  • 关于unity的组件VerticalLayoutGroup刷新显示不正常的问题
  • wait 和 notify 这个为什么要在synchronized 代码块中?
  • 大白话说区块链和通证
  • Jvm之垃圾收集器(个人见解仅供参考)
  • Minitab 21软件安装包下载及安装教程
  • Java版商城:Spring Cloud+SpringBoot b2b2c电子商务平台,多商家入驻、直播带货及免 费 小程序商城搭建
  • 阿里云被拉入黑洞模式怎么办?该怎么换ip-速盾网络
  • Android 13.0 recovery竖屏界面旋转为横屏
  • 异地环控设备如何远程维护?贝锐蒲公英解决远程互联难题
  • flutter 判断是否是web环境
  • 视频智能分析/云存储平台EasyCVR接入海康SDK,通道名称未自动更新该如何解决?
  • 后端开发——JDBC的学习(三)
  • Redis 生产环境查找无过期时间的 key
  • Visual Studio 2017编译Python3.8.18源码
  • 【mujoco】Ubuntu20.04中解决mujoco报错raise error.MujocoDependencyError
  • 机器学习的三个方面
  • 关于一名资深Java程序员在移动端的进阶之路
  • clickonce excel 插件发布安装的原理
  • 关于MySQL Cluster
  • 牵绳遛狗你我他文明家园每一天,助力共建文明社区,基于YOLOv7开发构建公共场景下未牵绳遛狗检测识别系统
  • 命令行艺术:简洁指南,效率倍增 | 开源日报 No.136
  • python基础教程五(字典概念和基本操作)
  • 【Delphi 基础知识 11】重载函数的使用
  • 经典目标检测YOLO系列(一)YOLOV1的复现(1)总体架构
  • 《设计模式》之策略模式