Apalabrados
Apalabrados

Reputation: 1146

Load SVG file with svg.js

I've seen several Questions related to this topic in SO and followed them (Loading a SVG file with svg.js) and no success trying to load a svg file.

__this.canvas = SVG('canvas').size(500, 500);
$.get('/cuentos/Rapunzel/prueba.svg', function(data) {
     __this.data = data;
     console.log(__this.data);
     __this.canvas.svg(__this.data);
});

__this.canvas is type of svgjs.Doc which is imported in the following manner:

import SVG from './node_modules/svg.js/svg.js';
declare var svgjs: (domElement: HTMLElement) => svgjs.Library;

The error:

enter image description here

There's something i'm doing wrong or missing.

Any help?

Upvotes: 0

Views: 2567

Answers (2)

Marcelo Nunes
Marcelo Nunes

Reputation: 11

Using jQuery this form works in both 2.7.1 and 3.0.x i tested

    var svgp = SVG($('#<name of svg>')[0]);

Upvotes: 0

fjc
fjc

Reputation: 5845

JQuery's $.get, for SVGs, gives back a DOM object representing the SVG by default. SVG.js's svg function, however, expects a string.

You can simply force JQuery to give back a string like that:

$.get('/cuentos/Rapunzel/prueba.svg', function(data) {
   __this.data = data;
   console.log(__this.data);
   __this.canvas.svg(__this.data);
}, 'text');

PS: I opened up a PR against SVG.js to display a proper error message for that case: https://github.com/svgdotjs/svg.js/pull/870

Upvotes: 1

Related Questions