用法
replaceCurrentColor 函数用于替换 SVG 文件中的 currentColor 为指定颜色。
注意
- 需要在浏览器环境中运行(使用
DOMParser和XMLSerializer) - 使用
fetch获取 SVG 文件
import { replaceCurrentColor } from '@movk/core'
// 获取并替换 SVG 中的 currentColor
try {
const svgContent = await replaceCurrentColor('/icons/star.svg', '#ff0000')
// 创建 Blob 并显示
const blob = new Blob([svgContent], { type: 'image/svg+xml' })
const url = URL.createObjectURL(blob)
const img = document.createElement('img')
img.src = url
document.body.appendChild(img)
}
catch (error) {
console.error('SVG 处理失败:', error)
}
// 只获取 SVG 内容,不替换颜色
const originalSvg = await replaceCurrentColor('/icons/star.svg')
API
replaceCurrentColor(path, color?)
替换 SVG 文件中的 currentColor。
参数
path
string required
SVG 文件路径。
color
string
替换的颜色值,不提供则返回原始 SVG。
返回值
返回值
Promise<string>
处理后的 SVG 字符串。
Changelog
No recent changes