replaceCurrentColor

获取一个 SVG 文件,并将其中的 currentColor 属性替换为指定的颜色。

replaceCurrentColor

此函数用于获取一个 SVG 文件,并将其中的 currentColor 属性替换为指定的颜色。这对于动态改变图标颜色非常有用。

此函数仅在浏览器环境中可用。

用法

假设 /icons/star.svg 的内容是: <svg fill="currentColor">...</svg>

import { replaceCurrentColor } from '@movk/core'

async function getColoredIcon() {
  try {
    // 获取 SVG 并将 currentColor 替换为红色
    const redIconSvg = await replaceCurrentColor('/icons/star.svg', '#FF0000')
    document.getElementById('icon-container').innerHTML = redIconSvg
  }
  catch (error) {
    console.error('处理 SVG 失败:', error)
  }
}

API

replaceCurrentColor(path: string, color?: string): Promise<string>

参数

path
string required
要获取的 SVG 文件的路径。
color
string
用于替换 currentColor 的颜色值。如果未提供,则函数仅获取并返回原始 SVG 内容。

返回值

Promise<string>
返回一个解析为处理后(或原始)SVG 字符串的 Promise。

Changelog

7a568 — feat: 初始化项目结构与核心功能

Copyright © 2024 - 2025 YiXuan - MIT License