Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download as PNG / SVG / etc #162

Open
techniq opened this issue May 20, 2024 · 5 comments
Open

Download as PNG / SVG / etc #162

techniq opened this issue May 20, 2024 · 5 comments

Comments

@techniq
Copy link
Owner

techniq commented May 20, 2024

@techniq
Copy link
Owner Author

techniq commented May 20, 2024

SVG reproduction when using CSS classes / variables will require some extra processing to fully reproduce visual.

image

@techniq
Copy link
Owner Author

techniq commented May 20, 2024

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).

image

see also: https://talk.observablehq.com/t/svg-to-png/6957

@techniq
Copy link
Owner Author

techniq commented Jun 27, 2024

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');
    }
};

@techniq
Copy link
Owner Author

techniq commented Jul 18, 2024

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();
};

@meta-cretin
Copy link

save as svg and png looks good to me...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants