image2 = async (node) => {
const n = d3.select(node);
const pb = n.style("padding-bottom");
const bg = n.style("background-color");
if (node.nodeName === "FIGURE") {
n.style("padding-bottom", ".4em");
n.style("background-color", "white");
}
const colorUrlMap = fixColorUrl(n);
const im = await htmlToImage.toCanvas(node, { pixelRatio: 2 });
im.style = `width: ${im.width >> 1}px`;
if (pb) n.style("padding-bottom", pb);
if (bg) n.style("background-color", bg);
for (const [d, kind, v] of colorUrlMap) d.style[kind] = v;
return im;
function fixColorUrl(n) {
const re = new RegExp(
`^url[(]['"]?${document.location.href.replaceAll(
/[|\\{}()[\]^$+*?.]/g,
"\\$&"
)}(#.*?)['"]?[)]$`
);
const colorUrlMap = [];
for (const d of node.querySelectorAll("*")) {
const styles = getComputedStyle(d);
for (const kind of ["stroke", "fill"]) {
const st = styles[kind];
if (st && re.test(st)) {
colorUrlMap.push([d, kind, d.style[kind]]);
d.style[kind] = st.replace(re, 'url("$1")');
}
}
}
return colorUrlMap;
}
}