Fabric.js creating image from SVG element failure
Fabric docs suggest it might be possible to create an image from an SVG element using fabric.Image.fromElement(element, optionsopt, callback) → fabric.Image
I seem to be running into TypeError: t.getAttribute is not a function when trying to test it out.
Any idea what I might be doing wrong? Does this method possibly not do what I think it does?
Code sample:
fetch('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg')
.then((response) => response.text())
.then((svg) =>
fabric.Image.fromElement(svg, (image) =>
console.log('image: ', image);
);
)
.catch(function(error)
console.log(error);
);
In a pen:
https://codepen.io/sheepgobeep/pen/GwNOeE
javascript svg fabricjs
add a comment |
Fabric docs suggest it might be possible to create an image from an SVG element using fabric.Image.fromElement(element, optionsopt, callback) → fabric.Image
I seem to be running into TypeError: t.getAttribute is not a function when trying to test it out.
Any idea what I might be doing wrong? Does this method possibly not do what I think it does?
Code sample:
fetch('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg')
.then((response) => response.text())
.then((svg) =>
fabric.Image.fromElement(svg, (image) =>
console.log('image: ', image);
);
)
.catch(function(error)
console.log(error);
);
In a pen:
https://codepen.io/sheepgobeep/pen/GwNOeE
javascript svg fabricjs
add a comment |
Fabric docs suggest it might be possible to create an image from an SVG element using fabric.Image.fromElement(element, optionsopt, callback) → fabric.Image
I seem to be running into TypeError: t.getAttribute is not a function when trying to test it out.
Any idea what I might be doing wrong? Does this method possibly not do what I think it does?
Code sample:
fetch('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg')
.then((response) => response.text())
.then((svg) =>
fabric.Image.fromElement(svg, (image) =>
console.log('image: ', image);
);
)
.catch(function(error)
console.log(error);
);
In a pen:
https://codepen.io/sheepgobeep/pen/GwNOeE
javascript svg fabricjs
Fabric docs suggest it might be possible to create an image from an SVG element using fabric.Image.fromElement(element, optionsopt, callback) → fabric.Image
I seem to be running into TypeError: t.getAttribute is not a function when trying to test it out.
Any idea what I might be doing wrong? Does this method possibly not do what I think it does?
Code sample:
fetch('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg')
.then((response) => response.text())
.then((svg) =>
fabric.Image.fromElement(svg, (image) =>
console.log('image: ', image);
);
)
.catch(function(error)
console.log(error);
);
In a pen:
https://codepen.io/sheepgobeep/pen/GwNOeE
javascript svg fabricjs
javascript svg fabricjs
edited Nov 12 '18 at 17:25
sheepgobeep
asked Nov 12 '18 at 16:54
sheepgobeepsheepgobeep
348418
348418
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
FromElement is not a public method.
That is not clear and i'll fix the docs.
FromElement gets called from the svg parser, but you can use it just on the whole SVG, and you can then discard what you do not need.
In the case of the tiger.svg you get a bunch of shapes and lines that do not make sense if not handled in a group.
You can use
fabric.loadSVGFromString: function(string, callback, reviver, options)
and inside the callback:
callback(objects, options)
var group = fabric.groupSVGElements(objects, options);
Or you can you the fromImage like suggested in the other answer.
The difference is that the Image is like a jpeg that can scale better when zoomed, while the group is slower to render but gives you access to each pieces of the SVG ( that can be useless if you do not need to change anything )
add a comment |
While this is not obvious from fabric.js docs, if you want to create an image from an SVG URL, you should actually use fabric.Image.fromURL(). Try this:
const canvas = new fabric.Canvas("c")
const callback = (image) =>
canvas.add(image).renderAll()
fabric.Image.fromURL('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg', callback,
width: 900,
height: 900,
scaleX: 0.2,
scaleY: 0.2
);<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<canvas id='c' width="500" height="400"></canvas>Also, note that you're using an old version of the library (1.7.22) while you're looking at the updated version of docs (v2+)
Thanks! I'm loading the SVG from a URL here just so my example is a self contained snippet anyone can run and see the console error. Loading SVGs and images from URL does indeed work as expected. I'm just curious as to how thefromElementmethod is supposed to actually work. Will take a look at V1 docs to see if maybefromElementwas modified in V2.
– sheepgobeep
Nov 13 '18 at 16:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266738%2ffabric-js-creating-image-from-svg-element-failure%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
FromElement is not a public method.
That is not clear and i'll fix the docs.
FromElement gets called from the svg parser, but you can use it just on the whole SVG, and you can then discard what you do not need.
In the case of the tiger.svg you get a bunch of shapes and lines that do not make sense if not handled in a group.
You can use
fabric.loadSVGFromString: function(string, callback, reviver, options)
and inside the callback:
callback(objects, options)
var group = fabric.groupSVGElements(objects, options);
Or you can you the fromImage like suggested in the other answer.
The difference is that the Image is like a jpeg that can scale better when zoomed, while the group is slower to render but gives you access to each pieces of the SVG ( that can be useless if you do not need to change anything )
add a comment |
FromElement is not a public method.
That is not clear and i'll fix the docs.
FromElement gets called from the svg parser, but you can use it just on the whole SVG, and you can then discard what you do not need.
In the case of the tiger.svg you get a bunch of shapes and lines that do not make sense if not handled in a group.
You can use
fabric.loadSVGFromString: function(string, callback, reviver, options)
and inside the callback:
callback(objects, options)
var group = fabric.groupSVGElements(objects, options);
Or you can you the fromImage like suggested in the other answer.
The difference is that the Image is like a jpeg that can scale better when zoomed, while the group is slower to render but gives you access to each pieces of the SVG ( that can be useless if you do not need to change anything )
add a comment |
FromElement is not a public method.
That is not clear and i'll fix the docs.
FromElement gets called from the svg parser, but you can use it just on the whole SVG, and you can then discard what you do not need.
In the case of the tiger.svg you get a bunch of shapes and lines that do not make sense if not handled in a group.
You can use
fabric.loadSVGFromString: function(string, callback, reviver, options)
and inside the callback:
callback(objects, options)
var group = fabric.groupSVGElements(objects, options);
Or you can you the fromImage like suggested in the other answer.
The difference is that the Image is like a jpeg that can scale better when zoomed, while the group is slower to render but gives you access to each pieces of the SVG ( that can be useless if you do not need to change anything )
FromElement is not a public method.
That is not clear and i'll fix the docs.
FromElement gets called from the svg parser, but you can use it just on the whole SVG, and you can then discard what you do not need.
In the case of the tiger.svg you get a bunch of shapes and lines that do not make sense if not handled in a group.
You can use
fabric.loadSVGFromString: function(string, callback, reviver, options)
and inside the callback:
callback(objects, options)
var group = fabric.groupSVGElements(objects, options);
Or you can you the fromImage like suggested in the other answer.
The difference is that the Image is like a jpeg that can scale better when zoomed, while the group is slower to render but gives you access to each pieces of the SVG ( that can be useless if you do not need to change anything )
answered Nov 14 '18 at 11:58
AndreaBogazziAndreaBogazzi
9,89032248
9,89032248
add a comment |
add a comment |
While this is not obvious from fabric.js docs, if you want to create an image from an SVG URL, you should actually use fabric.Image.fromURL(). Try this:
const canvas = new fabric.Canvas("c")
const callback = (image) =>
canvas.add(image).renderAll()
fabric.Image.fromURL('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg', callback,
width: 900,
height: 900,
scaleX: 0.2,
scaleY: 0.2
);<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<canvas id='c' width="500" height="400"></canvas>Also, note that you're using an old version of the library (1.7.22) while you're looking at the updated version of docs (v2+)
Thanks! I'm loading the SVG from a URL here just so my example is a self contained snippet anyone can run and see the console error. Loading SVGs and images from URL does indeed work as expected. I'm just curious as to how thefromElementmethod is supposed to actually work. Will take a look at V1 docs to see if maybefromElementwas modified in V2.
– sheepgobeep
Nov 13 '18 at 16:24
add a comment |
While this is not obvious from fabric.js docs, if you want to create an image from an SVG URL, you should actually use fabric.Image.fromURL(). Try this:
const canvas = new fabric.Canvas("c")
const callback = (image) =>
canvas.add(image).renderAll()
fabric.Image.fromURL('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg', callback,
width: 900,
height: 900,
scaleX: 0.2,
scaleY: 0.2
);<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<canvas id='c' width="500" height="400"></canvas>Also, note that you're using an old version of the library (1.7.22) while you're looking at the updated version of docs (v2+)
Thanks! I'm loading the SVG from a URL here just so my example is a self contained snippet anyone can run and see the console error. Loading SVGs and images from URL does indeed work as expected. I'm just curious as to how thefromElementmethod is supposed to actually work. Will take a look at V1 docs to see if maybefromElementwas modified in V2.
– sheepgobeep
Nov 13 '18 at 16:24
add a comment |
While this is not obvious from fabric.js docs, if you want to create an image from an SVG URL, you should actually use fabric.Image.fromURL(). Try this:
const canvas = new fabric.Canvas("c")
const callback = (image) =>
canvas.add(image).renderAll()
fabric.Image.fromURL('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg', callback,
width: 900,
height: 900,
scaleX: 0.2,
scaleY: 0.2
);<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<canvas id='c' width="500" height="400"></canvas>Also, note that you're using an old version of the library (1.7.22) while you're looking at the updated version of docs (v2+)
While this is not obvious from fabric.js docs, if you want to create an image from an SVG URL, you should actually use fabric.Image.fromURL(). Try this:
const canvas = new fabric.Canvas("c")
const callback = (image) =>
canvas.add(image).renderAll()
fabric.Image.fromURL('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg', callback,
width: 900,
height: 900,
scaleX: 0.2,
scaleY: 0.2
);<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<canvas id='c' width="500" height="400"></canvas>Also, note that you're using an old version of the library (1.7.22) while you're looking at the updated version of docs (v2+)
const canvas = new fabric.Canvas("c")
const callback = (image) =>
canvas.add(image).renderAll()
fabric.Image.fromURL('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg', callback,
width: 900,
height: 900,
scaleX: 0.2,
scaleY: 0.2
);<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<canvas id='c' width="500" height="400"></canvas>const canvas = new fabric.Canvas("c")
const callback = (image) =>
canvas.add(image).renderAll()
fabric.Image.fromURL('https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/tiger.svg', callback,
width: 900,
height: 900,
scaleX: 0.2,
scaleY: 0.2
);<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.22/fabric.min.js"></script>
<canvas id='c' width="500" height="400"></canvas>answered Nov 12 '18 at 22:37
shkapershkaper
1,3411814
1,3411814
Thanks! I'm loading the SVG from a URL here just so my example is a self contained snippet anyone can run and see the console error. Loading SVGs and images from URL does indeed work as expected. I'm just curious as to how thefromElementmethod is supposed to actually work. Will take a look at V1 docs to see if maybefromElementwas modified in V2.
– sheepgobeep
Nov 13 '18 at 16:24
add a comment |
Thanks! I'm loading the SVG from a URL here just so my example is a self contained snippet anyone can run and see the console error. Loading SVGs and images from URL does indeed work as expected. I'm just curious as to how thefromElementmethod is supposed to actually work. Will take a look at V1 docs to see if maybefromElementwas modified in V2.
– sheepgobeep
Nov 13 '18 at 16:24
Thanks! I'm loading the SVG from a URL here just so my example is a self contained snippet anyone can run and see the console error. Loading SVGs and images from URL does indeed work as expected. I'm just curious as to how the
fromElement method is supposed to actually work. Will take a look at V1 docs to see if maybe fromElement was modified in V2.– sheepgobeep
Nov 13 '18 at 16:24
Thanks! I'm loading the SVG from a URL here just so my example is a self contained snippet anyone can run and see the console error. Loading SVGs and images from URL does indeed work as expected. I'm just curious as to how the
fromElement method is supposed to actually work. Will take a look at V1 docs to see if maybe fromElement was modified in V2.– sheepgobeep
Nov 13 '18 at 16:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266738%2ffabric-js-creating-image-from-svg-element-failure%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown