Skip to main content
Sebas tian
Participant
May 19, 2023 6:13 PM UTC
Question

Load or Draw a SVG with javascript

Hello everyone! 

I'm working with some images and need to load dynamic SVG to show some graphics shapes, but I found this is not possible natively, so I'm looking for options. 
I can't import the SVG to the FLA, also use transparent MPG or GIF to dynamic load results on tons of files and a huge size increase. 

note: all my SVG are composed only with Path objects. no polygons, and no simple shapes. 
I tried to draw the paths of SVG using Path2D() but it draw only for a fraction of a seccond and then disapears. 

Anyone has tried to draw or import SVG, I have no idea other options, besides change the framework  

Thank you for reding 

    This topic is closed to new replies. Start a new post to keep the conversation going.

    3 replies

    kglad
    Community Expert
    Community Expert
    May 20, 2023 4:37 AM UTC

    why can't you import an svg?

    Translate
    Sebas tian
    Participant
    May 22, 2023 1:38 PM UTC

    Thanks kglad, 

    I'm trying to make a dynamic project. witch load different content maintaining the main structure. 
    I would like to avoid editing the FLA just to import files to the project if those files can be loaded dynamically 

    Translate
    kglad
    Community Expert
    Community Expert
    May 22, 2023 2:30 PM UTC

    you can use preloadJS, https://createjs.com/docs/preloadjs/modules/PreloadJS.html

     

    eg,

     

    displayPicture('./images/svg_file.svg');
    var tl = this;
     
    function displayPicture(imgPath) {
    var image = new Image();
    image.onload = onLoadF;
    image.src=imgPath;
    }
     
    function onLoadF(e) {
    var img = new createjs.Bitmap(e.target)
    tl.addChild(img);
    stage.update();
    }
    Translate