Snap.svg icon indicating copy to clipboard operation
Snap.svg copied to clipboard

create SVG in memory without displaying it

Open kimgerdes opened this issue 6 years ago • 3 comments
trafficstars

Hello, I am trying to use Snap to precompute a series of SVGs, that are shown on demand. is that possible? My function creates the SVG with s=Snap(600, 800) and returns s This allows to paste s anywhere but it is already created on the bottom of the page without any id associated. We would just like to get the SVG in the variable. Thanks for any pointers to what I could do.

kimgerdes avatar Sep 27 '19 13:09 kimgerdes

I am also interested in knowing if this can be done. Thanks !

kirianguiller avatar Feb 19 '23 21:02 kirianguiller

Not entirely sure what you are after, but you could make it invisible, or off screen, or create a fragment and append as needed.

ibrierley avatar Feb 19 '23 22:02 ibrierley

I had a need at one point to take an SVG, clone it, modify it, and display as an Image. remove was the key to keep it off the bottom of the screen.

Example:

   var clone = svg.clone().remove;
    s_clone.attr({
        viewBox: '0 0 300 300',
        height: '640px',
        width: '640px',
    });
   //...
    var paths = clone.selectAll('path');
    paths.forEach(function (elem, i) {
        var ropath = elem.clone().remove(); //each clone needs to be removed
        var expanded = '<svg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 300 300\' width=\'640px\' height=\'640px\'>' + ropath.toString() + '</svg>'; //individual components of the original
        images[num - i] = new Image();
        images[num - i].src = 'data:image/svg+xml;base64,' + btoa(expanded);
   });
    images[0] = new Image();
    images[0].src = 'data:image/svg+xml;base64,' + btoa(s_clone.toString());

matelich avatar Feb 20 '23 14:02 matelich