feat: SVG 图标序列化为图片(第三版)
- 不修改 backgroundUrls(const) - 节点创建后直接修改 node.backgroundImages - XMLSerializer + btoa 序列化 SVG
This commit is contained in:
+14
-1
@@ -985,7 +985,7 @@
|
||||
pushCaptureChild(children, afterNode);
|
||||
|
||||
const type = element.tagName === "IMG" || backgroundUrls.length ? "RECTANGLE" : "FRAME";
|
||||
return {
|
||||
const node = {
|
||||
id: `node-${++nodeCounter}`,
|
||||
type,
|
||||
tag: element.tagName,
|
||||
@@ -1005,6 +1005,19 @@
|
||||
backgroundImages: backgroundUrls,
|
||||
children
|
||||
};
|
||||
|
||||
// SVG 图标 → 序列化为 data URL 图片
|
||||
if ((element.tagName === "SVG" || element.tagName === "svg") && !node.backgroundImages.length) {
|
||||
try {
|
||||
const svgClone = element.cloneNode(true);
|
||||
if (!svgClone.getAttribute("xmlns")) svgClone.setAttribute("xmlns", "http://www.w3.org/2000/svg");
|
||||
const svgStr = new XMLSerializer().serializeToString(svgClone);
|
||||
node.backgroundImages = ["data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgStr)))];
|
||||
node.type = "RECTANGLE";
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
function normalizeAssetUrl(url) {
|
||||
|
||||
Reference in New Issue
Block a user