Open techniq opened 6 months ago
SVG reproduction when using CSS classes / variables will require some extra processing to fully reproduce visual.
Mike Bostock's examples of serialize()
and rasterize()
for SVG look straight forward (using document.createTreeWalker()
.
Likely how Download SVG
and Download PNG
are implemented (or at least the basics).
See discussion for approach with SVG
function downloadSVG() {
const svgEl = document.querySelector('.layercake-layout-svg');
if (svgEl) {
svgEl?.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
svgEl.querySelector('.svg_text')?.classList.remove('hidden');
const svgString = new XMLSerializer().serializeToString(svgEl);
const a = document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([svgString]));
a.download = 'download.svg';
a.click();
a.remove();
svgEl?.removeAttribute('xmlns');
svgEl.querySelector('.svg_text')?.classList.add('hidden');
}
};
See discussion for approach with Canvas
function downloadPNG() {
const canvas = document.querySelector('.layercake-layout-canvas') as HTMLCanvasElement;
const ctx = canvas.getContext('2d');
const dataUrl = ctx?.canvas.toDataURL('image/png', 1);
const a = document.createElement('a');
a.href = dataUrl;
a.download = 'download.png';
a.click();
a.remove();
};
save as svg and png looks good to me...
<Svg>
,<Canvas>
, etc) will make some options more difficult / impossible<Canvas>
, should be able to use:<Svg>
, should be able to use:new XMLSerializer().serializeToString(svg)
DownloadCanvas
or create a newCanvas
offscreen with higher container width/height overridden