created() {
this.title = this.readFile("../../../static/images/雷达.svg");
console.log(this.title,"svg文件读取");
var newSvg=this.title.replace(/#(?:[0-9a-fA-F]{6})/, "#f1f1f1");
console.log(newSvg,"修改颜色后的svg文件");
var href = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(newSvg)));//svg转base64
console.log("svg转base64",href);
},
methods:{
// 读取svg代码
readFile(filePath) {
// 创建一个新的xhr对象
let xhr = null
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest()
} else {
xhr = new ActiveXObject('Microsoft.XMLHTTP')
}
const okStatus = document.location.protocol === 'file' ? 0 : 200
xhr.open('GET', filePath, false)
xhr.overrideMimeType('image/svg+xml')
xhr.send(null)
return xhr.status === okStatus ? xhr.responseText : null
},
}
vue读取修改svg颜色 并转换为base64格式
最新推荐文章于 2024-07-29 14:17:59 发布